How to Install and Configure Nginx on Ubuntu 24.04 with Free SSL (Let's Encrypt)
This guide installs the Nginx web server on Ubuntu 24.04 LTS, opens the firewall, hosts a website with its own server block, and secures it with a free Let's Encrypt SSL certificate that renews automatically.
Note: Replace example.com with your own domain everywhere in this guide, and point the domain's A record to your server's public IP before requesting the certificate.
Prerequisites
- An Ubuntu 24.04 server (AWS EC2, Azure, or any VPS) with at least 1 GB RAM.
- A user with sudo privileges.
- A domain name pointing to the server (for the SSL part).
- Ports 22, 80 and 443 allowed in your cloud security group.
What You Will Build
At the end of this tutorial your server will have this layout:
/etc/nginx/
├── nginx.conf
├── sites-available/
│ ├── default
│ └── example.com
└── sites-enabled/
└── example.com -> ../sites-available/example.com
/var/www/example.com/
└── html/
└── index.html1. Update the Server
Always start with the latest package lists and security updates:
sudo apt update && sudo apt upgrade -y
2. Install Nginx
Install Nginx from the official Ubuntu repository:
sudo apt install nginx -yEnable it at boot and check that it is running:
sudo systemctl enable nginx
sudo systemctl start nginx
sudo systemctl status nginxYou should see active (running) in green:

Press q to exit the status view.
3. Allow Nginx Through the Firewall
Ubuntu uses UFW. Allow SSH first so you do not lock yourself out, then allow HTTP and HTTPS:
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
sudo ufw status
Warning: On AWS EC2, UFW is not enough. Also open ports 80 and 443 in the instance Security Group, otherwise the site will not load.
The Nginx application profiles in UFW are:
| Profile | Opens |
|---|---|
| Nginx HTTP | Port 80 only |
| Nginx HTTPS | Port 443 only |
| Nginx Full | Ports 80 and 443 |
4. Test the Default Page
Find your server's public IP:
curl -4 ifconfig.meOpen http://YOUR_SERVER_IP in a browser. You should see the Nginx welcome page:

5. Create a Website (Server Block)
Create the web root
sudo mkdir -p /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/example.com/html
sudo chmod -R 755 /var/www/example.comAdd a test page
Create /var/www/example.com/html/index.html:
nano /var/www/example.com/html/index.htmlPaste this content:
<!DOCTYPE html>
<html>
<head>
<title>example.com</title>
</head>
<body>
<h1>example.com is working!</h1>
<p>Served by Nginx on Ubuntu 24.04.</p>
</body>
</html>Create the server block
sudo nano /etc/nginx/sites-available/example.comAdd the following configuration:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html index.htm;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location / {
try_files $uri $uri/ =404;
}
}Enable the site
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo unlink /etc/nginx/sites-enabled/defaultTest the configuration and reload Nginx:
sudo nginx -t
sudo systemctl reload nginxIf the test passes you will see:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successfulTip: Always run sudo nginx -t before reloading. A typo in a config file can stop Nginx from starting.
6. Secure the Site with Let's Encrypt SSL
Install Certbot and the Nginx plugin:
sudo apt install certbot python3-certbot-nginx -yRequest the certificate (Certbot also updates the Nginx config for HTTPS):
sudo certbot --nginx -d example.com -d www.example.comAnswer the prompts:
- Enter an email address for renewal notices.
- Agree to the terms of service.
- Choose whether to share your email with EFF.

Check automatic renewal
Certificates last 90 days. Certbot installs a timer that renews them automatically. Test it with a dry run:
sudo systemctl status certbot.timer
sudo certbot renew --dry-runNow open https://example.com. The browser shows the padlock and your page:

7. Recommended Hardening
Edit /etc/nginx/nginx.conf and add these lines inside the http { } block:
server_tokens off;
client_max_body_size 20M;
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml image/svg+xml;Add security headers inside your server block:
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;Security: Never edit nginx.conf on a production server without a backup. Run sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak first.
Troubleshooting
502 Bad Gateway
The backend application (PHP-FPM, Node.js, etc.) is not running. Check its service and the error log:
sudo tail -n 50 /var/log/nginx/example.com.error.logAddress already in use
Another program (often Apache) is using port 80. Find it with:
sudo ss -tulpn | grep :80Stop and disable Apache if it is not needed:
sudo systemctl stop apache2
sudo systemctl disable apache2Useful Commands
| Command | Purpose |
|---|---|
| sudo nginx -t | Test configuration |
| sudo systemctl reload nginx | Apply changes without downtime |
| sudo systemctl restart nginx | Full restart |
| sudo tail -f /var/log/nginx/error.log | Watch errors live |
Conclusion
Nginx is now installed on Ubuntu 24.04, serving your website over HTTPS with automatic certificate renewal and basic hardening. For more options, read the official Nginx documentation.
Discussion (0)