Created
January 2, 2014 21:30
-
-
Save desmondmorris/8227288 to your computer and use it in GitHub Desktop.
Automatically creating new virtual hosts with Nginx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Script by: Seb Dangerfield - sebdangerfield.me.uk | |
# http://www.sebdangerfield.me.uk/2011/03/automatically-creating-new-virtual-hosts-with-nginx-bash-script/ | |
NGINX_CONFIG='/etc/nginx/sites-available' | |
NGINX_SITES_ENABLED='/etc/nginx/sites-enabled' | |
WEB_DIR='/var/www' | |
SED=`which sed` | |
CURRENT_DIR=`dirname $0` | |
if [ -z $1 ]; then | |
echo "No domain name given" | |
exit 1 | |
fi | |
DOMAIN=$1 | |
# check the domain is roughly valid! | |
PATTERN="^([[:alnum:]]([[:alnum:]\-]{0,61}[[:alnum:]])?\.)+[[:alpha:]]{2,6}$" | |
if [[ "$DOMAIN" =~ $PATTERN ]]; then | |
DOMAIN=`echo $DOMAIN | tr '[A-Z]' '[a-z]'` | |
echo "Creating hosting for:" $DOMAIN | |
else | |
echo "invalid domain name" | |
exit 1 | |
fi | |
#Replace dots with underscores | |
SITE_DIR=`echo $DOMAIN | $SED 's/\./_/g'` | |
# Now we need to copy the virtual host template | |
CONFIG=$NGINX_CONFIG/$DOMAIN.conf | |
sudo cp $CURRENT_DIR/virtual_host.template $CONFIG | |
sudo $SED -i "s/DOMAIN/$DOMAIN/g" $CONFIG | |
sudo $SED -i "s!ROOT!$WEB_DIR/$SITE_DIR!g" $CONFIG | |
# set up web root | |
sudo mkdir $WEB_DIR/$SITE_DIR | |
sudo chown nginx:nginx -R $WEB_DIR/$SITE_DIR | |
sudo chmod 600 $CONFIG | |
# create symlink to enable site | |
sudo ln -s $CONFIG $NGINX_SITES_ENABLED/$DOMAIN.conf | |
# reload Nginx to pull in new config | |
sudo /etc/init.d/nginx reload | |
# put the template index.html file into the new domains web dir | |
sudo cp $CURRENT_DIR/index.html.template $WEB_DIR/$SITE_DIR/index.html | |
sudo $SED -i "s/SITE/$DOMAIN/g" $WEB_DIR/$SITE_DIR/index.html | |
sudo chown nginx:nginx $WEB_DIR/$SITE_DIR/index.html | |
echo "Site Created for $DOMAIN" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment