Home How ToSecure Nginx with Let’s Encrypt SSL on Ubuntu

Secure Nginx with Let’s Encrypt SSL on Ubuntu

by Knowledge Base Linux
Secure Nginx with Let's Encrypt SSL on Ubuntu

Why Secure Nginx with Let’s Encrypt?

Serving your site over HTTPS is no longer optional — browsers flag plain HTTP as “Not Secure”, and Google uses HTTPS as a ranking signal. Let’s Encrypt is a free, automated certificate authority trusted by all major browsers. In this guide you will learn how to install Certbot on Ubuntu and use it to secure Nginx with Let’s Encrypt SSL, including automatic renewal.

Certbot is the official Let’s Encrypt client. Its Nginx plugin can automatically edit your server blocks, install the certificate, and set up HTTP-to-HTTPS redirection — the whole process takes a few minutes. Best of all, certificates renew automatically, so once configured you can forget about manual SSL renewals forever.

Prerequisites – Secure Nginx with Let s Encrypt

  • Ubuntu 22.04 server with Nginx installed and running
  • A domain name (e.g. example.com) with DNS A records pointing to your server IP (both example.com and www.example.com)
  • Root or sudo access
  • Port 80 reachable from the internet (Let’s Encrypt validates domain control over HTTP)
Let's Encrypt SSL certificate for Nginx

Step 1 – Install Certbot and the Nginx Plugin

Update the package index and install Certbot with its Nginx plugin. Ubuntu 22.04 ships a recent Certbot in the universe repository, so no PPA is required on current LTS releases:

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

Verify the installation:

certbot --version

Step 2 – Check Your Nginx Server Block

Certbot reads your Nginx configuration to find the domains to certify. Make sure you have a server block with the correct server_name:

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;

    location / {
        try_files $uri $uri/ =404;
    }
}

Test the configuration and reload Nginx:

sudo nginx -t && sudo systemctl reload nginx

If you need a refresher on redirects, see our guide on redirecting HTTP to HTTPS.

Step 3 – Obtain and Install the Certificate

Run Certbot with the Nginx plugin. It will ask for an email (for expiry notices), ask you to agree to the terms, and ask whether to redirect HTTP to HTTPS (choose 2 – Redirect):

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

For a fully non-interactive run (useful in scripts):

sudo certbot --nginx --non-interactive --agree-tos \
  -m [email protected] -d example.com -d www.example.com \
  --redirect

On success you will see “Congratulations!” and the certificate paths under /etc/letsencrypt/live/example.com/.

Step 4 – Verify HTTPS Is Working

Visit https://example.com in a browser and confirm the padlock icon. From the command line:

curl -sI https://example.com | head -n 3
echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -dates

The second command prints the certificate validity dates. Let’s Encrypt certificates are valid for 90 days.

Step 5 – Set Up Automatic Renewal

Certbot installs a systemd timer (or cron job) that attempts renewal twice daily. Verify it:

sudo systemctl status certbot.timer

Test the renewal process without making changes:

sudo certbot renew --dry-run

If the dry run succeeds, renewals will happen automatically. Certificates renew when fewer than 30 days remain, and the Nginx plugin reloads Nginx afterward via its deploy hook.

Step 6 – Harden the SSL Configuration (Optional)

After Certbot modifies your server block, consider tightening the TLS settings. Check your grade with an external tester, then adjust ssl_protocols and ciphers if needed:

sudo nano /etc/nginx/sites-available/example.com
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

Reload Nginx after editing: sudo systemctl reload nginx.

Troubleshooting Common Certbot Issues – Secure Nginx with Let s Encrypt

“Failed authorization” / connection errors

Let’s Encrypt must reach http://yourdomain/.well-known/acme-challenge/. Check: DNS A record points to the server (dig +short example.com), port 80 is open in the firewall, and no CDN “I’m under attack” mode is blocking the validation path.

Too many certificates / rate limits

Let’s Encrypt allows 50 certificates per registered domain per week and 5 duplicate certificates per week. During testing, use --dry-run or the staging server (--test-cert) to avoid hitting limits.

Renewal fails after working initially

Often caused by a changed Nginx config or a firewall blocking port 80. Run sudo certbot renew --dry-run to reproduce, check /var/log/letsencrypt/letsencrypt.log, and ensure the certbot.timer is active.

Wildcard Certificates with the DNS Challenge

The HTTP challenge only validates specific hostnames. If you need a wildcard (*.example.com) covering all subdomains, use the DNS-01 challenge instead. This requires API access to your DNS provider via a Certbot DNS plugin:

sudo apt install python3-certbot-dns-cloudflare -y
# store your API token securely:
printf 'dns_cloudflare_api_token = YOUR_TOKEN\n' | sudo tee /etc/letsencrypt/cloudflare.ini >/dev/null
sudo chmod 600 /etc/letsencrypt/cloudflare.ini
sudo certbot certonly --dns-cloudflare \
  --dns-cloudflare-credentials /etc/letsencrypt/cloudflare.ini \
  -d example.com -d '*.example.com'

Plugins exist for Route53, DigitalOcean, Cloudflare, and dozens of other providers. DNS validation also works when port 80 is blocked, making it the right choice for internal or staging servers. After obtaining the certificate, reference the fullchain and privkey paths in your Nginx ssl_certificate directives manually, since --nginx automation is not used with certonly.

Monitoring Certificate Expiry

Even with auto-renewal, monitor expiry as a safety net. A one-line cron check that emails you when fewer than 21 days remain:

#!/bin/bash
for cert in /etc/letsencrypt/live/*/cert.pem; do
  days=$(echo | openssl x509 -in "$cert" -noout -checkend $((21*86400)) >/dev/null && echo OK || echo EXPIRING)
  [ "$days" = "EXPIRING" ] && echo "WARNING: $cert expiring soon" | mail -s "SSL expiry alert" [email protected]
done

Alternatively, external uptime monitors (UptimeRobot, HetrixTools) can watch HTTPS endpoints and alert on certificate problems — useful when you manage certificates across many servers and want a single pane of glass.

Conclusion

You have learned how to install Certbot on Ubuntu and secure Nginx with a free Let’s Encrypt SSL certificate, verify the installation, and automate renewal so the certificate never expires unexpectedly. For multi-service setups (for example behind Docker reverse proxies), the same Certbot workflow applies — just point it at the correct webroot or use the DNS challenge. Keep port 80 open for renewals and monitor the expiry dates periodically.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.