Install and Configure Nginx on Ubuntu 22.04

Nginx is an open-source web server used to deliver both static and dynamic web content. It can act as a web server, reverse proxy, load balancer, or HTTP cache, integrating seamlessly with various applications to provide efficient web delivery via your server’s IP or domain.

This guide explains how to install Nginx on Ubuntu 22.04 and set up sample web applications for optimal performance.

Prerequisites

  • An Ubuntu 22.04 server.
  • A domain A record pointing to your server’s IP address.
  • SSH access as a non-root user with sudo privileges.
  • An updated server package index.

Install Nginx on Ubuntu 22.04

Nginx is included in Ubuntu 22.04’s default APT repositories. Follow the steps below to install and verify the web server.

Update the Package Index

Install Nginx

$ sudo apt install nginx -y

Check the Installed Nginx Version

Example output:

nginx version: nginx/1.18.0 (Ubuntu)

Manage the Nginx System Service

Nginx runs as a systemd service that manages the web server’s processes. Use the following commands to control its status.

Enable Nginx on Boot

$ sudo systemctl enable nginx

Start Nginx

$ sudo systemctl start nginx

Stop Nginx

$ sudo systemctl stop nginx

Restart Nginx

$ sudo systemctl restart nginx

Check Nginx Service Status

$ sudo systemctl status nginx

Example output:

● 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 Sun 2025-04-06 07:47:16 UTC; 1min 49s ago
       Docs: man:nginx(8)
   Main PID: 3143 (nginx)
      Tasks: 5 (limit: 9385)
     Memory: 4.8M
     CGroup: /system.slice/nginx.service
             ├─3143 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ├─3144 "nginx: worker process"
             ├─3145 "nginx: worker process"
             ├─3146 "nginx: worker process"
             └─3147 "nginx: worker process"

Create an Nginx Virtual Host

Virtual hosts define how Nginx serves specific domains or web apps. The following steps show how to create a basic configuration to host a sample website.

Create the Virtual Host File

$ sudo nano /etc/nginx/sites-available/app.example.com.conf

Add the Virtual Host Configuration

server {
    listen 80;
    listen [::]:80;
    server_name app.example.com;
    root /var/www/app.example.com;
    index index.html;
    location / {
        try_files $uri $uri/ =404;
    }
}

This configuration listens on HTTP port 80 and serves files from the web root /var/www/app.example.com.

Test the Configuration

Enable the Virtual Host

$ sudo ln -s /etc/nginx/sites-available/app.example.com.conf /etc/nginx/sites-enabled/

Create the Web Root Directory

$ sudo mkdir -p /var/www/app.example.com

Create a Sample HTML File

$ sudo nano /var/www/app.example.com/index.html

Add HTML Content


 <head></head>
<body>
<h1>Greetings from centron</h1>
</body>
</html> </pre>

Restart Nginx

$ sudo systemctl restart nginx

Test Access to Your Domain

Use curl to confirm your site is accessible:

$ curl http://app.example.com

Secure the Nginx Web Server

SSL certificates ensure encrypted communication between a user’s browser and the Nginx web server via HTTPS. By default, Nginx listens on the unencrypted HTTP port 80. Follow these steps to generate and apply trusted Let’s Encrypt SSL certificates, securing your web server.

Install Certbot

Install the Certbot Let’s Encrypt client package using Snap:

$ sudo snap install --classic certbot

Check Certbot Version

Verify that Certbot was installed successfully:

Example output:

certbot 3.3.0

Allow HTTP Traffic for Certificate Verification

Certbot uses HTTP port 80 for ACME domain validation. Allow incoming connections temporarily:

Generate the SSL Certificate

Create a new SSL certificate for your domain. Replace app.example.com with your actual domain:

$ sudo certbot --nginx -d app.example.com --agree-tos

Set Up Firewall Rules

Ubuntu 22.04 includes the Uncomplicated Firewall (UFW) by default. Configure it to permit Nginx to handle incoming HTTP and HTTPS connections.

Allow HTTPS Traffic

Open the HTTPS port (443) to allow secure web access:

Check Firewall Status

Verify that UFW rules for Nginx are active:

Example output:

Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere
80/tcp                     ALLOW       Anywhere
443/tcp                    ALLOW       Anywhere
22/tcp (v6)                ALLOW       Anywhere (v6)
80/tcp (v6)                ALLOW       Anywhere (v6)
443/tcp (v6)               ALLOW       Anywhere (v6)

Conclusion

In this tutorial, you installed and configured Nginx on Ubuntu 22.04 to host secure web applications. You also enabled HTTPS using Let’s Encrypt SSL certificates and configured UFW firewall rules. Nginx supports multiple virtual hosts for secure application deployment and integrates easily with services like MySQL and PHP to power dynamic web applications.

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: