Skip to content

How to Install Nginx on Ubuntu 26.04

Install Nginx on Ubuntu 26.04, configure UFW, set up a server block, add free Let's Encrypt HTTPS, or install the latest version from nginx.org.

Steps
6
Category
Web Servers
Covers
Firewall, Let's Encrypt, Nginx
On this page 6 steps

Nginx (pronounced “engine-x”) is a fast, lightweight web server that also works as a reverse proxy, load balancer and HTTP cache. It powers a large share of the busiest websites because it handles thousands of simultaneous connections with very little memory.

This guide shows how to install Nginx on Ubuntu 26.04 LTS (Resolute Raccoon), open the firewall, host a website with a server block, secure it with a free Let’s Encrypt certificate, and install the newest Nginx release from the official nginx.org repository if you need it.

Prerequisites

  • A server or desktop running Ubuntu 26.04.
  • A user account with sudo privileges.
  • For HTTPS: a domain name whose DNS A record points to the server’s public IP address.

Which Nginx Version Should You Install?

There are two sources for Nginx on Ubuntu. Pick one; don’t mix them on the same server.

SourceVersion on Ubuntu 26.04Best for
Method 1: Ubuntu repositoryNginx 1.28.3Most servers. Security fixes come through normal Ubuntu updates.Go to Method 1 →
Method 2: nginx.org repositoryLatest stable (1.30) or mainline (1.31)Newer features, or matching the version used elsewhere.Go to Method 2 →

Steps 2 to 6 (firewall, testing, server blocks and HTTPS) apply to both methods, with the small layout differences noted in Method 2.

Ubuntu keeps the version fixed for the life of the release and backports security fixes into it, so an older version number from Ubuntu is still patched. See the Nginx release history for the current stable and mainline versions.

Step 1 of 6Install Nginx from the Ubuntu Repository (Method 1)

Refresh the package index and install Nginx from the Ubuntu repository:

sudo apt update
sudo apt install nginx -y

Ubuntu starts the service and enables it at boot automatically. Check the installed version and the service status:

nginx -v
systemctl status nginx

You should see active (running). Press q to leave the status view.

Step 2 of 6Allow HTTP and HTTPS in the Firewall

If you use UFW, Nginx registers three application profiles:

sudo ufw app list
ProfileOpens
Nginx HTTPPort 80
Nginx HTTPSPort 443
Nginx FullPorts 80 and 443

Allow SSH first so you don’t lock yourself out, then allow web traffic and turn the firewall on:

sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
sudo ufw status

Step 3 of 6Test the Web Server

Open http://your_server_ip in a browser. You should see the Welcome to nginx! page. To find the server’s IP address, or to test from the server itself, run:

hostname -I
curl -I http://localhost

A response that starts with HTTP/1.1 200 OK and contains Server: nginx confirms Nginx is serving pages.

Step 4 of 6Manage the Nginx Service

These are the commands you’ll use most often:

CommandWhat it does
sudo nginx -tCheck the configuration for errors. Always run it before a reload.
sudo systemctl reload nginxApply configuration changes without dropping connections
sudo systemctl restart nginxStop and start the service
sudo systemctl stop nginxStop the service
sudo systemctl disable nginxDon’t start Nginx at boot

Step 5 of 6Host a Website with a Server Block

Server blocks (the Nginx version of virtual hosts) let one server host several websites. Replace example.com with your domain in the commands below. Create a folder for the site and a test page:

sudo mkdir -p /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/example.com/html
nano /var/www/example.com/html/index.html
<!DOCTYPE html>
<html>
  <head><title>example.com</title></head>
  <body><h1>It works! example.com is served by Nginx.</h1></body>
</html>

Create the server block configuration:

sudo nano /etc/nginx/sites-available/example.com
server {
    listen 80;
    listen [::]:80;

    server_name example.com www.example.com;
    root /var/www/example.com/html;
    index index.html index.htm;

    access_log /var/log/nginx/example.com.access.log;
    error_log  /var/log/nginx/example.com.error.log;

    location / {
        try_files $uri $uri/ =404;
    }
}

Enable the site by linking it into sites-enabled, check the syntax, and reload:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Visit http://example.com to see your test page. To remove the default welcome site, delete its link with sudo rm /etc/nginx/sites-enabled/default and reload Nginx.

Step 6 of 6Secure the Site with Let’s Encrypt (HTTPS)

Certbot gets a free certificate from Let’s Encrypt and updates your server block for HTTPS automatically. Install it with the Nginx plugin:

sudo apt install certbot python3-certbot-nginx -y

Request a certificate for your domain. Certbot asks for an email address for expiry notices and adds a redirect from HTTP to HTTPS:

sudo certbot --nginx -d example.com -d www.example.com

Certificates are valid for 90 days. The Certbot package installs a systemd timer that renews them automatically. Confirm the timer and test a renewal:

systemctl list-timers | grep certbot
sudo certbot renew --dry-run

For more options, see how to generate a Let’s Encrypt SSL certificate using Certbot.

Method 2: Install the Latest Nginx from nginx.org

The Nginx project publishes its own packages for Ubuntu 26.04 with the newest stable or mainline release. If Nginx from Ubuntu is already installed, remove it first so the two don’t conflict:

sudo apt remove nginx nginx-common -y

Install the tools needed to add the repository, then import the nginx.org signing key:

sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring -y
curl -fsSL https://nginx.org/keys/nginx_signing.key | gpg --dearmor | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg > /dev/null

Add the stable repository. For mainline, replace packages/ubuntu with packages/mainline/ubuntu:

echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/ubuntu $(lsb_release -cs) nginx" | sudo tee /etc/apt/sources.list.d/nginx.list

Give the nginx.org packages priority over Ubuntu’s, then install:

printf "Package: *nPin: origin nginx.orgnPin: release o=nginxnPin-Priority: 900n" | sudo tee /etc/apt/preferences.d/99nginx
sudo apt update
sudo apt install nginx -y
sudo systemctl enable --now nginx
nginx -v

Note: The nginx.org package is laid out differently from Ubuntu’s: there’s no sites-available or sites-enabled. Put each site in its own file in /etc/nginx/conf.d/ ending in .conf. The default web root is /usr/share/nginx/html and the worker processes run as the nginx user instead of www-data.

Important Nginx Files and Folders

PathPurpose
/etc/nginx/nginx.confMain configuration file
/etc/nginx/sites-available/Server blocks you have defined (Ubuntu package)
/etc/nginx/sites-enabled/Links to the server blocks that are active
/etc/nginx/conf.d/Extra configuration; site files for the nginx.org package
/etc/nginx/snippets/Reusable configuration pieces
/var/www/htmlDefault web root (Ubuntu package)
/var/log/nginx/access.logEvery request served
/var/log/nginx/error.logErrors and warnings: check here first

Troubleshooting

  • Nginx fails to start with “Address already in use”: another program, often Apache, is using port 80. Find it with sudo ss -tlnp | grep ‘:80’, then stop it (for Apache: sudo systemctl disable –now apache2).
  • 403 Forbidden: Nginx can’t read the files or there’s no index file. Check that the folder is readable with namei -l /var/www/example.com/html/index.html.
  • Changes have no effect: run sudo nginx -t; if it reports an error, the reload didn’t apply. Also check that the site is linked in sites-enabled.
  • Certbot can’t verify the domain: make sure the domain’s DNS points to this server and that port 80 is open in UFW and in your cloud provider’s firewall.

Uninstall Nginx

Remove Nginx and its configuration files. Your website files in /var/www are not touched:

sudo apt purge nginx nginx-common -y
sudo apt autoremove -y

Conclusion

Nginx is now serving your site over HTTPS on Ubuntu 26.04. Next, install PHP 8.5 on Ubuntu 26.04 for dynamic sites, use Nginx as a reverse proxy for Node.js, Python or Docker apps, and review how to secure your Nginx web server. Running Ubuntu 24.04? See how to install Nginx on Ubuntu 24.04.

Rahul

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

Leave a Reply

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


Back to top