Last updated

Homelab

How To Install Nginx on Ubuntu 24.04

Nginx is a fast web server and reverse proxy. Install it from Ubuntu's packages, open the firewall, check it works, and set up your first site with a server block.

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

1. Install Nginx

Installs Nginx and starts it right away.

sudo apt update
sudo apt install nginx

2. Open the firewall

If UFW is on, allow web traffic. Nginx Full opens 80 and 443.

sudo ufw app list
sudo ufw allow 'Nginx Full'

3. Check it is running

Visit http://your_server_ip in a browser to see the Welcome to nginx page.

systemctl status nginx
curl -I http://localhost

4. Manage the service

Reload applies config changes without dropping connections; always test the config first.

CommandWhat it does
sudo nginx -tTest the configuration
sudo systemctl reload nginxApply config changes without downtime
sudo systemctl restart nginxFull restart
sudo systemctl stop nginxStop Nginx
sudo systemctl enable nginxStart at boot (default)

5. Add a server block for your site

Create a folder for the site and a config file, enable it, test and reload.

sudo mkdir -p /var/www/example.com/html
sudo nano /etc/nginx/sites-available/example.com

server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com/html;
    index index.html;
}

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

6. Important files

Where Nginx keeps things on Ubuntu.

PathWhat it is
/etc/nginx/nginx.confMain configuration
/etc/nginx/sites-available/Site configs (server blocks)
/etc/nginx/sites-enabled/Enabled sites (symlinks)
/var/www/htmlDefault web root
/var/log/nginx/access.logEvery request
/var/log/nginx/error.logErrors

FAQ

How do I add HTTPS?

Install Certbot and run sudo certbot –nginx -d example.com. See our Let's Encrypt guide for the full steps.

I see the default page instead of my site. Why?

Check server_name matches your domain, the site is linked into sites-enabled, and run sudo nginx -t then reload.