How to Install and Configure Nginx on Ubuntu 16.04


In this article, we will learn about how to install and configure the Nginx Web server. Nginx is the most popular web server where some of the most popular and high traffic sites are hosted where it is more friendly and secured than Apache and it is also used as a web server.

Prerequisites

Ubuntu 16.04 installed on the machine with a non-root user with sudo permissions on the machine.

Installing the Nginx in Machine

We can install the Nginx, which is available on Ubuntu’s default repository so can directly install the Nginx using the apt command.

Before we install the Nginx we needed to update the machine using the below command

$ sudo apt-get update
Get:1 http://security.ubuntu.com/ubuntu xenial-security InRelease [94.5 kB]
Hit:2 http://in.archive.ubuntu.com/ubuntu xenial InRelease
Get:3 http://in.archive.ubuntu.com/ubuntu xenial-updates InRelease [95.7 kB]
Hit:4 http://in.archive.ubuntu.com/ubuntu xenial-backports InRelease
Fetched 190 kB in 12s (15.5 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
19 packages can be upgraded. Run 'apt list --upgradable' to see them.

After we update the Linux we needed to 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 additional packages will be installed:
fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0
libjpeg-turbo8 libjpeg8 libtiff5 libvpx3 libxpm4 libxslt1.1 nginx-common
nginx-core
Suggested packages:
libgd-tools fcgiwrap nginx-doc ssl-cert
The following NEW packages will be installed:
fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0
libjpeg-turbo8 libjpeg8 libtiff5 libvpx3 libxpm4 libxslt1.1 nginx
nginx-common nginx-core
0 upgraded, 14 newly installed, 0 to remove and 19 not upgraded.
Need to get 2,994 kB of archives.
After this operation, 9,789 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 libjpeg-turbo8 amd6 1.4.2-0ubuntu3 [111 kB]
Get:2 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 libxpm4 amd64 1:3.511-1 [33.1 kB]
Get:3 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 libjbig0 amd64 2.1-.1 [26.6 kB]
Get:4 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 fonts-dejavu-core al 2.35-1 [1,039 kB]
Get:5 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 fontconfig-config al 2.11.94-0ubuntu1 [49.9 kB]…
…
..
Setting up libjpeg-turbo8:amd64 (1.4.2-0ubuntu3) ...
Setting up libxpm4:amd64 (1:3.5.11-1) ...
Setting up libjbig0:amd64 (2.1-3.1) ...
Setting up fonts-dejavu-core (2.35-1) ...
Setting up fontconfig-config (2.11.94-0ubuntu1) ...
Setting up libfontconfig1:amd64 (2.11.94-0ubuntu1) ...
Setting up libjpeg8:amd64 (8c-2ubuntu8) ...
Setting up libtiff5:amd64 (4.0.6-1) ...
Setting up libvpx3:amd64 (1.5.0-2ubuntu1) ...
Setting up libgd3:amd64 (2.1.1-4ubuntu0.16.04.3) ...
Setting up libxslt1.1:amd64 (1.1.28-2.1) ...
Setting up nginx-common (1.10.0-0ubuntu0.16.04.2) ...
Setting up nginx-core (1.10.0-0ubuntu0.16.04.2) ...
Setting up nginx (1.10.0-0ubuntu0.16.04.2) ...
Processing triggers for libc-bin (2.23-0ubuntu3) ...
Processing triggers for systemd (229-4ubuntu7) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for ufw (0.35-0ubuntu2) ...

Configuring the Firewall to allow Nginx

We needed to configure the firewall to allow the Nginx services, can use ufw to allow the Nginx access. We can check the list of the applications configurations that firewall knows, list the application use below command –

$ sudo ufw app list
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH

We needed to enable Nginx by using the below commands

$ sudo ufw allow 'Nginx HTTP'
Rules updated
Rules updated (v6)
root@ubuntu16:~# sudo ufw status
Status: active
To                      Action                      From
--                      ------                      ----
Nginx HTTP             ALLOW                      Anywhere
OpenSSH                ALLOW                      Anywhere
OpenSSH (v6)           ALLOW                      Anywhere (v6)
Nginx HTTP (v6)        ALLOW                      Anywhere (v6)

Checking the Nginx Web Server

We will not check the status of the Nginx Web Server using the below command

$ systemctl status
nginxnginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: e
Active: active (running) since Sun 2016-08-14 16:44:20 IST; 8min ago
Main PID: 4983 (nginx)
CGroup: /system.slice/nginx.service
├─4983 nginx: master process /usr/sbin/nginx -g daemon on; master_pr
└─4984 nginx: worker process
Aug 14 16:44:19 ubuntu16 systemd[1]: Starting A high performance web server and
Aug 14 16:44:20 ubuntu16 systemd[1]: nginx.service: Failed to read PID from fil
Aug 14 16:44:20 ubuntu16 systemd[1]: Started A high performance web server and lines 1-11/11 (END)

As we can see with the above output that Nginx service is started and running successfully, we can open the nginx on web browser with http://ip-address of the server.

Managing the Nginx Process

We can manage the Nginx Web Server using the below command

To stop the Nginx web server use below command

$ sudo systemctl stop nginx

To Start the Nginx Web Server use the below command

$ sudo systemctl start nginx

To restart the Nginx Web Server use the below command

$ sudo systemctl restart nginx

To load the configuration changes without droping the existing connections, we can use the below command

$ sudo systemctl reload nginx

To start / stop the Nginx web server at the boot time we can use the below commands

To Start

$ sudo systemctl enable nginx

To Stop

$ sudo systemctl disable nginx

Important Files and Directories of Nginx Web Server

Files Location

The default location of the Nginx Web server Document root folders is /var/www/html we can change the location by changing the configuration files.

Nginx Web Server Configuration Files

/etc/nginx : The nginx configuration folders, as all the configuration files are located at this place.
/etc/nginx/nginx.conf : This configuration file is the main files where we can change the global configuration of the Nginx Web Server.
/etc/nginx/sites-available: This folder is for to configure the per site i.e., Server Blocks and the configuration files are found in the folders unless they are linked to sites-enabled folder.
/etc/nginx/sites-enabled: This folders is used to enable the per-site i.e., Server blocks and links are created by using the files in the sites-available folder.
/etc/nignx/snippets: Ths snipest folder contains the configuration included in the Nginx, these are repeatable configurations segments on refactoring into snippets.

Logs Location of Nginx Web Server

/var/log/nignx/access.log : All the request for the Nginx Web Server are recorded in the log file, untile we change the location in the Nginx configuration file.
/var/log/nignx/error.log: Any Nginx Web Server errors are recorded in the log file.

In the above article we have learned how to configure the Nginx Web Server, how to change the configuration, location of the configuration files and how to allow the Nginx Web Werver through the ufw firewall and we have many more other options to server the technologies.

Sharon Christine
Sharon Christine

An investment in knowledge pays the best interest

Updated on: 20-Jan-2020

587 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements