Magazine

How to Install Nginx Server on Ubuntu 16.10

Posted on the 27 June 2020 by Satish Kumar @satish_kumar86

Nginx is the most potent, open-source, and a high-performance Web server. It can work as a reverse proxy server also, nowadays, is used by most of the most significant websites on the internet.

People pronounced “engine x” for Nginx; it is the hot choice for every website owner to power their site with Nginx.

In comparison to the Apache web server, Nginx is capable of handling more connections with a few amount of memory footprint in each connection.

Prerequisites

Make sure your Linux box does not have an Apache HTTP server or any application service running on port 80 and 443. You should have sudo privileges to execute commands on your Linux machine.

Install Nginx

Nowadays, Nginx software packages built-in Ubuntu default software repository, so the installation is effortless, You just run the following commands in terminal:
IyBzdWRvIGFwdCB1cGRhdGUKIyBzdWRvIGFwdCBpbnN0YWxsIG5naW54
After installation of Nginx, you can check the status of the Nginx service by using the following command:
IyBzdWRvIHN5c3RlbWN0bCBzdGF0dXMgbmdpbng=
The output of the above command should show that the Nginx service is running: Output:
bmdpbnguc2VydmljZSAtIEEgaGlnaCBwZXJmb3JtYW5jZSB3ZWIgc2VydmVyIGFuZCBhIHJldmVyc2UgcHJveHkgc2VydmVyCiAgIExvYWRlZDogbG9hZGVkICgvbGliL3N5c3RlbWQvc3lzdGVtL25naW54LnNlcnZpY2U7IGVuYWJsZWQ7IHZlbmRvciBwcmVzZXQ6IGVuYWJsZWQpCiAgIEFjdGl2ZTogYWN0aXZlIChydW5uaW5nKSBzaW5jZSBTdW4gMjAyMC0yMi0wNiAxNTo0NDowNCBVVEM7IDFtaW4gNTlzIGFnbwogTWFpbiBQSUQ6IDE0NjEgKG5naW54KQogICBDR3JvdXA6IC9zeXN0ZW0uc2xpY2Uvbmdpbnguc2VydmljZQogICAgICAgICAgIOKUnOKUgDE0NjEgbmdpbng6IG1hc3RlciBwcm9jZXNzIC91c3Ivc2Jpbi9uZ2lueCAtZyBkYWVtb24gb247IG1hc3Rlcl9wcm9jZXNzIG9uCiAgICAgICAgICAg4pSU4pSAMTQ2MyBuZ2lueDogd29ya2VyIHByb2Nlc3M=
You can also check the version of Nginx Web server, using the following command:
IyBzdWRvIG5naW54IC12
Output:
bmdpbnggdmVyc2lvbjogbmdpbngvMS4xNC4wIChVYnVudHUp

Firewall Configuration

Today, we all are using the UFW firewall to manage network connection and traffic on the Ubuntu machine. To use Nginx, you will need to open HTTP Port (80) and HTTPS port (443).

You can open HTTP and HTTPS port by enabling “Nginx Full” profile on UFW:

IyBzdWRvIHVmdyBhbGxvdyAnTmdpbnggRnVsbCc=
You can verify the firewall configuration using below command:
IyBzdWRvIHVmdyBzdGF0dXM=
The output of the above command is something like below:
U3RhdHVzOiBhY3RpdmUKClRvICAgICAgICAgICAgICAgICAgICAgICAgIEFjdGlvbiAgICAgIEZyb20KLS0gICAgICAgICAgICAgICAgICAgICAgICAgLS0tLS0tICAgICAgLS0tLQoyMi90Y3AgICAgICAgICAgICAgICAgICAgICBBTExPVyAgICAgICBBbnl3aGVyZQpOZ2lueCBGdWxsICAgICAgICAgICAgICAgICBBTExPVyAgICAgICBBbnl3aGVyZQoyMi90Y3AgKHY2KSAgICAgICAgICAgICAgICBBTExPVyAgICAgICBBbnl3aGVyZSAodjYpCk5naW54IEZ1bGwgKHY2KSAgICAgICAgICAgIEFMTE9XICAgICAgIEFueXdoZXJlICh2Nik=

Nginx Installation Test

You can verify that installed Nginx is working as expected by opening in your browser with your IP (http://your-IP). You will get the browser screen with default Nginx welcome page, as shown below:
Nginx on Browser

Install Nginx using Nginx PPA Repository

The Ubuntu default software repository is not updating packages regularly, so it often outdated. To install Nginx’s latest version, use Nginx’s official PPA repository.

To install Nginx using PPA repository in Ubuntu 16.10, follow below steps:

01. install “software-properties-common” in Ubuntu system
IyBzdWRvIGFwdCBpbnN0YWxsIHNvZnR3YXJlLXByb3BlcnRpZXMtY29tbW9u
02. Add Nginx’s PPA repository using following command
IyBzdWRvIGFkZC1hcHQtcmVwb3NpdG9yeSBwcGE6bmdpbngvc3RhYmxl
03. Update package manager with list and install Nginx using following command:
IyBzdWRvIGFwdCB1cGRhdGUKIyBzdWRvIGFwdCBpbnN0YWxsIG5naW54
04. After completing the installation of Nginx, check the version of installed Nginx
IyBzdWRvIG5naW54IC12
Output:
bmdpbnggdmVyc2lvbjogbmdpbngvMS4xNy4w

Manage Nginx service with systemctl

You can manage Nginx services with the similar command what use to manage other system services. Start the Nginx Service:
IyBzdWRvIHN5c3RlbWN0bCBzdGFydCBuZ2lueA==
Stop the Nginx Service:
IyBzdWRvIHN5c3RlbWN0bCBzdG9wIG5naW54
Restart the Nginx Service:
IyBzdWRvIHN5c3RlbWN0bCByZXN0YXJ0IG5naW54
Reload the Nginx Service:
IyBzdWRvIHN5c3RlbWN0bCByZWxvYWQgbmdpbng=
Enable the Nginx Service to start at boot:
IyBzdWRvIHN5c3RlbWN0bCBlbmFibGUgbmdpbng=
Disable the Nginx Service to not start on boot:
IyBzdWRvIHN5c3RlbWN0bCBkaXNhYmxlIG5naW54

Nginx configuration file’s structure on Ubuntu

The Nginx configuration files will always remain in the “/etc/nginx/” directory.

The main Nginx’s setting files located at “/etc/nginx/nginx.conf.”

To keep Nginx configuration is simple by configuring separate files for each site. You can keep as much as you want with a configuration file with a server block.

Nginx server block files or site configuration files stored in the “/etc/nginx/sites-available/” directory. To make these files in use on Nginx, link the files in the “/etc/nginx/sites-enable/” directory.

To activate any new site configuration, we need to create a symlink of site configuration file available in the “sites-available” directory to the “sites-enabled” directory.

To identify the site’s configuration, follow the standard naming conversion for server block files. For example, you have a site testweb.com. It is better to create a file as “/etc/nginx/sites-available/testweb.com.conf” to identify quickly when you have multiple sites configured in the Nginx web server.

The most important file to troubleshoot or debug error is called log files. The Nginx log files (access.log and error.log) generated in the “/var/log/nginx” directory. It is useful for debugging if different access and error log files are for each server block.

There is no boundry to configure domain document root directory, you can set any location you want. But the most recommended location for web root directory are:

  • /home/<user>/<site-name>
  • /var/www/<site-name>
  • /var/www/html/<site-name>
  • /opt/<site-name>

Conclusion

Congratulation; now you have installed the Nginx Web server on your Ubuntu 16.04 server. Now, you are ready to deploy and run your application using Nginx as a web server or reverse proxy server.

Back to Featured Articles on Logo Paperblog