Skip to content

Instantly share code, notes, and snippets.

@ashishmohite
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save ashishmohite/346c9095d957c3c4fb0b to your computer and use it in GitHub Desktop.

Select an option

Save ashishmohite/346c9095d957c3c4fb0b to your computer and use it in GitHub Desktop.
Automation script for apache virtual host creation
#!/usr/bin/env bash
list() {
echo "-------------------------------------------------------------------------------------------------"
echo "Available virtual hosts:"
echo "-------------------------------------------------------------------------------------------------"
ls -l /etc/apache2/sites-available/
echo "-------------------------------------------------------------------------------------------------"
echo "Enabled virtual hosts:"
echo "-------------------------------------------------------------------------------------------------"
ls -l /etc/apache2/sites-enabled/
echo "-------------------------------------------------------------------------------------------------"
}
remove() {
vurl=$1
sudo -v
echo "Removing $url from /etc/hosts."
sudo sed -i '/'$vurl'/d' /etc/hosts
echo "Disabling and deleting the $vurl virtual host."
sudo a2dissite $vurl
sudo rm /etc/apache2/sites-available/$vurl
sudo service apache2 reload
}
create() {
vurl = $1
docroot = $2
if [ ! -d $docroot ]
then
echo Error: "Entered path is not correct"
exit $err
fi
echo '-------------------------------------------------------------------------------------------------'
printf "URL : $vurl"
echo '-------------------------------------------------------------------------------------------------'
printf "DocumentRoot : $docroot"
echo '-------------------------------------------------------------------------------------------------'
printf '\n\n\n'
echo "Creating the template file\n\n"
cat <<EOF > ./template
<VirtualHost *:80>
ServerName template.name
DocumentRoot template.docroot
<Directory template.docroot/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
</VirtualHost>
EOF
sudo cp ./template /etc/apache2/sites-available/$vurl
sudo sed -i "s/template.name/$vurl/g" /etc/apache2/sites-available/$vurl
sudo sed -i "s#template.docroot#$docroot#g" /etc/apache2/sites-available/$vurl
printf "Removing temp. template file...\n\n"
sudo rm -rf ./template
printf "Adding $vurl to /etc/hosts file...\n\n"
sudo sed -i "1s/^/127.0.0.1 $vurl\n/" /etc/hosts
printf "Enabling the site...\n\n"
sudo a2ensite $vurl
printf "Reloading the server...\n\n"
sudo service apache2 reload
printf "Congratulations your virtual host[$vurl] has been created successfully \n\n"
printf "You can access it at http://$vurl and can add files at $docroot\n\n"
}
usage() {
cat <<EOF
Usage: virtualhost [OPTIONS] <name>
-u | --url to set the url for VirtualHost
-d | --docroot to set the document root directory location
-rm | --remove to remove a previously created vhost
-l | --list to list the current virtual hosts
Examples:
virtualhost.sh -u test.local -d /home/ashish/test this will create a new VirtualHost with url test.local with a webroot of /home/ashish/test reachable at http://test.local
virtualhost.sh -rm test.local mysite this will remove the VirtualHost with url test.local and remove the test.local entry from the /etc/hosts file.
EOF
exit 0
}
while [ "$1" != "" ]; do
case $1 in
-u | --url ) shift
vurl=$1
;;
-d | --docroot ) shift
docroot=$1
;;
-rm | --remove ) shift
vurl=$1
remove $vurl
exit
;;
-l | --list ) list
exit
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
if [ ! -z "$vurl" -a ! -z "$docroot" ]
then
create $vurl $docroot
else
echo "please enter the url for the virtual host (e.g. virtualhostname.local,test.local) :"
read vurl
echo "plase enter the path of document root (e.g. /home/username/test/):"
read docroot
create $vurl $docroot
fi
@mukeshshukla
Copy link

Suggestion : You can enhance it for accepting the extra derivatives too. you can accept multiple argument as and read them as key and value . and then you can use to append those derivatives.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment