|
| 1 | +NGINX_CONFIG='/etc/nginx/sites-available' |
| 2 | +NGINX_SITES_ENABLED='/etc/nginx/sites-enabled' |
| 3 | +WEB_DIR='/var/www' |
| 4 | +SED=`which sed` |
| 5 | +CURRENT_DIR=`dirname $0` |
| 6 | + |
| 7 | +if [ -z $1 ]; then |
| 8 | + echo "No domain name given" |
| 9 | + exit 1 |
| 10 | +fi |
| 11 | +DOMAIN=$1 |
| 12 | + |
| 13 | +# check the domain is roughly valid! |
| 14 | +PATTERN="^([[:alnum:]]([[:alnum:]\-]{0,61}[[:alnum:]])?\.)+[[:alpha:]]{2,6}$" |
| 15 | +if [[ "$DOMAIN" =~ $PATTERN ]]; then |
| 16 | + DOMAIN=`echo $DOMAIN | tr '[A-Z]' '[a-z]'` |
| 17 | + echo "Creating hosting for:" $DOMAIN |
| 18 | +else |
| 19 | + echo "invalid domain name" |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +#Replace dots with underscores |
| 24 | +SITE_DIR=`echo $DOMAIN | $SED 's/\./_/g'` |
| 25 | + |
| 26 | +# Now we need to copy the virtual host template |
| 27 | +CONFIG=$NGINX_CONFIG/$DOMAIN.conf |
| 28 | +sudo cp $CURRENT_DIR/virtual_host.template $CONFIG |
| 29 | +sudo $SED -i "s/DOMAIN/$DOMAIN/g" $CONFIG |
| 30 | +sudo $SED -i "s!ROOT!$WEB_DIR/$SITE_DIR!g" $CONFIG |
| 31 | + |
| 32 | +# set up web root |
| 33 | +sudo mkdir $WEB_DIR/$SITE_DIR |
| 34 | +sudo chown nginx:nginx -R $WEB_DIR/$SITE_DIR |
| 35 | +sudo chmod 600 $CONFIG |
| 36 | + |
| 37 | +# create symlink to enable site |
| 38 | +sudo ln -s $CONFIG $NGINX_SITES_ENABLED/$DOMAIN.conf |
| 39 | + |
| 40 | +# reload Nginx to pull in new config |
| 41 | +sudo /etc/init.d/nginx reload |
| 42 | + |
| 43 | +# put the template index.html file into the new domains web dir |
| 44 | +sudo cp $CURRENT_DIR/index.html.template $WEB_DIR/$SITE_DIR/index.html |
| 45 | +sudo $SED -i "s/SITE/$DOMAIN/g" $WEB_DIR/$SITE_DIR/index.html |
| 46 | +sudo chown nginx:nginx $WEB_DIR/$SITE_DIR/index.html |
| 47 | + |
| 48 | +echo "Site Created for $DOMAIN" |
0 commit comments