Nginx is a lightweight web server that can also operate as a reverse proxy and load balancer. This hands-on guide installs Nginx from the Ubuntu 24.04 LTS repository, creates a dedicated server block, configures UFW, validates the configuration, and enables HTTPS with a free Let’s Encrypt certificate.
sudo privileges, and the domain example.com with A/AAAA records pointing to the server. Replace example.com and admin@example.com with your real values.1. Verify the operating system and IP address
Connect to the server over SSH and confirm that you are running the expected Ubuntu release:
cat /etc/os-release
hostnamectl
ip -br address
The PRETTY_NAME field should show Ubuntu 24.04 LTS. Check the IP address currently returned by DNS:
getent ahosts example.com
If the returned address does not belong to this server, correct the DNS record and wait for propagation before requesting a TLS certificate.
2. Update Ubuntu and install Nginx
sudo apt update
sudo apt upgrade -y
sudo apt install nginx curl -y
The Ubuntu package creates a systemd service and starts Nginx automatically. Verify the installed version, service state, and listening ports:
nginx -v
sudo systemctl status nginx --no-pager
sudo ss -lntp | grep -E ':80|:443'
The service output should contain Active: active (running), and Nginx should be listening on TCP port 80.
3. Allow web traffic through UFW
If UFW is enabled on the server, allow SSH before changing the firewall so that you do not lock yourself out:
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
sudo ufw status verbose
The Nginx Full application profile permits TCP ports 80 and 443. If the firewall is controlled by a router, cloud security group, or another network layer, open the same ports there. Do not enable UFW blindly on a server with custom networking rules.
4. Create the document root and a test page
sudo mkdir -p /var/www/example.com/html
sudo chown -R "$USER":"$USER" /var/www/example.com
sudo chmod -R 755 /var/www/example.com
Create a small HTML page:
cat > /var/www/example.com/html/index.html <<'EOF'
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Nginx on Ubuntu 24.04</title>
</head>
<body>
<h1>Nginx is working!</h1>
<p>This site is running on Ubuntu Server 24.04 LTS.</p>
</body>
</html>
EOF
5. Create an Nginx server block
Ubuntu stores available website configurations in /etc/nginx/sites-available and enables them through symbolic links in sites-enabled.
sudo tee /etc/nginx/sites-available/example.com > /dev/null <<'EOF'
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location / {
try_files $uri $uri/ =404;
}
}
EOF
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
If this is the only website hosted on the server, disable the default site:
sudo rm -f /etc/nginx/sites-enabled/default
6. Test the configuration before reloading Nginx
Always run nginx -t before applying a configuration change:
sudo nginx -t
sudo systemctl reload nginx
sudo systemctl is-active nginx
A valid configuration produces output similar to:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
active
Test the virtual host locally even if DNS has not propagated yet:
curl -I -H 'Host: example.com' http://127.0.0.1
curl http://example.com
The expected HTTP status is 200 OK.
7. Enable HTTPS with Let’s Encrypt
Continue only after the domain resolves to this server and TCP port 80 is reachable from the Internet:
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx
-d example.com
-d www.example.com
-m admin@example.com
--agree-tos
--redirect
--no-eff-email
Certbot updates the Nginx configuration, installs the certificate, and redirects HTTP traffic to HTTPS. Verify the certificate and automatic renewal:
sudo certbot certificates
sudo certbot renew --dry-run
systemctl list-timers | grep certbot
curl -I https://example.com
8. Common Nginx administration commands
# Validate the configuration
sudo nginx -t
# Apply a valid configuration without dropping active connections
sudo systemctl reload nginx
# Restart the service completely
sudo systemctl restart nginx
# Display recent errors for this website
sudo tail -n 100 /var/log/nginx/example.com.error.log
# Follow incoming requests in real time
sudo tail -f /var/log/nginx/example.com.access.log
# Read recent systemd journal entries
sudo journalctl -u nginx --since '30 minutes ago' --no-pager
9. Troubleshooting common errors
“Address already in use”
Another process already owns port 80 or 443. Identify it before stopping any service:
sudo ss -lntp | grep -E ':80|:443'
sudo systemctl status apache2 --no-pager
403 Forbidden
Inspect permissions on every directory in the path, then read the site-specific error log:
namei -l /var/www/example.com/html/index.html
sudo tail -n 50 /var/log/nginx/example.com.error.log
502 Bad Gateway
This usually means that Nginx is configured as a reverse proxy but its upstream application is unavailable. Check the upstream service, socket, or listening port before changing Nginx:
sudo nginx -T | less
sudo ss -lntp
sudo journalctl -u nginx -n 100 --no-pager
10. Roll back or remove the sample website
To disable only this website while keeping Nginx installed:
sudo rm -f /etc/nginx/sites-enabled/example.com
sudo nginx -t
sudo systemctl reload nginx
To remove both its configuration and document root:
sudo rm -f /etc/nginx/sites-available/example.com
sudo rm -rf /var/www/example.com
The commands above permanently delete files. Confirm the domain and keep a backup before running them. To uninstall Nginx entirely:
sudo apt remove nginx nginx-common -y
sudo apt autoremove -y
Deployment checklist
sudo nginx -treports a successful configuration test.- The Nginx service is
active (running). - UFW or an external firewall permits TCP ports 80 and 443.
- The domain resolves to the correct server address.
- HTTP redirects to HTTPS and the certificate is valid.
sudo certbot renew --dry-runsucceeds.- The access and error logs are written to the expected files.
References: Ubuntu Server documentation: Install Nginx, Ubuntu Server documentation: Configure Nginx, and the Server World Ubuntu 24.04 topic index.