Skip to content

Instantly share code, notes, and snippets.

@SpartakusMd
Last active January 23, 2017 11:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SpartakusMd/702b846ccde6efd510dc to your computer and use it in GitHub Desktop.
Save SpartakusMd/702b846ccde6efd510dc to your computer and use it in GitHub Desktop.
VHost generator
#!/bin/bash
# Colors
txtred='\e[0;31m' # Red
txtgrn='\e[0;32m' # Green
txtylw='\e[0;33m' # Yellow
txtblu='\e[0;34m' # Blue
undred='\e[4;31m' # Red
undgrn='\e[4;32m' # Green
undylw='\e[4;33m' # Yellow
undblu='\e[4;34m' # Blue
txtrst='\e[0m' # Text Reset
# Display usage info
vhost-usage() {
cat <<"USAGE"
Usage: vhost [OPTIONS] <name>
-h|--help this screen
-pub to create the webhost root in ~/www/name/public/
-url to specify a local address, default is http://name.local
-rm to remove a previously created vhost, see examples
-d to specify the webroot directory location, default is in ~/www (NO TRAILING SLASH)
-email to specify the email of the administrator in the virtual host file
-l|--list to list the current virtual hosts
Examples:
vhost mysite.local this will create a new apache2 vhost named mysite.local with a webroot of /var/www/mysite.local/ reachable at http://mysite.local
vhost -pub mysite.local this will create a new apache2 vhost named mysite.local with a webroot of /var/www/mysite.local/public_html/ reachable at http://mysite.local
vhost -d ~/sites/mysite/myroot -url dev.mysite.dev mysite this will create a new apache2 vhost named mysite with a webroot of ~/sites/mysite/myroot reachable at http://dev.mysite.dev
vhost -rm mysite.local mysite this will remove the apache2 vhost named mysite and remove the mysite.local entry from the /etc/hosts file. Be sure to specify both
USAGE
exit 0
}
# Delete a virtual host file and it's entry in /etc/hosts
vhost-remove() {
sudo -v
echo -e "${txtred}Are you sure you want to remove the site ${txtblu}$url${txtred} ? (Y/N)${txtrst}"
read confirm
# Confirm the removing
if [ "$confirm" == "n" ] || [ "$confirm" == "N" ]; then
exit 0
fi
echo -e "${txtred}Removing${txtrst} ${txtblu}$url${txtrst} from /etc/hosts."
sudo sed -i '/'$url'/d' /etc/hosts
echo "Disabling and deleting the $name.conf virtual host."
sudo a2dissite $name.conf
sudo rm /etc/apache2/sites-available/$name.conf
sudo service apache2 reload
exit 0
}
# Displaying the list of virtual hosts
vhost-list() {
echo -e "${txtblu}Available virtual hosts:${txtrst}"
ls -l /etc/apache2/sites-available/
echo -e "${txtblu}Enabled virtual hosts:${txtrst}"
ls -l /etc/apache2/sites-enabled/
exit 0
}
# Last argument is the domain name.
# Ask for user input if no parameters were provided.
if [ ! $1 ]; then
echo -e "${txtblu}Please provide a domain name${txtrst}"
read name
else
name="${!#}"
fi
# Define and create default values
email="webmaster@localhost"
url="$name"
webroot="/var/www/$name"
# Loop to read options and arguments
while [ $1 ]; do
case "$1" in
'-l'|'--list') vhost-list;;
'-h'|'--help') vhost-usage;;
'-rm') url="$2"
vhost-remove;;
'-pub') webroot="/var/www/$name/public_html";;
'-d') webroot="$2";;
'-url') url="$2";;
'-email') email="$2";;
esac
shift
done
sudo -v
# Check if the webroot exists
if [ ! -d "$webroot" ]; then
echo -e "${txtgrn}Creating $webroot directory${txtrst}"
sudo mkdir -p $webroot
sudo chown www-data:www-data $webroot
fi
echo -e "Checking for the virtual host template file..."
if [ ! -f /etc/apache2/sites-available/template ]; then
echo "Downloading template file..."
sudo wget -P /etc/apache2/sites-available/ https://gist.githubusercontent.com/SpartakusMd/b36b05826d7078531a27/raw/template
fi
echo -e "Creating the new ${txtblu}$name.conf${txtrst} virtual host file that has a webroot of: ${txtblu}$webroot${txtrst}"
sudo cp /etc/apache2/sites-available/template /etc/apache2/sites-available/$name.conf
sudo sed -i 's/template.email/'$email'/g' /etc/apache2/sites-available/$name.conf
sudo sed -i 's/template.url/'$url'/g' /etc/apache2/sites-available/$name.conf
sudo sed -i 's#template.webroot#'$webroot'#g' /etc/apache2/sites-available/$name.conf
echo "Adding $url to the /etc/hosts file..."
sudo sed -i '1s/^/127.0.0.1 '$url'\n/' /etc/hosts
sudo a2ensite $name.conf
sudo service apache2 reload
echo -e "${txtgrn}Virtual host $name created with a webroot at $webroot reachable from ${txtblu}http://$url${txtrst}"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment