How to Install Nginx on Ubuntu 20.04 Server

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:

# sudo apt update
# sudo apt install nginx

After installation of Nginx, you can check the status of the Nginx service by using the following command:

# sudo systemctl status nginx

The output of the above command should show that the Nginx service is 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 2020-22-06 15:44:04 UTC; 1min 59s ago
 Main PID: 1461 (nginx)
   CGroup: /system.slice/nginx.service
           ├─1461 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
           └─1463 nginx: worker process

You can also check the version of Nginx Web server, using the following command:

# sudo nginx -v
nginx version: nginx/1.14.0 (Ubuntu)

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:

# sudo ufw allow 'Nginx Full'

You can verify the firewall configuration using below command:

# sudo ufw status

The output of the above command is something like below:

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)

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:

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 20.04, follow below steps:

01. install “software-properties-common” in Ubuntu system
# sudo apt install software-properties-common

02. Add Nginx’s PPA repository using following command

# sudo add-apt-repository ppa:nginx/stable

03. Update package manager with list and install Nginx using following command:

# sudo apt update
# sudo apt install nginx

04. After completing the installation of Nginx, check the version of installed Nginx

# sudo nginx -v
nginx version: nginx/1.17.0

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:

# sudo systemctl start nginx

Stop the Nginx Service:

# sudo systemctl stop nginx

Restart the Nginx Service:

# sudo systemctl restart nginx

Reload the Nginx Service:

# sudo systemctl reload nginx

Enable the Nginx Service to start at boot:

# sudo systemctl enable nginx

Disable the Nginx Service to not start on boot:

# sudo systemctl disable nginx

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 20.04 server. Now, you are ready to deploy and run your application using Nginx as a web server or reverse proxy server.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Related Articles