How to Install Nginx on Ubuntu 22.04?

install nginx on ubuntu 20.04 3

Nginx commonly pronounced as “Engine X” is an open-source, high-performance HTTP and reverse proxy server responsible for handling the load of some of the largest sites on the Internet.

It is generally used as standalone web servers, load balancers, content caches, and reverse proxy for HTTP and non-HTTP servers.

Nginx is able to handle a much large number of concurrent connections and has a smaller memory footprint per connection, as compared to Apache.

In this tutorial, you’ll learn to install and manage Nginx on Ubuntu 20.04.

#1 Prerequisites

  • Make sure you are logged in as a root user or a user with sudo privileges.
  • Make sure you don’t Apache or any other process already installed running on port 80 or 443.

#2 Installing Nginx

Nginx is already available in the default Ubuntu repositories and it is very easy to install it on the server. To install Nginx run the following commands:

sudo apt update
sudo 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 server. Now, you can manage the Nginx service in the same way as any other system unit.

#3 Configuring Nginx Firewall Settings

As you have already had Nginx installed and running on your server, now you must make sure your firewall is established correctly to allow traffic on HTTP (80) and HTTPS (443) ports. Assuming you are using UFW command-line, 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)

#4 Testing the Nginx 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:

install nginx on ubuntu 20.04 5

#5 Some Nginx Configuration File’s Structure and Great 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.
  • Always follow the standard convention.
    • Example: If your domain name is example.com then your configuration file should be named /etc/nginx/sites-available/example.com.conf
  • The /etc/nginx/snippets directory contains configurations that can be included in the server block files. If you use a repeatable configuration, then you can refactor those configuration 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>

# Conclusion

You’ve learned to install Nginx on Ubuntu 20.04 from the guide. You can now start deploying your applications and use Nginx as a web or proxy server.

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

Scroll to Top
0 Shares
Tweet
Share
Share
Pin