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 =, and change 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 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/
# sudo chown -R  www-data:www-data  /var/www/
# sudo chmod -R 755 /var/www

Create an index.html file.

        <title>Welcome to!</title>
        <h2> is working fine </h2>

Creating server blocks

sudo nano /etc/nginx/sites-available/

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

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

    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/ /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 in your browser and you can see

Welcome to! 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 -d


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.