How to install Nginx on Ubuntu 20.04

s2 0

Nginx, pronounced “engine x”, is an open source high performance HTTP and reverse proxy server that is responsible for handling the load of some of the largest websites on the Internet. It can be used as a standalone web server, load balancer, content cache and reverse proxy for HTTP and non-HTTP servers.

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

This tutorial describes how to install and manage Nginx on Ubuntu 20.04.

requirements

Before proceeding, make sure that you are logged in as a user with sudo privileges and that neither Apache nor any other process is running on port 80 or 443.

Install Nginx

Nginx is available in the standard Ubuntu repositories. To install it, run the following commands:

sudo apt updatesudo apt install nginx

Once the installation is complete, the Nginx service will start automatically. You can check it out by running:

sudo systemctl status nginx

The output looks something like this:

● 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 Sat 2020-05-02 20:25:43 UTC; 13s ago
...

That’s it. Nginx has been installed on your Ubuntu computer. You can manage the Nginx service like any other systemd entity.

Configure the firewall

Now that you’ve got Nginx installed and running on your server, you’ll need to make sure your firewall is configured to allow traffic over HTTP (80) and HTTPS (443) Ports. Assuming you use UFW
, you can do this by enabling the “Nginx Full” profile, which contains rules for both ports:

sudo ufw allow 'Nginx Full'

To check the 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 test your new Nginx installation, open https://YOUR_IP in your browser of your choice, and you should see the default Nginx landing page as shown in the image below:

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 make managing the Nginx configuration easier, 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 is recommended that you follow the standard naming convention. For example, if your domain name is. reads mydomain.com then your configuration file should be named /etc/nginx/sites-available/mydomain.com.conf
  • 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 the root directory of your domain document to any location. The most common locations for webroot are:
    • /home/<user_name>/<site_name>
    • /var/www/<site_name>
    • /var/www/html/<site_name>
    • /opt/<site_name>

diploma

We showed you how to install Nginx on Ubuntu 20.04. You can now start deploying your applications and using Nginx as a web or proxy server.

If you have any questions or feedback, please feel free to leave a comment.