How to Install and Manage Nginx on Ubuntu 16.04

In this article, we shall be learning about how to install Nginx on Ubuntu 16.04 and how to allow the Nginx from the firewall. Also, we will learn about how to control the Nginx.

Nginx is a popular web server where most of the websites are hosted on the real world on the Nginx where it is more friendly than Apache and can be used as a Reverse proxy also.


In this context (demo), we will need a non-root user with sudo privileges.

Installing the Nginx on Ubuntu 16.04

As a default, the Nginx is available with default Ubuntu repositories so that we can install the Nginx directly from the command line, we can also use the apt to install the packages, but before we install the Nginx we shall update and install the Nginx.

$ sudo apt-get update
sudo apt-get update
Hit:1 xenial InRelease
Hit:2 xenial-security InRelease
Get:3 xenial-updates InRelease [95.7 kB]
Hit:4 xenial InRelease
Hit:5 xenial-backports InRelease
Get:6 xenial-updates/main amd64 Packages [37 3 kB]
Get:7 xenial-updates/main i386 Packages [368 kB]
Get:8 xenial-updates/universe amd64 Packages [319 kB]
Get:9 xenial-updates/universe i386 Packages [316 kB]
Fetched 1,471 kB in 4s (325 kB/s).
Reading package lists... Done
Now we will install the Nginx using the below command -

$ sudo apt-get install nginx
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libyaml-0-2 python-crypto python-ecdsa python-jinja2 python-markupsafe python-paramiko python-pkg-resources
python-setuptools python-six python-yaml sshpass
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed: nginx
0 upgraded, 1 newly installed, 0 to remove and 13 not upgraded.
Need to get 3,498 B of archives.
After this operation, 37.9 kB of additional disk space will be used.
Get:1 xenial-updates/main amd64 nginx all 1.10.0-0ubuntu0.16.04.2 [3,498 B]
Fetched 3,498 B in 0s (11.1 kB/s)
Selecting previously unselected package nginx.
(Reading database ... 92777 files and directories currently installed.)
Preparing to unpack .../nginx_1.10.0-0ubuntu0.16.04.2_all.deb ...
Unpacking nginx (1.10.0-0ubuntu0.16.04.2) ...
Setting up nginx (1.10.0-0ubuntu0.16.04.2) ...

Important Nginx Files and Folders

  • /var/www/html: – is the folder all the default content is located for the Nginx. /etc/nginx : The nginx configuration folder, all the Nginx configuration files is stored.
  • tc/nginx/nginx.conf : This is the main Nginx configuration or it also called as Nginx global configuration.
  • /etc/nginx/sites-available: This is the location where all the individual server blocks are stored, and Nginx will not use the configuration files until and unless they are linked to the sites-enabled folder.
  • /etc/nginx/sites-enabled/: This is the folder where enabled sites or server blocks are stored and these are created by linking the server blcks from the site-available folder.
  • /etc/nginx/snippets: This is the folder where the configuration files or fragments which are used repeatable in the configurations, which are called as snippets.
  • /var/log/nginx/access.log: All the request to the Nginx web server is recorded in this log, otherwise if we change the Nginx configuration to store in the other folder.
  • /var/log/nginx/error.log: All the error relates to the Nginx web server are stored in this log.

Adjusting the Firewall to Access Nginx

Before we access the Nginx from the web browser, we will allow the firewall the access the services from outside.

To see a list of profiles available to allow from the firewall, do the following –

$ sudo ufw app list
Available applications:
Nginx Full
Nginx HTTP
  • Nginx Full : This will allow the both ports 80 and 443.
  • Nginx HTTP : This will allow only the port 80.
  • Nginx HTTPS : This will allow only the port 443.

In this demo we allow the both HTTP 80 and HTTPS 443, below is the command to allow both 80 and 443 protocols

$ sudo ufw allow 'Nginx HTTP'
Rule added
Rule added (v6)

If we check the status of the firewall we can now see the Nginx Full is allowed from both IPv4 and IPv6 both.

$ sudo ufw status
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx Full ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx Full (v6) ALLOW Anywhere (v6)

Checking the Nginx Service Status

As a default, after the installation of the Ubuntu, it starts the Nginx and we can check the status of the Nginx using the below command –

$ 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 Sun 2016-08-21 18:50:36 IST; 11min ago
Process: 4370 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 4286 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS
Main PID: 4387 (nginx)
Tasks: 2
Memory: 8.0M
CPU: 152ms
CGroup: /system.slice/nginx.service
├─4387 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
└─4389 nginx: worker process
Aug 21 18:50:35 ubuntu16 systemd[1]: Starting A high performance web server and a reverse proxy server...
Aug 21 18:50:36 ubuntu16 systemd[1]: nginx.service: Failed to read PID from file /run/ Invalid argument
Aug 21 18:50:36 ubuntu16 systemd[1]: Started A high performance web server and a reverse proxy server.

We can also check the Nginx server running or not by using the curl command, below is the command to check the status of the server with the IP-address of the server.

$ curl -4
<!DOCTYPE html>
<title>Welcome to nginx!</title>
   body {
      width: 35em;
      margin: 0 auto;
      font-family: Tahoma, Verdana, Arial, sans-serif;
   <h1>Welcome to nginx!</h1>
   <p>If you see this page, the nginx web server is successfully installed and
   working. Further configuration is required.</p>
   <p>For online documentation and support please refer to
   <a href=""></a>.<br/>
   Commercial support is available at
   <a href=""></a>.</p>
   <p><em>Thank you for using nginx.</em></p>

We can now access the Nginx default page from any browser so that we can confirm that the Nginx is running and accessible from outside of the server.


Managing the Nginx Process Using the Commands

To stop the Nginx

$ sudo systemctl stop nginx

To start the Nginx

$ sudo systemctl start nginx

To restart the Nginx

$ sudo systemctl restart nginx

To reload the configuration without dropping the existing connections

$ sudo systemctl reload nginx

To start the Nginx at the boot time

$ sudo systemctl enable nginx

To disable or stopping the Nginx at the boot time.

$ sudo systemctl disable nginx

After this article and setup, we are able to install the Nginx web server which allows the Nginx for firewall, and manages the Nginx to start,stop, enable, disable and reload the Nginx web server.

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 23-Jan-2020


Kickstart Your Career

Get certified by completing the course

Get Started