In this guide, we’ll discuss how to configure server blocks in Nginx on an Ubuntu 16.04 server

1. Install the Nginx Web server

# sudo apt-get update
# sudo apt-get install nginx

Can enable firewall or else you can skip below step.

Enabling UFW (Ubuntu Firewall)

# sudo ufw enable
# sudo ufw allow 'Nginx Full'
# sudo ufw status

2. Install MySQL

# sudo apt-get install mysql-server

3. Install PHP 5.6

Adding PPA for PHP 5.6 in Ubuntu and installing PHP 5.6.

# sudo apt-get install python-software-properties 
# sudo add-apt-repository ppa:ondrej/php
# sudo apt-get update 
# sudo apt-get install -y php5.6

Installed php version can be found by:

# php -v

Installing php5.6 modules

# sudo apt-get install php5.6-mbstring php5.6-mcrypt php5.6-mysql php5.6-xml


4. Install PHP5.6-FPM

# sudo apt-get install php5.6-fpm

A small change is to be done in the php5-fpm configuration file  www.conf.

# sudo nano /etc/php/5.6/fpm/pool.d/www.conf

Find the line, listen = 127.0.0.1:9000, and change 127.0.0.1:9000 to /run/php/php5.6-fpm.sock .

Means...It should look like:

listen = /run/php/php5.6-fpm.sock

Save the configuration file and restart php-fpm.

# /etc/init.d/php5.6-fpm restart

5. Create Nginx server blocks

For example we are using careMonster.com as domain.

Nginx on Ubuntu 16.04 has one server block enabled by default and is configured to serve documents out of a directory at /var/www/html.

Each domain needs a directory. -p flag used with mkdir is to create necessary parent directories.

# sudo mkdir -p /var/www/careMonster.com/html
# sudo chown -R  www-data:www-data  /var/www/careMonster.com/html
# sudo chmod -R 755 /var/www

Create an index.html file.

<html>
    <head>
        <title>Welcome to careMonster.com!</title>
    </head>
    <body>
        <h2> careMonster.com is working fine </h2>
    </body>
</html>
/var/www/careMonster.com/html/index.html

Creating server blocks

sudo nano /etc/nginx/sites-available/careMonster.com

server {
    listen 80 ;
    listen [::]:80 ;

    root /var/www/careMonster.com/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name careMonster.com www.careMonster.com;
    location / {
        try_files $uri $uri/ =404;
    }
    location ~ \.php$ {
           try_files $uri =404;
           fastcgi_pass unix:/run/php/php5.6-fpm.sock;
           fastcgi_index index.php;
           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
           include fastcgi_params;
                
     }

}

Enabling server block

# sudo ln -s /etc/nginx/sites-available/careMonster.com /etc/nginx/sites-enabled/
# sudo nano /etc/nginx/nginx.conf

Within the file, find the server_names_hash_bucket_size directive. Remove the # symbol to uncomment the line:

server_names_hash_bucket_size 64;

Then save and exit.

For testing any syntax errors in Nginx files:

# sudo nginx -t

If no problems were found, you can restart Nginx to enable your changes:

# sudo systemctl restart nginx

Testing Result

Browse the site careMonster.com in your browser and you can see

Welcome to careMonster.com!

careMonster.com is working fine

Configuring ssl with nginx

Certbot is an easy-to-use client that fetches a certificate from Let's Encrypt—an open certificate authority launched by the EFF, Mozilla, and others—and deploys it to a web server.

Adding repository

# sudo add-apt-repository ppa:certbot/certbot
# sudo apt-get update
# sudo apt-get install python-certbot-nginx
# sudo certbot --nginx -d careMonster.com -d www.careMonster.com

Conclusion

Congratulations!....You are now able to create server block for your domain. Several blocks can be created in a server so long as your hardware can handle the traffic.