NetworkingSecurity

Free SSL with Let’s Encrypt for Nginx: From Issuing to Automatic Renewal

Issue a free HTTPS certificate with Certbot on Ubuntu, write a clean Nginx config, automate renewal with a systemd timer, and catch up on Let's Encrypt's recent changes.

Free SSL with Let’s Encrypt for Nginx: From Issuing to Automatic Renewal
Table of contents
  1. Prerequisites
  2. Installing Certbot
  3. Issuing a certificate with the Nginx plugin
  4. A complete, clean configuration
  5. Automatic renewal
  6. Changes you should know about
  7. HSTS, one step at a time
  8. Checking the certificate and its expiry
  9. Wildcard certificates
  10. Common errors

HTTPS is no longer optional: browsers flag plain HTTP pages as “Not secure”, HTTP/2 effectively only works over TLS, and many APIs refuse unencrypted requests. Let’s Encrypt issues trusted certificates for free and automatically, and its official client, Certbot, does almost all of the work on Nginx. In this guide we move a domain on Ubuntu 24.04 to HTTPS and make sure renewal happens without us.

Prerequisites

  • The domain’s A record (and AAAA, if you have IPv6) points at the server. Confirm with dig +short example.com.
  • Ports 80 and 443 are open to the internet. HTTP-01 validation always happens on port 80, even if your site only serves on 443.
  • Nginx is installed and has a server block with the right server_name for the domain.
sudo ufw allow 'Nginx Full'      # if you use UFW
sudo nginx -t && sudo systemctl reload nginx

Installing Certbot

On Ubuntu 24.04 the package from the official archive is enough. Certbot’s own documentation recommends the snap; both work, just pick one so you don’t end up with two copies on the server.

sudo apt update
sudo apt install certbot python3-certbot-nginx
certbot --version

Issuing a certificate with the Nginx plugin

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

On first run Certbot asks for an email address and agreement to the terms, then:

  1. finds the domain’s server block in the Nginx configuration;
  2. creates a temporary file under /.well-known/acme-challenge/ so Let’s Encrypt can verify you control the domain;
  3. saves the certificate in /etc/letsencrypt/live/example.com/ and adds the ssl_certificate lines to the configuration;
  4. optionally adds an HTTP to HTTPS redirect.

If you would rather Certbot didn’t touch your Nginx files, use certonly with the webroot method and write the SSL lines yourself:

sudo certbot certonly --webroot -w /var/www/html -d example.com -d www.example.com

In that case the challenge path in Nginx must point at the same folder, with no redirect or proxy in front of it:

location ^~ /.well-known/acme-challenge/ {
    root /var/www/html;
}

A complete, clean configuration

This example redirects HTTP to HTTPS and forwards traffic to an application running on port 8080 of the same server (a Docker container, for example):

server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;

    location ^~ /.well-known/acme-challenge/ { root /var/www/html; }
    location / { return 301 https://example.com$request_uri; }
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name example.com www.example.com;

    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
    }
}

Ubuntu 24.04 ships Nginx 1.24, which understands this listen 443 ssl http2 form. From Nginx 1.25.1 the new form is listen 443 ssl; plus a separate http2 on; line, and the old form only produces a warning.

sudo nginx -t && sudo systemctl reload nginx

Automatic renewal

The certbot package on Ubuntu installs a systemd timer that runs twice a day and, by default, only renews certificates with less than 30 days left:

systemctl list-timers | grep certbot
sudo certbot renew --dry-run

If --dry-run completes without errors, real renewal will work too. With the nginx plugin, Certbot reloads Nginx itself after renewing; with the webroot method you need a deploy hook so Nginx picks up the new certificate:

sudo tee /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh >/dev/null <<'EOF'
#!/bin/sh
systemctl reload nginx
EOF
sudo chmod +x /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh

Scripts in that folder only run when a certificate was actually renewed, not on every run of the timer.

Changes you should know about

  • OCSP is gone: Let’s Encrypt ended OCSP support in 2025 and its certificates no longer contain an OCSP URL. Remove ssl_stapling on; from old configurations; it no longer does anything except log warnings.
  • No more expiry emails: Let’s Encrypt stopped sending expiry reminder emails in 2025. Monitoring expiry dates is now your job (a simple command follows below).
  • Shorter certificate lifetimes: Let’s Encrypt has announced it will gradually reduce certificate lifetimes from 90 days to 45. If automatic renewal works, you will never notice; if you copy certificates around by hand anywhere, automate it now.

HSTS, one step at a time

The HSTS header tells browsers to connect only over HTTPS for a given period. Start with a small value, because until it expires returning visitors cannot go back to HTTP:

add_header Strict-Transport-Security "max-age=86400" always;

After a few problem-free days, raise it to a year (31536000). Only add includeSubDomains once you are sure every subdomain has HTTPS.

Checking the certificate and its expiry

sudo certbot certificates
echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -issuer -dates

For monitoring, openssl’s -checkend option is exactly what you need: it exits non-zero if the certificate expires within the given number of seconds:

echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null \
  | openssl x509 -noout -checkend $((14*24*3600)) || echo "certificate expires within 14 days!"

Wildcard certificates

For *.example.com HTTP validation is not enough; you need DNS-01, where Certbot asks for a TXT record named _acme-challenge. If your DNS is on Cloudflare, the python3-certbot-dns-cloudflare plugin automates this with an API token limited to that zone. The manual method (--manual) does not renew automatically and is not suitable for production.

Common errors

Error message Likely cause
Timeout during connect Port 80 is closed from outside; check the server firewall and the data centre’s panel firewall.
Invalid response ... 404 The /.well-known/acme-challenge/ path doesn’t point at the right folder, or a redirect or proxy is in front of it.
DNS problem: NXDOMAIN The DNS record hasn’t propagated yet, or is wrong.
too many certificates ... already issued You hit a rate limit; always test with --dry-run or --staging.
Is your server in Iran? HTTP-01 validation is performed by Let’s Encrypt servers outside the country and needs inbound international access to your server. If that access is disrupted, issuing and renewing fail. DNS-01 only needs outbound access from the server to your DNS provider’s API and to Let’s Encrypt, so it is the more reliable option in that situation.

After HTTPS, the logical next steps are putting your Docker services behind Nginx and closing ports properly alongside Docker.

About the author

Arash Karimian

Network and infrastructure engineer with years of day-to-day work on Linux servers, firewalls and network tuning. Writes about networking, security and server troubleshooting.

All articles by Arash Karimian

Comments

No comments yet. For technical questions, include the details (OS version, the exact error and the command you ran) to get a precise answer.

Leave a comment