How To Set Up Nginx Server Blocks on Ubuntu 20.04

July 23, 2020






Nginx Server Block is use to configure and run more than one website or web application on a single server/system.

The Nginx Server Block helps us configure web document root (web directory to store website files), use different SSL certificates for each site, and create different or separate security policies.

This tutorial will show how to create an Nginx server block (like apache virtual host) in simple steps on Ubuntu 20.04 Server.


Before starting the tutorial, make sure you have the following prerequisites:

Having a domain name pointing to the server’s public IP. We will use

You have to install the Nginx server in your Ubuntu machine, if not pre-installed using this guide.

You should log in to the server as a user with sudo privileges.

Create the Directory Structure

The document root is a directory on the server where we store the website files and access the domain name requests as a response. You can configure the document root directory at any location you want on the server.

We will use the following directory structure:

│   └── public_html
│   └── public_html
│   └── public_html

We are always creating a separate directory in the directory “/var/www/” on our server to host each domain. Within each of these directories, we will create a directory called “public” to store the domain website files.

Now we create the root directory for our domain

$ sudo mkdir -p /var/www/

For testing the site root directory is working, we will create an “index.html” file inside the domain’s document root directory.

$ sudo vim /var/www/
<!DOCTYPE html>
<html lang="en" dir="ltr">
    <meta charset="utf-8">
    <title>Welcome to</title>
    <h1>Success! home page!</h1>

To avoid any permission issue, as Nginx will run by the user “www-data,” we change the ownership of the domain’s document root directory to the Nginx user (www-data):

$ sudo chown -R nginx: /var/www/

Create a Server Block

In the Ubuntu system, by default, we store the Nginx server block files in the directory “/etc/nginx/sites-available/.” These server blocks are getting enabled by creating a symbolic link to the directory “/etc/nginx/sites-enabled/.”

Now create a server block file for with the below configuration:

$ sudo vim /etc/nginx/conf.d/
server {
    listen 80;
    root /var/www/;
    index index.html;
    access_log /var/log/nginx/;
    error_log /var/log/nginx/;

You can give any name for the server block file, but we give the name of the domain for identification and best use.

You can now enable this newly configured server block by creating a symbolic link file in the directory “sites-enabled” as shown below:

$ sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

You can test the Nginx configuration for correct syntax using the following command:

$ sudo nginx -t

If there is no error, the output will look like below:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Now, restart the Nginx service for configured changes to take effect:

$ sudo systemctl restart nginx

To verify the server block working, open your site in the browser (, and you will get the screen like below on your browser.


Now, you have learned to create an Nginx server block for hosted websites on Ubuntu 20.04, also learned to host multiple domains in a single Nginx instance and on a single Ubuntu server. You can create as much as you can server blocks for hosting multiple domains by repeating the above steps.

If you have any doubt, problem, facing any issue in Nginx configuration or feedback, please leave a comment below.

Satish Kumar

Satish Kumar

I am Satish Kumar, Founder of LinuxConcept. Linux and F.O.S.S enthusiast, love to work on open source platform and technologies.










Related Posts

Delete a user account from Ubuntu Desktop or Server

If you no longer need a user account, it is good idea to delete that account.Getting ready You will need super user or root privileges to delete a group from the Ubuntu server.How to do it... Follow these steps to delete the user account:Enter the following command to...

Creating a group on Ubuntu Desktop or Server

Group is a way to organize and administer user accounts in Linux. Groups are used to collectively assign rights and permissions to multiple user accounts.Getting ready You will need super user or root privileges to add a group to the Ubuntu server.How to do it......

Creating a user account on Ubuntu desktop or server

Suppose you have already installed the Ubuntu desktop or server. In that case, it means you have already created a default user on your system, even on the cloud also Ubuntu is installed with preconfigured default user account. You can do all administrative tasks...



Submit a Comment

Your email address will not be published. Required fields are marked *

one × four =

News & Updates

Join Our Newsletter