Skip to content

Instantly share code, notes, and snippets.

@Filipvds
Created November 12, 2014 07:38
Show Gist options
  • Save Filipvds/9cc255ac8a8ee250e86b to your computer and use it in GitHub Desktop.
Save Filipvds/9cc255ac8a8ee250e86b to your computer and use it in GitHub Desktop.
vhost creation script
#!/usr/bin/env bash
# Run this as sudo!
# I move this file to /usr/local/bin/vhost and run command 'vhost' from anywhere, using sudo.
#
# Show Usage, Output to STDERR
#
function show_usage {
cat <<- _EOF_
Create a new vhost
-r RootFolder - i.e. public
-h Help - Show this menu.
-s ServerName - i.e. example.com or sub.example.com
-a ServerAlias - i.e. *.example.com or another domain altogether
-u UserName - i.e. expweb
_EOF_
exit 1
}
#
# Output vHost skeleton, fill with userinput
# To be outputted into new file
#
function create_vhost {
cat <<- _EOF_
<VirtualHost *:8080>
ServerName $ServerName
$ServerAlias
ServerAdmin serveradmin@$ServerName
DocumentRoot $DocumentRoot
<IfModule mod_fcgid.c>
SuexecUserGroup $User $User
<Directory $DocumentRoot>
Options +ExecCGI
AllowOverride All
AddHandler fcgid-script .php
FCGIWrapper $WrapperDir/php-fcgi-starter .php
Order allow,deny
Allow from all
</Directory>
</IfModule>
ErrorLog /var/www/$User/log/error_$ReverseServerName.log
CustomLog /var/www/$User/log/access_$ReverseServerName.log combined
ServerSignature Off
</VirtualHost>
_EOF_
}
#
# Output vHost skeleton, fill with userinput
# To be outputted into new file
#
function create_wrapper {
cat <<- _EOF_
#!/bin/sh
PHPRC=/etc/php5/cgi/
export PHPRC
export PHP_FCGI_MAX_REQUESTS=5000
export PHP_FCGI_CHILDREN=8
exec /usr/lib/cgi-bin/php
_EOF_
}
# Sanity Check - are there two arguments with 2 values?
if [ "$#" -lt 4 ]; then
show_usage
fi
# Parse flags
while getopts "d:r:s:a:u:p:h" OPTION; do
case $OPTION in
h)
show_usage
;;
r)
RootFolder=$OPTARG
;;
s)
ServerName=$OPTARG
;;
a)
Alias=$OPTARG
;;
u)
User=$OPTARG
;;
p)
Password=$OPTARG
;;
*)
show_usage
;;
esac
done
# Create random password
if [ "$Password" == "" ]; then
Password=`cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 12 | head -n 1`
fi
# Create reverse servername
ReverseServerName=`echo $ServerName|awk -F. '{s="";for (i=NF;i>1;i--) s=s sprintf("%s.",$i);$0=s $1}1'`
# If root folder is set:
if [ "$RootFolder" != "" ]; then
RootFolder=$RootFolder
else
RootFolder="public"
fi
# # Create document root
DocumentRoot="/var/www/$User/$ReverseServerName/$RootFolder"
WrapperDir="/var/www/$User/wrapper"
LogDir="/var/www/$User/log"
# Check if username exist
if id -u $User >/dev/null 2>&1; then
echo "User $User already exists"
else
echo "Adding user/group $User with password $Password"
groupadd $User
useradd -d /var/www/$User -m -g $User $User
echo "$User:$Password" | chpasswd
fi
echo "Creating $DocumentRoot" && mkdir -p $DocumentRoot
echo "Creating $WrapperDir" && mkdir -p $WrapperDir
echo "Creating $LogDir" && mkdir -p $LogDir
chown -R $User:$User /var/www/$User
# If alias is set:
if [ "$Alias" != "" ]; then
ServerAlias="ServerAlias "$Alias
else
ServerAlias="ServerAlias www."$ServerName
fi
if [ -f "/etc/apache2/sites-available/$ReverseServerName.conf" ]; then
echo 'Vhost already exists. Aborting...'
show_usage
else
# Creating vhost
echo "Creating vhost config /etc/apache2/sites-available/$ReverseServerName.conf"
create_vhost > /etc/apache2/sites-available/${ReverseServerName}.conf
# Creating wrapper
echo "Creating wrapper $WrapperDir/php-fcgi-starter"
create_wrapper > ${WrapperDir}/php-fcgi-starter
chown -R ${User}:${User} ${WrapperDir}/php-fcgi-starter
chmod 755 ${WrapperDir}/php-fcgi-starter
# Enable Site
echo "Enabling vhost"
a2ensite ${ReverseServerName}
echo "Reloading apache"
service apache2 reload
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment