How to install Nginx on Ubuntu 16.04

s2 0

Nginx, pronounced “engine x”, is a free, open source, high performance HTTP and reverse proxy server that powers some of the largest websites on the Internet.

Compared to Apache, Nginx can handle a much larger number of simultaneous connections and has a lower memory footprint per connection.

In this tutorial, we will discuss how to install and manage Nginx on Ubuntu 16.04 systems.


Before starting the tutorial, make sure that you are logged in as a user with sudo privileges and that no Apache or any other service is running on port 80 or 443.

Install Nginx

Nginx packages are available in standard Ubuntu software repositories. Installation is pretty straightforward, just run the following commands:

sudo apt updatesudo apt install nginx

After the installation is complete, check the status of the Nginx service by typing:

sudo systemctl status nginx

The output should show you that the Nginx service is up and running:

● 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 Sun 2018-01-05 15:44:04 UTC; 1min 59s ago
 Main PID: 1291 (nginx)
   CGroup: /system.slice/nginx.service
           ├─1291 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
           └─1293 nginx: worker process

How to check the Nginx version type:

sudo nginx -v
nginx version: nginx/1.10.3 (Ubuntu)

Adjust the firewall

Assuming you use UFW
To manage your firewall you need to use HTTP (80) and HTTPS (443) Ports.

You can do this by enabling the “Nginx Full” profile, which contains rules for both ports:

sudo ufw allow 'Nginx Full'

To check the firewall status type:

sudo ufw status

The output looks something like this:

Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere
Nginx Full                 ALLOW       Anywhere
22/tcp (v6)                ALLOW       Anywhere (v6)
Nginx Full (v6)            ALLOW       Anywhere (v6)

Test the installation

To check that Nginx is working as expected, open https://YOUR_IP in your browser of choice, and you will get the standard Nginx welcome page as shown below:

Install Nginx from the Nginx PPA repository

The Nginx packages from the Ubuntu repositories are often out of date. To install the latest Nginx version, use the official Nginx PPA repository.

To install the latest version of Nginx on Ubuntu 16.04, do the following:

  1. First install the software-properties-common Package:

    sudo apt install software-properties-common
  2. Add the Nginx PPA repository with the following command:

    sudo add-apt-repository ppa:nginx/stable
  3. Update the package list and install Nginx:

    sudo apt updatesudo apt install nginx
  4. After the installation is complete, check the Nginx version with:

    sudo nginx -v

    The output looks something like this:

    nginx version: nginx/1.12.2

Manage the Nginx service with systemctl

You can manage the Nginx service like any other systemd entity.

  • To stop the Nginx service, do the following:

    sudo systemctl stop nginx
  • To start the Nginx service, type:

    sudo systemctl start nginx
  • Restart the Nginx service:

    sudo systemctl restart nginx
  • After making some configuration changes, reload the Nginx service:

    sudo systemctl reload nginx
  • Disable the Nginx service to start at boot:

    sudo systemctl disable nginx
  • Re-enable the Nginx service to restart at boot:

    sudo systemctl enable nginx

Structure and best practices of the Nginx configuration file

  • All Nginx configuration files are in the /etc/nginx/ Directory.
  • The main configuration file of Nginx is /etc/nginx/nginx.conf.
  • To simplify the maintenance of the Nginx configuration, it is recommended to create a separate configuration file for each domain. You can have as many server block files as you need.
  • Nginx server block files are stored in /etc/nginx/sites-available Directory. The configuration files found in this directory will not be used by Nginx unless they are associated with the /etc/nginx/sites-enabled Directory.

  • To activate a server block, you need to get a symlink (pointer) from the configuration file sites in a. create sites-available Directory for sites-enabled Directory.
  • It’s a good idea to follow a standard naming convention, for example if your domain name then your configuration file should be named /etc/nginx/sites-available/
  • the /etc/nginx/snippets -Directory contains configuration snippets that can be included in the server block files. If you are using repeatable configuration segments, you can convert these segments into snippets and include the snippet file in the server blocks.
  • Nginx log files (access.log and error.log) are located in the /var/log/nginx/ Directory. It is recommended to have another one access and error Log files for each server block.
  • You can set your domain document root directory to any location. The most common locations for Webroot include:
    • /home/<user_name>/<site_name>
    • /var/www/<site_name>
    • /var/www/html/<site_name>
    • /opt/<site_name>


Congratulations, you have successfully installed Nginx on your Ubuntu 18.04 server. You can now start deploying your applications and using Nginx as a web or proxy server.

A secure certificate is a must for all websites these days. To secure your website with a free Let’s Encrypt SSL certificate, you can read the guide below:

Secure Nginx with Let’s Encrypt on Ubuntu 16.04