How to Install Nginx on Ubuntu 20.04

0 comments 6.7K views 3 mins read
Published on: February 1, 2023 | Last updated on: October 11, 2024

Learn How to Install Nginx on Ubuntu 20.04 with the help of this tutorial.

Some of the busiest websites on the Internet rely on Nginx, an open-source, high-performance HTTP and reverse proxy server pronounced “engine x.” Besides serving as a reverse proxy for HTTP and non-HTTP servers, it can also act as a load balancer, content cache, and standalone web server.

Nginx has a smaller memory footprint per connection and can handle a much larger number of simultaneous connections than Apache.

Prerequisites

  • Access to a Sudo User
  • Ubuntu server 18.04 or 20.04 or 22.04

Installing Nginx

In Ubuntu, Nginx can be found in the official packages. The following commands must be entered in order to set it up:

sudo apt update
sudo apt install nginx

After setup is complete, the Nginx service will immediately begin running. It can be checked by using:

sudo systemctl status nginx

The final result might look 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
...

You should now see Nginx in your Ubuntu server’s list of programs. The Nginx service can be controlled just like any other systemd component.

Configuring firewall

Once Nginx has been installed and is running, the firewall must be set up to permit traffic on the HTTP (80) and HTTPS (443) ports. Using UFW, you can do so by activating the “Nginx Full” profile, which includes the necessary rules for both ports:

sudo ufw allow 'Nginx Full'

In order to check the current status, enter:

sudo ufw status

You can expect something like this from the output:

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

Open “http://YOUR IP” in your preferred browser to check out your new Nginx installation, and you should see the default Nginx landing page, as depicted in the image below:

Nginx Configuration File’s Structure and Best Practices

The settings for Nginx are all in the /etc/nginx folder.

The main file for configuring Nginx is /etc/nginx/nginx.conf.

If you want to make it easier to manage your Nginx configuration, you should make a separate file for each domain. You can use as many server block files as you need.

The block files for a Nginx server are in the /etc/nginx/sites-available folder. If the files in this directory aren’t also linked to /etc/nginx/sites-enabled, Nginx won’t use them for configuration.

A symlink must be used to move the configuration file sites from the sites-available directory to the sites-enabled directory (a pointer).

It’s best to use standard names when naming things. In this case, /etc/nginx/sites-available/mydomain.com.conf would be the location of the configuration file for the mydomain.com domain.

In the /etc/nginx/snippets directory, you can find a list of configuration snippets that can be added to the server block files. You can turn configuration chunks that you use often into snippets and then add the snippet file to the server blocks.

Nginx log files (access.log and error.log) are located in the /var/log/nginx directory. For each server partition, it’s best to keep a separate set of access and error log files.

The “document root” of a domain can be a folder anywhere you like. Webroot is most often found in these places:

/home/<user_name>/<site_name>
/var/www/<site_name>
/var/www/html/<site_name>
/opt/<site_name>

Conclusion

We’ve demonstrated how to set up Nginx on Ubuntu 20.04. Nginx is now ready for use as a web or proxy server, and you can begin deploying your applications.

We welcome your thoughts and questions in the form of comments.