Posted by

Free HTTPS on Nginx with Certbot and Let's Encrypt: Install, Issue Certificates and Test Auto-Renewal

Every public website should use HTTPS. It protects logins and forms from being read or changed in transit, browsers mark plain HTTP pages as "Not secure", and many modern browser features only work on secure pages. Let's Encrypt is a free, automated certificate authority, and Certbot is the client recommended by the EFF for requesting and renewing its certificates on your own server.

This guide sets up Certbot on an Ubuntu or Debian server running Nginx, issues a certificate, redirects HTTP to HTTPS and, most importantly, makes sure renewal works without you having to remember it.

1. Before you start

  • A domain that points to the server. The DNS A record (and AAAA record, if you have one) for example.com and www.example.com must point to this server's public IP address. If an AAAA record exists, Let's Encrypt may validate over IPv6, so make sure Nginx answers on IPv6 too, or remove the record.
  • Port 80 open from the internet. The default HTTP-01 challenge connects to your server on port 80. Keep it open even after you switch to HTTPS, because renewals use it too. With UFW: sudo ufw allow 'Nginx Full' (ports 80 and 443).
  • A working Nginx server block with a server_name that matches the domain names you will request. The Certbot Nginx plugin uses it to find where to install the certificate.

Check that the Nginx configuration is valid before you begin:

sudo nginx -t

2. Install Certbot

The Certbot project recommends installing it with snap, which gives you a current version that updates itself. Remove any older Certbot from the system packages first so the two do not conflict:

sudo apt remove certbot
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
certbot --version

If you prefer distribution packages, sudo apt install certbot python3-certbot-nginx also works on Ubuntu and Debian. The version may be older, but it receives security updates from your distribution. Pick one method and stick with it.

3. Get a certificate and let Certbot configure Nginx

The --nginx plugin proves you control the domain, gets the certificate, and edits your server block to use it:

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

On the first run Certbot asks for an email address and for you to accept the Let's Encrypt Subscriber Agreement. It then offers to redirect all HTTP traffic to HTTPS; accept this unless you have a specific reason not to.

The first -d name becomes the certificate name, and every name you list is included in one certificate. Request all the names a site needs in one command, so you do not end up with several overlapping certificates.

Prefer to edit Nginx yourself? Use certonly

If you manage the Nginx configuration with a template or a configuration management tool, ask Certbot only for the certificate:

# Uses the Nginx plugin for the challenge but does not change your config
sudo certbot certonly --nginx -d example.com -d www.example.com

# Or place the challenge file in the site's web root
sudo certbot certonly --webroot -w /var/www/example.com -d example.com -d www.example.com

With the webroot method, Nginx must serve /.well-known/acme-challenge/ from that directory over plain HTTP on port 80. For the very first certificate, start with only the port 80 server block below: an HTTPS block that points at certificate files that do not exist yet makes nginx -t fail. Add the port 443 block after the certificate has been issued.

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

    # Let Certbot's webroot challenge work over plain HTTP
    location /.well-known/acme-challenge/ {
        root /var/www/example.com;
    }

    # Send everything else to HTTPS
    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    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;

    root /var/www/example.com;
}

Keeping the challenge location in the port 80 block means renewals keep working even though all other HTTP traffic is redirected. Then test and reload:

sudo nginx -t && sudo systemctl reload nginx

4. Where the files are (and which ones to use)

File in /etc/letsencrypt/live/example.com/What it containsUse it for
fullchain.pemYour certificate plus the intermediate certificateNginx ssl_certificate
privkey.pemThe private keyNginx ssl_certificate_key
cert.pemYour certificate onlyRarely needed; using it alone in Nginx causes chain errors in some clients
chain.pemThe intermediate certificate onlySome software that asks for the chain separately

The files in live/ are links to the newest version in archive/. Always point your configuration at the live/ paths and never copy the files elsewhere, or your services will keep using an old certificate after renewal. Keep privkey.pem private: do not change its permissions to make it readable by other users and never put it in a Git repository.

5. Make sure automatic renewal works

Let's Encrypt certificates are short-lived on purpose, and Let's Encrypt has announced that lifetimes will become shorter over time. Do not plan around a fixed number of days; rely on automatic renewal and check that it works.

Certbot installs a scheduled task that runs certbot renew twice a day. It only renews certificates that are close to expiry (by default, within 30 days), so frequent runs are safe. See the timer:

systemctl list-timers | grep -i certbot

