Skip to content

Instantly share code, notes, and snippets.

@troyxmccall
Last active December 12, 2015 07:19
Show Gist options
  • Save troyxmccall/4735943 to your computer and use it in GitHub Desktop.
Save troyxmccall/4735943 to your computer and use it in GitHub Desktop.
create vhosts on the fly for nginx on vagrant //rename to shell (vhoster.sh)
# sudo ./vhoster.sh example.com
NGINX_CONFIG='/etc/nginx/sites-available'
NGINX_SITES_ENABLED='/etc/nginx/sites-enabled'
WEB_DIR='/vagrant'
SED=`which sed`
CURRENT_DIR=`dirname $0`
if [ -z $1 ]; then
echo "No domain name given"
exit 1
fi
DOMAIN=$1
# regex this bitch to validate
PATTERN="^([[:alnum:]]([[:alnum:]\-]{0,61}[[:alnum:]])?\.)+[[:alpha:]]{2,6}$"
if [[ "$DOMAIN" =~ $PATTERN ]]; then
DOMAIN=`echo $DOMAIN | tr '[A-Z]' '[a-z]'`
echo "Creating vhosting 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