How to install Nginx on Ubuntu 16.04


Nginx is one of the most popular web servers in the world and is responsible for hosting some of the largest and most visited sites on the Internet. In most cases it is more resource intensive than Apache and can be used as a web server or reverse proxy.

In this guide, we will discuss how to install Nginx on your Ubuntu 16.04 server.


Before you start this tutorial, you must have a regular non-root user with privileges sudo configured on your server. You can learn how to set up a regular user account by following our initial server setup guide for Ubuntu 16.04.

Once you have access to the account, log in as a non-root user.

Step 1. Install Nginx

Nginx is available in the default Ubuntu repositories, so installation is pretty straightforward.

Since this is our first interaction with the package system apt in this session, we will update our local package index to have access to the latest package lists. After that we can set nginx :

                      sudo apt-get update
sudo apt-get install nginx

After accepting the procedure, apt-get will install Nginx and all required dependencies to your server.

Step 2. Adjust the firewall

Before we can test Nginx, we need to reconfigure our firewall software to provide access to the service. Nginx registers as a service with ufw with our firewall when installed. This makes it easier to access Nginx.

We can list application configurations that ufw knows by typing:

                      sudo ufw app list

You should get a list of application profiles:


                        Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS

As you can see, there are three profiles available for Nginx:

  • Nginx full : this profile opens port 80 (regular, unencrypted web traffic) and port 443 (TLS / SSL encrypted traffic)
  • Nginx HTTP : this profile only opens port 80 (normal, unencrypted web traffic)
  • Nginx HTTPS : this profile only opens port 443 (TLS / SSL encrypted traffic)

We recommend that you enable the most restrictive profile that will still allow configured traffic. Since we have not yet configured SSL for our server, in this tutorial we will only need to allow traffic on port 80.

You can enable this by typing:

                      sudo ufw allow 'Nginx HTTP'

You can check the changes by typing:

                      sudo ufw status

You should see HTTP traffic allowed in the displayed output:


                        Status: active

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

Step 3. Check the web server

At the end of the installation process, Ubuntu 16.04 starts Nginx. The web server must be running.

We can check systemd systems systemd init to make sure the service is running by typing:

                      systemctl status nginx


                        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 Mon 2017-05-09 11:23:12 EDT; 1min 1s ago
 Main PID: 18421 (nginx)
   CGroup: /system.slice/nginx.service
           +-18421 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
           L-18422 nginx: worker process

As you can see above, the service appears to have started successfully. However, the best way to test this is to actually request the page from Nginx.

You can access the default Nginx landing page to make sure the software is working correctly. You can access it via your domain name or your server’s IP address.

If you don’t want to configure a domain name for your server, you can use your server’s public IP address. If you don’t know the IP address of your server, there are several ways you can get it from the command line.

Try typing this on your server command line:

                      ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's//.*$//'

You will get multiple lines. You can try each one in your web browser to see if they work.

An alternative is typing, which should give you your public IP address, as seen from elsewhere on the internet:

                          sudo apt-get install curl
    curl -4


Once you have your server’s IP address or domain, enter it into your browser’s address bar:


You should see the default Nginx landing page, which should look something like this:

This page is simply included with Nginx to show you that the server is working properly.

Step 4. Managing the Nginx Process

Now that your web server is up and running, we can move on to some rudimentary control commands.

To stop the web server, you can enter:

                      sudo systemctl stop nginx

To start the web server when it is stopped, enter:

                      sudo systemctl start nginx

To stop and start the service, enter:

                      sudo systemctl restart nginx

If you’re just making configuration changes, Nginx can reload frequently without dropping connections. To do this, you can use the following command:

                      sudo systemctl reload nginx

By default, Nginx is configured to start automatically on server boot. If that’s not what you want, you can turn it off by typing:

                      sudo systemctl disable nginx

To re-enable the service to start at system boot, you can enter:

                      sudo systemctl enable nginx

Step 5. Introducing Important Nginx Files and Directories

Now that you know how to manage the service itself, you should take a few minutes to familiarize yourself with a few important directories and files.


  • /var/www/html … The actual web content, which by default only consists of the default Nginx page you saw earlier, is served from /var/www/html … This can be changed by modifying the Nginx config files.

Server configuration

  • /etc/nginx Etc /etc/nginx : Nginx config directory. All Nginx config files are located here.
  • /etc/nginx/nginx.conf : Main configuration file for Nginx. It can be modified to make changes to the global Nginx configuration.
  • /etc/nginx/sites-available : a directory that can store the “server blocks” for each site. Nginx will not use configuration files found in this directory unless they are linked to the included sites directory (see below). Typically, all server block configuration is done in this directory and then enabled by linking to another directory.
  • /etc/nginx/sites-enabled/ Etc /etc/nginx/sites-enabled/ : the directory that stores the allowed “server blocks” for each site. Typically, they are created by linking to configuration files located in a directory accessible to sites-available
  • /etc/nginx/snippets Etc /etc/nginx/snippets … This directory contains configuration snippets that can be included in the Nginx configuration. Potentially repeatable configuration segments are good candidates for refactoring into fragments.

Server logs

  • /var/log/nginx/access.log : every request to your web server is saved to a log file /var/log/nginx/access.log unless Nginx is configured otherwise.
  • /var/log/nginx/error.log : Any Nginx errors will be logged /var/log/nginx/error.log


Now that you have a web server installed, you have many options for the type of content and technology you want to use to create a richer experience.

Learn how to use Nginx server blocks. If you want to build a more complete application stack, check out this article on How to Set Up a LEMP Stack on Ubuntu 16.04.

Related Posts