Skip to content

How to Install and Configure Nginx on Ubuntu 24.04 with Free SSL (Let's Encrypt)

Cloud Linux 34 Views 5 min read Updated
How to Install and Configure Nginx on Ubuntu 24.04 with Free SSL (Let's Encrypt) - cover image

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.html

1. Update the Server

Always start with the latest package lists and security updates:

sudo apt update && sudo apt upgrade -y

Figure 1: Updating packages on Ubuntu 24.04

Figure 1: Updating packages on Ubuntu 24.04

2. Install Nginx

Install Nginx from the official Ubuntu repository:

sudo apt install nginx -y

Enable it at boot and check that it is running:

sudo systemctl enable nginx
sudo systemctl start nginx
sudo systemctl status nginx

You should see active (running) in green:

Figure 2: Nginx service running

Figure 2: Nginx service running

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

Figure 3: UFW rules for SSH and Nginx

Figure 3: UFW rules for SSH and Nginx

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:

ProfileOpens
Nginx HTTPPort 80 only
Nginx HTTPSPort 443 only
Nginx FullPorts 80 and 443

4. Test the Default Page

Find your server's public IP:

curl -4 ifconfig.me

Open http://YOUR_SERVER_IP in a browser. You should see the Nginx welcome page:

Figure 4: The default Nginx welcome page

Figure 4: The default 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.com

Add a test page

Create /var/www/example.com/html/index.html:

nano /var/www/example.com/html/index.html

Paste 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.com

Add 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/default

Test the configuration and reload Nginx:

sudo nginx -t
sudo systemctl reload nginx

If 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 successful

Tip: 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 -y

Request the certificate (Certbot also updates the Nginx config for HTTPS):

sudo certbot --nginx -d example.com -d www.example.com

Answer the prompts:

  1. Enter an email address for renewal notices.
  2. Agree to the terms of service.
  3. Choose whether to share your email with EFF.

Figure 5: Certbot installing the certificate

Figure 5: Certbot installing the certificate

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-run

Now open https://example.com. The browser shows the padlock and your page:

Figure 6: The website over HTTPS

Figure 6: The website over HTTPS

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.log

Address already in use

Another program (often Apache) is using port 80. Find it with:

sudo ss -tulpn | grep :80

Stop and disable Apache if it is not needed:

sudo systemctl stop apache2
sudo systemctl disable apache2

Useful Commands

CommandPurpose
sudo nginx -tTest configuration
sudo systemctl reload nginxApply changes without downtime
sudo systemctl restart nginxFull restart
sudo tail -f /var/log/nginx/error.logWatch 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)