Skip to content

Instantly share code, notes, and snippets.

@faysalmahamud
Last active March 4, 2017 13:08
Show Gist options
  • Save faysalmahamud/0f040e1e106c40ba1779fb6c5ad129ed to your computer and use it in GitHub Desktop.
Save faysalmahamud/0f040e1e106c40ba1779fb6c5ad129ed to your computer and use it in GitHub Desktop.
Virtualhost Manage Script For Shell
#inspired from https://github.com/RoverWire/virtualhost
# Make a file named virtualhost and place it in /usr/local/bin folder.
# Give +x permission to the file.
# use as
# command create, existing, delete
# virtualhost command sitename.fmt folder_path virtual_host_name
# i.e virtualhost create example.fmt /var/www/example //create new domain
# this will create site-available folder example.com.conf
# some times we need to group projects in same virtual hosts
# i.e virtualhost create example.fmt /var/www/example wordpress
# this will create wordpress.conf. you can use multiple times for wordpress projects.
# i.e virtualhost existing example2.fmt /var/www/example2 wordpress
# if domain name in host but not virtual host it also add virtual host configuration in the wordpress
# i.e virtualhost delete example2.fmt /var/www/example2 wordpress
# this command delete virtual host, domain name and also if you want to delete folder directory.
#!/bin/bash
### Set Language
TEXTDOMAIN=virtualhost
### Set default parameters
action=$1
domain=$2
rootDir=$3
exitsConf=$4
owner=$(who am i | awk '{print $1}')
email='webmaster@localhost'
sitesEnable='/etc/apache2/sites-enabled/'
sitesAvailable='/etc/apache2/sites-available/'
userDir='/var/www/'
if [ "$exitsConf" == "" ]; then
conf=$domain
else
conf=$exitsConf
fi
sitesAvailabledomain=$sitesAvailable$conf.conf
### don't modify from here unless you know what you are doing ####
if [ "$(whoami)" != 'root' ]; then
echo $"You have no permission to run $0 as non-root user. Use sudo"
exit 1;
fi
if [ "$action" != 'create' ] && [ "$action" != 'delete' ] && [ "$action" != 'existing' ]
then
echo $"You need to prompt for action (create or delete) -- Lower-case only"
exit 1;
fi
while [ "$domain" == "" ]
do
echo -e $"Please provide domain. e.g.dev,staging"
read domain
done
if [ "$rootDir" == "" ]; then
rootDir=${domain//./}
fi
### if root dir starts with '/', don't use /var/www as default starting point
if [[ "$rootDir" =~ ^/ ]]; then
userDir=''
fi
rootDir=$userDir$rootDir
echo $action;
if [ "$action" == 'create' ]
then
### check if domain already exists
if [ -e $sitesAvailabledomain ]; then
echo -e $"This domain already exists.\nPlease Try Another one"
exit;
fi
if grep -q "$domain" /etc/hosts; then
echo "Exists"
exit;
fi
### check if directory exists or not
if ! [ -d $rootDir ]; then
### create the directory
mkdir $rootDir
### give permission to root dir
chmod 755 $rootDir
### write test file in the new domain dir
if ! echo "<?php echo phpinfo(); ?>" > $rootDir/phpinfo.php
then
echo $"ERROR: Not able to write in file $rootDir/phpinfo.php. Please check permissions"
exit;
else
echo $"Added content to $rootDir/phpinfo.php"
fi
fi
### create virtual host rules file
if ! echo "
<VirtualHost *:80>
ServerAdmin $email
ServerName $domain
DocumentRoot $rootDir
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>" > $sitesAvailabledomain
then
echo -e $"There is an ERROR creating $domain file"
exit;
else
echo -e $"\nNew Virtual Host Created\n"
fi
### Add domain in /etc/hosts
if ! echo "127.0.0.1 $domain" >> /etc/hosts
then
echo $"ERROR: Not able to write in /etc/hosts"
exit;
else
echo -e $"Host added to /etc/hosts file \n"
fi
if [ "$owner" == "" ]; then
chown -R $(whoami):$(whoami) $rootDir
else
chown -R $owner:$owner $rootDir
fi
### enable website
a2ensite $domain
### restart Apache
/etc/init.d/apache2 restart
### show the finished message
echo -e $"Complete! \nYou now have a new Virtual Host \nYour new host is: http://$domain \nAnd its located at $rootDir"
exit;
elif [ "$action" == 'existing' ]
then
if grep -q "$domain" /etc/hosts; then
echo "Exists"
if grep -q "$domain" $sitesAvailabledomain; then
echo "Virtual Host found in the $domain file";
else
echo -e $"\nNew Virtual Host Created $domain \n";
echo "
<VirtualHost *:80>
ServerAdmin $email
ServerName $domain
DocumentRoot $rootDir
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>" >> $sitesAvailabledomain
fi
exit;
fi
### check if directory exists or not
if ! [ -d $rootDir ]; then
### create the directory
mkdir $rootDir
### give permission to root dir
chmod 755 $rootDir
### write test file in the new domain dir
if ! echo "<?php echo phpinfo(); ?>" > $rootDir/phpinfo.php
then
echo $"ERROR: Not able to write in file $rootDir/phpinfo.php. Please check permissions"
exit;
else
echo $"Added content to $rootDir/phpinfo.php"
fi
fi
### create virtual host rules file
if ! echo "
<VirtualHost *:80>
ServerAdmin $email
ServerName $domain
DocumentRoot $rootDir
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>" >> $sitesAvailabledomain
then
echo -e $"There is an ERROR creating $domain file"
exit;
else
echo -e $"\nNew Virtual Host Created\n"
fi
### Add domain in /etc/hosts
if ! echo "127.0.0.1 $domain" >> /etc/hosts
then
echo $"ERROR: Not able to write in /etc/hosts"
exit;
else
echo -e $"Host added to /etc/hosts file \n"
fi
if [ "$owner" == "" ]; then
chown -R $(whoami):$(whoami) $rootDir
else
chown -R $owner:$owner $rootDir
fi
### restart Apache
/etc/init.d/apache2 restart
### show the finished message
echo -e $"Complete! \nYou now have a new Virtual Host \nYour new host is: http://$domain \nAnd its located at $rootDir"
exit;
else
### check whether domain already exists
if ! [ -e $sitesAvailabledomain ]; then
echo -e $"This domain does not exist.\nPlease try another one"
exit;
else
### Delete domain in /etc/hosts
newhost=${domain//./\\.}
sed -i "/$newhost/d" /etc/hosts
### disable website
a2dissite $domain
### restart Apache
/etc/init.d/apache2 restart
### Delete virtual host rules files
rm $sitesAvailabledomain
fi
### check if directory exists or not
if [ -d $rootDir ]; then
echo -e $"Delete host root directory ? (y/n)"
read deldir
if [ "$deldir" == 'y' -o "$deldir" == 'Y' ]; then
### Delete the directory
rm -rf $rootDir
echo -e $"Directory deleted"
else
echo -e $"Host directory conserved"
fi
else
echo -e $"Host directory not found. Ignored"
fi
### show the finished message
echo -e $"Complete!\nYou just removed Virtual Host $domain"
exit 0;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment