Skip to content

Instantly share code, notes, and snippets.

@ahmadsoe
Last active August 29, 2015 14:04
Show Gist options
  • Save ahmadsoe/e03dfb0abe237375b6b4 to your computer and use it in GitHub Desktop.
Save ahmadsoe/e03dfb0abe237375b6b4 to your computer and use it in GitHub Desktop.
#! /bin/bash
# Acknowledgements
# I forked this code from Chris Fidao see: https://gist.github.com/fideloper
# May need to run this as sudo!
# I have it in /usr/local/bin and run command 'vhost' from anywhere, using sudo.
NORMAL=$(tput sgr0)
GREEN=$(tput setaf 2; tput bold)
YELLOW=$(tput setaf 3)
RED=$(tput setaf 1)
red() {
echo -e -n "$RED$*$NORMAL"
}
green() {
echo -e -n "$GREEN$*$NORMAL"
}
yellow() {
echo -e -n "$YELLOW$*$NORMAL"
}
#
# Show Usage, Output to STDERR
#
show_usage() {
cat <<- _EOF_
Create a new vHost in Ubuntu Server
Assumes /etc/apache2/sites-available and /etc/apache2/sites-enabled setup used
-s ServerName - i.e. example.com or sub.example.com
-h Help - Show this menu.
example: vhost.sh -s 'exampleSite.com'
_EOF_
exit 1
}
#
# Output vHost skeleton, fill with userinput
# To be outputted into new file
#
create_vhost() {
cat <<- _EOF_
<VirtualHost *:80>
ServerAdmin webmaster@$ServerName
ServerName $ServerName
DocumentRoot /var/www/$ServerName/public_html
<Directory /var/www/$ServerName/public_html>
Options -Indexes +FollowSymLinks +MultiViews
#placed so local host can serve up website to itself
Require local
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog /var/www/$ServerName/logfiles/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/www/$ServerName/logfiles/access.log combined
</VirtualHost>
_EOF_
}
#set correct folder & file permissions
set_folder_permissions() {
chown -R www-data:www-data /var/www/$ServerName # make sure same owner:group
chmod -R go-rwx /var/www/$ServerName # Remove all group/other permissions
chmod -R g+rw /var/www/$ServerName # Add group read/write
chmod g+x /var/www/$ServerName #Ensure document root has execute or according to Ubuntu's docs: "Folders (directories) must have 'execute' permissions set (x or 1), or folders (directories) will NOT FUNCTION as folders (directories) and WILL DISAPPEAR from view in the file browser (Nautilus)."
chmod g+x /var/www/$ServerName/public_html
chmod -R o+r /var/www/$ServerName # Allow other to read only
}
#Sanity Check - are there two arguments with 2 values?
if [ $# -ne 2 ]; then
show_usage
fi
#Parse flags
while getopts "d:s:g:o:" OPTION; do
case $OPTION in
h)
show_usage
;;
s)
ServerName=$OPTARG
;;
*)
show_usage
;;
esac
done
#create directories if not created
if [ ! -d /var/www/$ServerName ]; then
mkdir -p /var/www/$ServerName
set_folder_permissions
fi
if [ ! -d /var/www/$ServerName/logfiles ]; then
mkdir -p /var/www/$ServerName/logfiles
set_folder_permissions
fi
if [ ! -d /var/www/$ServerName/public_html ]; then
mkdir -p /var/www/$ServerName/public_html
set_folder_permissions
fi
#since Apache 2.4 by default all vhost config files need to have ".conf" at the end of them
if [ -f "/etc/apache2/sites-available/${ServerName}.conf" ]; then
red "Error: "
echo -n "vHost already exists. Please remove file: "
yellow "/etc/apache2/sites-available/${ServerName}.conf"
echo " before re-running this script again."
else
create_vhost > /etc/apache2/sites-available/${ServerName}.conf
a2ensite $ServerName #Enable site
service apache2 restart #Optional implementation
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment