How to Install Nginx on Ubuntu 24.04 and 22.04

How to Install Nginx on Ubuntu 24.04 and 22.04

Install Nginx on Ubuntu 24.04 or 22.04, open the firewall, check the service and host your own site with a server block.

To install Nginx on Ubuntu, update your package lists, run sudo apt install nginx, allow HTTP and HTTPS through the UFW firewall, and confirm the service is running with systemctl status nginx. You can then serve your own site by creating a document root and a server block for your domain. The whole setup takes about ten minutes.

This guide is for Ubuntu 24.04 LTS and Ubuntu 22.04 LTS on a Cloud VPS Hosts VPS or dedicated server. The same commands work on Debian 12. You need SSH access as a user with sudo rights.

What do you need before you install Nginx?

You need a fresh Ubuntu server you can reach over SSH, and optionally a domain name that points to it.

  • A server running Ubuntu 24.04 or 22.04. If you have not prepared it yet, follow our initial Ubuntu server setup guide first.
  • A user with sudo rights and SSH key login. See Setting up an SSH key.
  • For a real website, a domain with an A record (and an AAAA record if you use IPv6) pointing to your server’s IP address.

The examples use example.com as the domain and 203.0.113.10 as the server IP. Replace both with your own values.

How do you install Nginx on Ubuntu?

Nginx is in Ubuntu’s default repositories, so one apt command installs it and starts the service.

sudo apt update
sudo apt install nginx -y

Ubuntu starts Nginx automatically and enables it at boot. Check the installed version with:

nginx -v

Ubuntu 24.04 ships Nginx 1.24 and Ubuntu 22.04 ships Nginx 1.18. Both are maintained with security updates through Ubuntu’s normal update process.

How do you allow Nginx through the firewall?

If UFW is active, allow both HTTP and HTTPS with the “Nginx Full” profile, which opens ports 80 and 443.

sudo ufw app list
sudo ufw allow 'Nginx Full'
sudo ufw status
UFW profile Ports opened When to use it
Nginx HTTP 80 Testing only, before you add SSL
Nginx HTTPS 443 HTTPS only, if another rule handles port 80
Nginx Full 80 and 443 Most websites, including the HTTP to HTTPS redirect

Make sure OpenSSH is still allowed before you enable UFW, or you will lock yourself out of the server. sudo ufw status should list OpenSSH as ALLOW.

How do you check that Nginx is running?

Ask systemd for the service status:

systemctl status nginx

Look for Active: active (running) in the output. Press q to exit. Then open http://203.0.113.10 in your browser. You should see the default “Welcome to nginx!” page, which confirms the web server answers on port 80.

These commands manage the service from now on:

Task Command
Apply configuration changes without dropping connections sudo systemctl reload nginx
Restart the service sudo systemctl restart nginx
Stop or start sudo systemctl stop nginx / sudo systemctl start nginx
Test the configuration for errors sudo nginx -t

How do you host your own website with an Nginx server block?

A server block tells Nginx which folder to serve for which domain. Create a folder for your site, add a page, write the server block and enable it.

1. Create the document root

sudo mkdir -p /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/example.com/html

2. Add a test page

nano /var/www/example.com/html/index.html

Paste a minimal page and save it:

<!doctype html>
<html lang="en">
  <head><title>example.com is working</title></head>
  <body><h1>Nginx is serving example.com</h1></body>
</html>

3. Create the server block

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;

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

4. Enable the site, test and reload

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

Reload only after nginx -t reports “syntax is ok” and “test is successful”. Now open http://example.com and you should see your test page.

If nginx -t reports could not build server_names_hash, you are using long domain names. Open /etc/nginx/nginx.conf, remove the # in front of server_names_hash_bucket_size 64;, then test and reload again.

How do you secure the site with HTTPS?

Once the site works on HTTP, add an SSL certificate so browsers show the padlock. Our guide to installing an SSL certificate on Nginx picks up exactly where this one ends, starting from the server block above. You will need a CSR first, covered in How to generate a CSR.

Paid RapidSSL, GeoTrust and DigiCert certificates, including OV, EV and Wildcard options, are available in our SSL certificate store.

Where are Nginx’s important files on Ubuntu?

Path What it holds
/etc/nginx/nginx.conf Main configuration file
/etc/nginx/sites-available/ Server blocks for each site
/etc/nginx/sites-enabled/ Links to the server blocks Nginx actually loads
/var/www/ Website files
/var/log/nginx/access.log Every request Nginx serves
/var/log/nginx/error.log Errors, the first place to look when something fails

Need a server to follow along? Our Linux VPS plans come with full root access and support Ubuntu 22.04 and 24.04.

Frequently asked questions

Should I install Nginx from Ubuntu’s repository or from nginx.org?

Ubuntu’s repository is the simplest choice and receives security fixes through normal system updates. Use the official nginx.org packages only if you need features from a newer Nginx release.

How do I remove the default Nginx welcome page?

Remove the default site link with sudo rm /etc/nginx/sites-enabled/default, then run sudo nginx -t and reload Nginx. Your own server blocks keep working.

Why do I get a 403 Forbidden error?

Nginx cannot read your files or there is no index file. Check that index.html exists in the folder set as root, and that the folders are readable, for example with sudo chmod -R 755 /var/www/example.com.

Can I run Nginx and Apache on the same server?

Not on the same ports. Only one program can listen on port 80 and 443. If Apache is installed, stop and disable it with sudo systemctl disable --now apache2 before starting Nginx.

Post Your Comment

Knowledgebase
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.