Sub topic 3
Automating renewal
Let's Encrypt certificates expire after 90 days, so renewal must run without manual steps. This chapter assumes you have issued a certificate with Certbot, that nginx serves it, and that you can use sudo, systemctl, and journal logs.
Automating renewal
On most distributions, Certbot installs a systemd timer that runs certbot renew about twice a day. Confirm it exists and test a renewal against the staging environment:
systemctl list-timers | grep -i certbot
sudo certbot renew --dry-runCertbot renews only certificates within 30 days of expiry, so frequent runs do not cause unnecessary reissuance.
Reloading the web server after renewal with hooks
A renewed certificate on disk has no effect until nginx reloads it. A deploy hook is a script that Certbot runs after each successful renewal. Create one:
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.shVerifying the certificate and renewal
Check the certificate that clients actually receive. The notAfter date should be about 90 days away after a successful renewal:
echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null \
| openssl x509 -noout -subject -issuer -dates
sudo certbot certificatesTroubleshooting common failures
| Symptom | Likely cause | Fix |
|---|---|---|
| Validation fails with a connection error | Port 80 is blocked, or DNS points elsewhere | Open port 80 and check the A record |
| Renewal succeeds but the old certificate is served | Deploy hook missing or nginx not reloaded | Create the hook and run `sudo nginx -t` |
| Production requests are rejected | Rate limit reached | Test with `--staging` and wait before retrying |
Read /var/log/letsencrypt/letsencrypt.log for the full error.
Protecting private keys and account data
Private keys live in /etc/letsencrypt/archive and /etc/letsencrypt/live. The account key in /etc/letsencrypt/accounts authorizes your ACME account. Anyone who reads either file can impersonate your server. Keep the tree root-only:
sudo chmod 700 /etc/letsencryptNever commit these files to version control.
Wildcard certificates with DNS-01
A wildcard certificate such as *.example.com requires the DNS-01 challenge. Manual DNS mode cannot renew unattended, so use a DNS provider plugin. This example uses Cloudflare:
sudo apt install python3-certbot-dns-cloudflare
sudo certbot certonly --dns-cloudflare \
--dns-cloudflare-credentials /root/cloudflare.ini \
-d example.com -d '*.example.com'The credentials file holds one line, dns_cloudflare_api_token = YOUR_TOKEN. Set it to chmod 600.
Exercise
Why does a wildcard certificate need the DNS-01 challenge instead of HTTP-01?
Show answer
HTTP-01 validates one hostname by fetching a file over port 80. A wildcard covers every subdomain, and the CA requires DNS-01 to prove control of the whole domain by publishing a TXT record.