Friday, 6 October 2017

Automatically creating new virtual hosts for Nginx using Bash script

Setting up virtual hosts on any sort of web server normally takes at least a few minutes and several commands (if you’re not running a control panel of some variety, and even then it normally takes a good number of clicks and checking boxes to get what you want). All of this can be quite annoying when you have to set up several of these a day.

So I put together a simple bash script to quickly provision the hosting for a new static site running on Nginx


What does the Script do:

  • Creates a new vhosts entry for nginx using a basic template.
  • Creates a new directory for the new vhost and sets www-data as the owner
  • Creates the following directory: Backup, Logs, Private, Public. In which public is your
    Document Root.
  • Reloads Nginx to allow the new vhost to be picked up.

How to use it:

Create a file from terminal: 

# vim nginxvhostsetup.sh
Inside the file paste the following script:

#!/bin/bash

# Info
# ---
# script can run with the domain as a command line input
# `sudo ./nginx_domain.sh my_domain.com` or without and
# the script will prompt the user for input

#config
web_root='/var/www/'
config_dir='/etc/nginx/'

if [ -z "$1" ]
then

        #user input
        echo -e "Enter domain name:"
        read DOMAIN
        echo "Creating Nginx domain settings for: $DOMAIN"

        if [ -z "$DOMAIN" ]
        then
                echo "Domain required"
                exit 1
        fi
fi

if [ -z "$DOMAIN" ]
then
        DOMAIN=$1
fi

(
cat <<EOF
server {
        listen   80; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default_server ipv6only=on; ## listen for ipv6

        root $web_root/$DOMAIN/public;
        index index.php  index.html index.htm;

        # Make site accessible from http://localhost/
        server_name $DOMAIN www.$DOMAIN;

        location / {try_files $uri $uri/ @forum;}

        location ~ \.php$ {
                try_files $uri =404;

                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }
        location ~ /\.ht {
                deny all;
        }
            access_log $web_root/$DOMAIN/log/access_log.txt;
            error_log $web_root/$DOMAIN/log/error_log.txt error;
}
EOF
) >  $config_dir/sites-available/$DOMAIN.conf

echo "Making web directories"
mkdir -p $web_root/"$DOMAIN"
mkdir -p $web_root/"$DOMAIN"/{public,private,log,backup}
ln -s $config_dir/sites-available/"$DOMAIN".conf $config_dir/sites-enabled/"$DOMAIN".conf
/etc/init.d/nginx reload
echo "Nginx - reload"
chown -R www-data:www-data $web_root/"$DOMAIN"
chmod 755 $web_root/"$DOMAIN"/public
echo "Permissions have been set"
echo "$DOMAIN has been setup"

Once you have created the script just run the nginxvhostsetup.sh using sudo  :

# sudo ./nginxvhostsetup.sh
And you should see an output similar to the following:

Enter domain name:
example.com
Creating Nginx domain settings for: example.com
Making web directories
Nginx - reload
Permissions have been set
example.com has been setup
Voila!! It's done. 

The vhost for your new domain is created. Copy your files to public folder in /var/www/domain_name.com/ . and your website is up and running

    No comments:

    Post a Comment