# Typical timer names
#   snap.certbot.renew.timer   (snap installation)
#   certbot.timer              (apt package)

Then run a test renewal against the Let's Encrypt staging environment. It does not change your certificates and does not count against the production limits:

sudo certbot renew --dry-run

If the dry run succeeds, real renewals will most likely succeed too. If it fails, fix the problem now, while the current certificate is still valid.

Also note that Let's Encrypt stopped sending certificate expiry reminder emails in 2025. You can no longer rely on an email to warn you, so test renewal after any change to DNS, firewall or Nginx, and consider an external monitor that checks the expiry date of your live site.

6. Reload services after renewal with a deploy hook

When you use the --nginx installer, Certbot reloads Nginx after renewing. With certonly, or when other services (a mail server, for example) use the same certificate, add a deploy hook. It runs only when a certificate was actually renewed.

The simplest way is an executable script in the deploy hooks directory, which Certbot runs for every renewed certificate:

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

You can also attach a hook to one certificate with --deploy-hook "systemctl reload nginx" when you request it; Certbot stores it in that certificate's renewal settings.

7. Everyday Certbot commands

TaskCommand
List certificates, their names and expiry datessudo certbot certificates
Test renewal without changing anythingsudo certbot renew --dry-run
Add a name to an existing certificatesudo certbot --nginx --cert-name example.com -d example.com -d www.example.com -d shop.example.com
Remove a certificate you no longer usesudo certbot delete --cert-name old.example.com
Check the certificate a live site is servingopenssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -subject -dates
Read Certbot's logsudo less /var/log/letsencrypt/letsencrypt.log

When you change the list of names with --cert-name, list every name the certificate should contain, not only the new one. Names you leave out are removed. Before deleting a certificate, make sure no Nginx server block still points at its files, or Nginx will fail to start.

8. Common errors and how to fix them

  • "Timeout during connect" or "Connection refused". Let's Encrypt could not reach port 80. Check the firewall (sudo ufw status), your cloud provider's security group, and that Nginx is listening on port 80 for IPv4 and IPv6.
  • "Invalid response" or a 404 for /.well-known/acme-challenge/. The request reached a different server or server block. Check the DNS records (including AAAA), any CDN or proxy in front of the server, and that server_name matches the requested names.
  • "Could not automatically find a matching server block". The Nginx plugin found no server_name for the domain. Add it to the right server block, run sudo nginx -t, reload, and try again.
  • "Too many certificates" or "too many failed authorizations". You hit a Let's Encrypt rate limit, usually by repeating a failing command. Wait for the limit to reset and test with --dry-run (or --staging) until the process works. The current limits are on the Let's Encrypt rate limits page.
  • Browser still shows the old certificate. Nginx has not been reloaded, or it points at files outside /etc/letsencrypt/live/. Reload Nginx and check the paths.

9. Wildcard certificates need DNS validation

A wildcard such as *.example.com can only be issued with the DNS-01 challenge, where you prove control by creating a TXT record. This is best done with a Certbot DNS plugin for your DNS provider so renewals stay automatic. A manual DNS challenge does not renew on its own, so avoid it for production. For most sites, listing the exact names you need is simpler.

Quick checklist

  • DNS A and AAAA records point to the server.
  • Ports 80 and 443 are open; port 80 stays open for renewals.
  • Certbot installed one way only (snap or apt).
  • All site names are in one certificate.
  • Nginx uses fullchain.pem and privkey.pem from /etc/letsencrypt/live/.
  • HTTP redirects to HTTPS.
  • The renewal timer is active and sudo certbot renew --dry-run succeeds.
  • A deploy hook reloads every service that uses the certificate.
  • You have an external check of the live certificate's expiry date.

Together with key-only SSH, a firewall, Fail2ban and automatic security updates, working HTTPS with tested auto-renewal completes a solid baseline for a public Linux web server.

Sources

  • Certbot documentation and instructions for Nginx on Linux (certbot.eff.org, eff-certbot.readthedocs.io)
  • Let's Encrypt documentation: Getting Started, Challenge Types, Rate Limits and Staging Environment (letsencrypt.org/docs)
  • Let's Encrypt blog: announcements on ending expiration notification emails and on shorter certificate lifetimes (letsencrypt.org)
  • Nginx documentation: Configuring HTTPS servers and the ngx_http_ssl_module (nginx.org)
  • Ubuntu Server documentation: Security (documentation.ubuntu.com/server)