How to install Nginx on Ubuntu 24

To install Nginx on Ubuntu you need one apt command: the package is in Ubuntu’s own repository and starts right away. This guide is for Ubuntu Server 24.04 and 22.04. It also covers the firewall, a first site of your own and the newer Nginx builds from nginx.org.

Install Nginx from the Ubuntu repository

Log in to the server over SSH as a user who can run sudo, then refresh the package list and install Nginx. The service starts on its own and comes back after a reboot.

sudo apt update sudo apt install -y nginx nginx -v

The last command prints the installed version, for example nginx version: nginx/1.24.0 (Ubuntu) on Ubuntu 24.04. Open http://<server address> in a browser: the default page says “Welcome to nginx!”.

The “Welcome to nginx!” page in a browser after installing Nginx on Ubuntu

Open ports 80 and 443 in the firewall

Ubuntu’s firewall, UFW, is off on a fresh system. Check it:

sudo ufw status

If it says Status: active, allow Nginx: the Nginx Full profile opens both HTTP (80) and HTTPS (443).

sudo ufw allow 'Nginx Full' sudo ufw status

Before you turn UFW on over SSH, allow SSH first, or the firewall cuts off your own connection: sudo ufw allow OpenSSH

Set up your first site

On Ubuntu, each site has its own file in /etc/nginx/sites-available/, and Nginx reads the ones linked from /etc/nginx/sites-enabled/. Create a folder for the site’s files and a config file. Replace example.com with your domain.

sudo mkdir -p /var/www/example.com echo '<h1>It works</h1>' | sudo tee /var/www/example.com/index.html sudo nano /etc/nginx/sites-available/example.com

Paste this into the file, save it with Control-O and exit with Control-X:

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

Turn the site on, check the configuration and reload Nginx:

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

The check must end with syntax is ok and test is successful. If it shows an error, it names the file and the line. When the domain points to the server, add HTTPS with a free certificate: how to get a Let’s Encrypt certificate with Certbot.

Install the latest Nginx from nginx.org

Ubuntu’s package gets security fixes but stays on one version for the life of the release. For a newer Nginx, use the official repository from nginx.org. These are the commands from its installation page: they add the nginx.org signing key and the “stable” repository, then install Nginx from there.

sudo apt install -y curl gnupg2 ca-certificates lsb-release ubuntu-keyring curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] https://nginx.org/packages/ubuntu $(lsb_release -cs) nginx" | sudo tee /etc/apt/sources.list.d/nginx.list echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" | sudo tee /etc/apt/preferences.d/99nginx sudo apt update sudo apt install -y nginx sudo systemctl enable --now nginx nginx -v

The nginx.org package brings its own /etc/nginx/nginx.conf, which reads only /etc/nginx/conf.d/*.conf. Installed over Ubuntu’s Nginx, it stops serving the sites in sites-enabled and leaves the service stopped, hence the last two commands. Move each site to /etc/nginx/conf.d/<name>.conf, then check the configuration and reload Nginx with the last two commands of the first site.

Where Nginx keeps its files

  • /etc/nginx/nginx.conf: the main configuration (workers, logs, gzip).
  • /etc/nginx/sites-available/ and /etc/nginx/sites-enabled/: your sites, one file each (Ubuntu’s package).
  • /var/log/nginx/access.log and /var/log/nginx/error.log: requests and errors.
  • /var/www/html/: the default page.
  • Check the configuration: sudo nginx -t
  • Apply it without dropping connections: sudo systemctl reload nginx

If Nginx doesn’t start

  • bind() to 0.0.0.0:80 failed (98: Address already in use): another web server holds port 80, usually Apache. See who holds it: sudo ss -tlnp | grep ':80' If it is Apache, stop it: sudo systemctl disable --now apache2
  • The site shows “Welcome to nginx!” instead of your page: the domain falls into the default site. Check server_name in your file, or remove the default site: sudo rm /etc/nginx/sites-enabled/default Then reload Nginx.
  • The page doesn’t open from outside: check the firewall: sudo ufw status On a cloud server, also check the provider’s security group or firewall for ports 80 and 443.
  • For any other error, read the last lines of the service log: sudo journalctl -u nginx -n 50 Then those of the error log: sudo tail -n 50 /var/log/nginx/error.log

If you also work on a Mac

MacWashing, System Junk: Xcode junk, user caches, installers and logs found, 22.83 GB ready to be cleaned

System Junk in MacWashing finds the caches of every app it knows, including those hidden inside app data, plus logs, Xcode junk and leftovers of deleted apps. It shows how much each takes and clears them in one go. Anything debatable stays unchecked until you decide.

Free trial · macOS 14 or later

Questions and answers

How do I install Nginx on Ubuntu?

Refresh the package list: sudo apt update Then install Nginx: sudo apt install nginx It starts at once and serves its welcome page on port 80.

What is the difference between reload and restart?

reload reads the new configuration and keeps open connections. restart stops and starts the service. Use reload after editing a site, restart only if reload doesn’t help.

Which Nginx version should I use?

Ubuntu’s package is enough for most sites and gets security updates with the system. Take the nginx.org repository when you need a feature from a newer release.