Install Nginx on Ubuntu 18.04 & 16.04 – Beginner Tutorial [5 Minutes]


Install Nginx on Ubuntu 16.04 and Ubuntu 18.04 are the same. There is not much different between this procedure.

Now, let us go through the install nginx ubuntu process.

What is Nginx?

Nginx is the Web Server similar to Apache. Nginx is more powerful and powers many high traffic website. It servers static content much faster than Apache. However both of them have their own advantage and benefits.

Nginx can be downloaded from the Ubuntu repository.

Looks like our task is more simplified.

Now, let us look at the requirement and procedures.

Install Nginx on Ubuntu


  • You just need to setup your Ubuntu server as per this ubuntu server setup guide.

Now, let us see how to configure nginx ubuntu.

1. Installing Nginx on Ubuntu 16.04

First update the Ubuntu package manager using the below command.

$ sudo apt update

Since, ubuntu maintains a lot of software in their repository, it makes us easier to download and install Nginx.

The beauty of the process, it’s all simple commands that anyone can execute perfectly.

We will use .apt package manager to install Nginx. Here is the command for that.

$ sudo apt install nginx

It will show you the packages and disk space requirement and accept and install.

Now, all the required packages and Nginx will be installed on server.

The next step in install nginx on ubuntu tutorial is to make sure firewall allows Nginx to function properly.

2. Firewall Configuration for Nginx

When you install Nginx, it will automatically register itself with UFW as a service.

So, basically Nginx is allowed to work on the server without any problem.

Now, Let us see how the list of services allowed by firewall. Use the below command for that.

$ sudo ufw app list

This sudo app list command will give the following output.

Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS

You can see three profiles for the Nginx. Let me explain what are they.

Nginx Full: This profile let’s the Nginx to allow the port 80 and port 443. Here port 80 is unencrypted and the port 443 encrypted.

Nginx HTTP: This profile only allows traffic on port 80 (unencrypted)

Nginx HTTPS: This profile allows Nginx on port 443 (Encrypted)

Now, only we are installing the Nginx on the server and we dont have any website so there is no need of SSL.

So, we only need to allow http traffic. So, just allow that profile using the below command.

$ sudo ufw allow 'Nginx HTTP'

Check the status using the below command.

$ sudo ufw status

You will get the below output.

Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere                  
Nginx HTTP                 ALLOW       Anywhere                  
OpenSSH (v6)               ALLOW       Anywhere (v6)             
Nginx HTTP (v6)            ALLOW       Anywhere (v6)

3. Test the Web Server Status

To test the web server, use the below command.

We have installed the Nginx on the server already as mentioned in the above install nginx on ubuntu procedure.

So, at this time, it should be running. use systemctl to check the status.

$ systemctl status nginx

You should get the following status as output.

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2018-05-30 12:38:15 UTC; 5 days ago
     Docs: man:nginx(8)
 Main PID: 2369 (nginx)
    Tasks: 2 (limit: 1153)
   CGroup: /system.slice/nginx.service
           ├─2369 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─2380 nginx: worker process

The active status indicates that the service is running successfully.

We can test it on another way. Just enter your server public IP, you will get the Nginx page displayed on the browser.

Enter your server IP like below.


4. Nginx Management Commands

We can start and stop the Nginx using the commands.

Now, the server is currently running. To stop the Nginx server, use the below command.

$ sudo systemctl stop nginx

To start the service again, use the below command.

$ sudo systemctl start nginx

You can restart the server using the below command.

$ sudo systemctl restart nginx

If you have made any changes on the server, just use the below command. It won’t cut the existing connections and simply update the changes.

$ sudo systemctl reload nginx

Nginx always starts at system boot everytime when you restart the server. To avoid this, just use the below command to stop Nginx from starting after system boot.

$ sudo systemctl disable nginx

If you want to start the Nginx at system boot, use the below command.

$ sudo systemctl enable nginx

5. Nginx Server Block Setup

Configure Nginx Ubuntu

The Nginx server blocks can be used to host multiple site on single server.

