Install NGINX on Ubuntu 24.04 and 22.04

Homelab

NGINX is a fast web server and reverse proxy. Install it from Ubuntu's packages, open the firewall, confirm it works and serve your first site.

Full guide: How To Install Nginx on Ubuntu 24.04

Related guide: Install NGINX in 60 seconds (with HTTPS)

1. Install

Installs NGINX and starts it.

sudo apt update
sudo apt install nginx

2. Open the firewall

Allow HTTP and HTTPS if UFW is on.

sudo ufw allow 'Nginx Full'

3. Verify

You should get HTTP 200 and see the Welcome to nginx page at http://your_server_ip.

systemctl status nginx --no-pager
curl -I http://localhost

4. Serve your site

Put your files in a folder and point a server block at it.

sudo mkdir -p /var/www/example.com
sudo tee /etc/nginx/sites-available/example.com >/dev/null <<'EOF'
server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com;
    index index.html;
}
EOF
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

5. Add HTTPS

Free certificate from Let's Encrypt, renewed automatically.

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com

6. Troubleshooting

Read the error log and test the config. Port 80 already in use usually means Apache is running.

sudo nginx -t
sudo tail -n 50 /var/log/nginx/error.log
sudo ss -tlpn | grep ':80 '

FAQ

NGINX or Apache?

NGINX is lighter and great as a reverse proxy; Apache is fine too, especially with .htaccess-based apps.

I still see the default page. Why?

Your server_name does not match the address you used, or the site is not linked into sites-enabled. Check both, then nginx -t and reload.

Further reading: NGINX beginner's guide