How To Install Nginx on Ubuntu 22.04

October 11, 2022 · 0 min read
How To Install Nginx on Ubuntu 22.04
One of the most widely used web servers in the world, Nginx hosts some of the biggest and busiest websites on the internet. It is a lightweight option that can be used as a web server or reverse proxy. This guide will walk you through installing Nginx on your Ubuntu 22.04 server, configuring the firewall, managing the Nginx process, and configuring server blocks to host multiple domains from a single server.

Installing Nginx

Nginx is available in Ubuntu's default repositories, so it can be installed from there using the apt packaging system.

Because this is our first interaction with the apt packaging system in this session, we'll update our local package index to get the most up-to-date package listings. Following that, we can install Nginx:

sudo apt update
sudo apt install nginx

When prompted to confirm installation, press Y. If any services are prompted to be restarted, press ENTER to accept the defaults and continue. Nginx and any required dependencies will be installed on your server by apt.

Changing the Firewall

The firewall software must be configured to allow access to the service before testing Nginx. Upon installation, Nginx registers itself as a service with ufw, making it simple to grant Nginx access.

List the application configurations with which ufw is familiar by typing:

sudo ufw app list

You should get a listing of the application profiles:

Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  OpenSSH

As demonstrated by the output, there are three profiles available for Nginx:

  • Nginx Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
  • Nginx HTTP: This profile opens only port 80 (normal, unencrypted web traffic)
  • Nginx HTTPS: This profile opens only port 443 (TLS/SSL encrypted traffic)

It is recommended that you enable the most restrictive profile that will still allow the traffic you’ve configured. Right now, we will only need to allow traffic on port 80.

You can enable this by typing:

sudo ufw allow 'Nginx HTTP'

You can verify the change by typing:

sudo ufw status

The output will indicated which HTTP traffic is allowed:

Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere                  
Nginx HTTP                 ALLOW       Anywhere                  
OpenSSH (v6)               ALLOW       Anywhere (v6)             
Nginx HTTP (v6)            ALLOW       Anywhere (v6)

Checking your Web Server

Nginx is started by Ubuntu 22.04 at the end of the installation process. The web server should already be operational.

We can check the service's status with the systemd init system by typing:

systemctl status nginx
● 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 Fri 2022-03-01 16:08:19 UTC; 3 days ago
     Docs: man:nginx(8)
 Main PID: 2369 (nginx)
    Tasks: 2 (limit: 1153)
   Memory: 3.5M
   CGroup: /system.slice/nginx.service
           ├─2369 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─2380 nginx: worker process

The service has begun successfully, as evidenced by this out. The best way to test this, however, is to actually request a page from Nginx.

Navigate to your server's IP address to access the default Nginx landing page and confirm that the software is operational. If you don't know your server's IP address, use the icanhazip.com tool to get your public IP address as received from another location on the internet:

curl -4 domain.com

When you have your server’s IP address, enter it into your browser’s address bar:

http://your_server_ip

You should receive the default Nginx landing page: