Install Nginx on Ubuntu 20.04

2 min read

Prerequisites

Before continuing, make sure you are logged in as a user with sudo privileges, and you don’t have Apache or any other process running on port 80 or 443.

Installing Nginx

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

sudo apt updatesudo apt install nginx

Once the installation is completed, the Nginx service will start automatically. You can verify it by running:

sudo systemctl status nginx

The output will look 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 machine. You can manage the Nginx service in the same way as any other systemd unit.

Configuring firewall

Now that you have Nginx installed and running on your server, you need to make sure your firewall is configured to allow traffic on HTTP (80) and HTTPS (443) ports. Assuming you are using UFW, you can do that by enabling the ‘Nginx Full’ profile which includes rules for both ports:

sudo ufw allow 'Nginx Full'

To verify the status type:

sudo ufw status

The output will look something like the following:

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 http://YOUR_IP in your browser of choice, and you should see the default Nginx landing page as shown on the image below:

Nginx Configuration File’s Structure and Best Practices

  • All Nginx configuration files are located in the /etc/nginx directory.
  • The main Nginx configuration file is /etc/nginx/nginx.conf.
  • To make Nginx configuration easier to maintain, 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 are not used by Nginx unless they are linked to the /etc/nginx/sites-enabled directory.
  • To activate a server block, you need to create a symlink (a pointer) from the configuration file sites in a sites-available directory to the sites-enabled directory.
  • It is recommended to follow the standard naming convention. For example, if your domain name is 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 use repeatable configuration segments, then you can refactor those segments into snippets and include the snippet file to the server blocks.
  • Nginx log files (access.log and error.log) are located in the /var/log/nginx directory. It is recommended to have a different access and error log files for each server block.
  • You can set your domain document root directory to any location you want. The most common locations for webroot include:
    • /home/<user_name>/<site_name>
    • /var/www/<site_name>
    • /var/www/html/<site_name>
    • /opt/<site_name>


We want to hear your thoughts about this. Is there anything in particular that you love about it? What can we do to improve our services and experience? Leave a comment below or open a ticket on our helpdesk and we’ll personally review all suggestions and feedback. 

About Servercheap.NET

Since it was founded in 2015, Servercheap has always strived to provide its clients with enterprise-level performance at an unbeatable cost. Servercheap offers a wide range of customizable hybrid and virtual private server hosting services. All Servercheap clients enjoy a 99.9% uptime SLA and 24/7 rapid response support team.
At Servercheap, our core directive has always been to provide our clients with the best services and infrastructure possible, whether you’re hosting a game server, a high-intensity database, a development environment, or anything in-between.
For more information, visit https://www.servercheap.net

Leave a Reply

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