Yes, it is possible to do that. All you have to do is make a small change in the Nginx configuration.

We are not going in that as we focus on single domain setup. I am going to use the here.


You can use your own domain name there.

Nginx always servers the content from /var/www/html/ directory by default.

This setup is only suitable if you are hosting a single site.


We are going to create another directory for our domain It will be serve your domain content.

Also, we will leave the /var/www/html to serve if no domain is matching in the nginx configuration.

Execute the below command to create the directory.

$ sudo mkdir -p /var/www/

Here, the -p will allow us to create any parent directory if neccessary.

After that, we will assign the ownership of the directory using the $USER variable.

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

Set the permissions for web root by executing the below command.

$ sudo chmod -R 755 /var/www/

Then, you have to create the index.html page using the nano editor as mentioned below.

$ nano /var/www/

Add the below code in that file, then save and close.

        <title>Welcome to!</title>
        <h1>Success!  The server block is working!</h1>

After that, save and close file.

In order for the above content to be served, we have to create a Nginx server block at /etc/nginx/sites-available/ 

Use the nano editor to open the server block.

$ sudo nano /etc/nginx/sites-available/

Paste the below content in the file and save.

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

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


        location / {
                try_files $uri $uri/ =404;

After that, execute the below command to enable the link from sites-enabled directory. The sites-enabled file will be ready by Nginx when it starts.

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

We have configured two blocks to server requests.

  • It will serve the content for and
  • default It will serve content for the request on port 80.

We have to make a small change in the Nginx configuration file to avoid the memory hash problem. the problem arises when you add more server names.

Open the file using nano editor as mentioned below.

$ sudo nano /etc/nginx/nginx.conf

Go through the configuration  file and find the server_name_hash_bucket_size directory and uncomment the line by removing the # symbol.

http {
    server_names_hash_bucket_size 64;

Once you are done with the above steps, now check for the syntax error in the Nginx file.

$ sudo nginx -t

After that, restart the Nginx server using the below command.

$ sudo systemctl restart nginx

Now, if you go to your IP address on your browser, you should see “Success! The server block is working!

6. Nginx Files and Explanations

When you install Nginx for the first time, you will be confused with it’s file and directory names.

For any changes, you have to directly work on those files. At that time, you will not know which directory has to be checked.

Having basic knowledge about such directories and will help you understand the basic functionalities of Nginx.

  • /var/www/html: This directory contains the content which is served on internet. You can upload your website content here to served via the Nginx Server. By default, this directory will have the page which we saw earlier.
  • /etc/nginx: This is Nginx configuration directory which contains the all the configuration file for Nginx.
  • /etc/nginx/nginx.conf: this is main Nginx configuration file. Whatever the changes you make here will reflect globally.
  • /etc/nginx/sites-available/: This directory will contain the server blocks for websites. But, this server blocks will not be used unless they are linked to sites-enabled directory.
  • /etc/nginx/sites-enabled/: This directory contains the enabled server block linked from the sites-available directory.
  • /etc/nginx/snippets: Configuration fragments are contained by this directory.
  • /var/log/nginx/access.log: When you make request to the server, that will be stored here. Each and every request coming to the server is stored here.
  • /var/log/nginx/error.log: All Nginx errors are stored in this Log file.

The above ones are the important Nginx files that you should know.

Uninstall Nginx Ubuntu

If you want to uninstall Nginx on Ubuntu server, use the command below to uninstall nginx while keeping the configuration file for later use.

$ sudo apt-get remove nginx nginx-common

If you want to uninstall Nginx entirely, just use the below command.

$ sudo apt-get purge nginx nginx-common

Start Nginx Ubuntu

If you want to start the Nginx Server on Ubuntu server, use the below command.

$ sudo systemctl restart nginx



This is the basic Install Nginx on Ubuntu tutorial which is enough to configure the Nginx to serve a single website.

You can install other components such as PHP and MySQL to work with wordpress on this server.

It will be covered in future tutorial. Just, stay tuned with our news letter to get notified for the Next tutorial.


Share on facebook
Share on twitter
Share on linkedin

Leave a Comment