Sub topic 12
Automating Renewal
Assume you are comfortable with sudo, systemd timers, nginx, and basic DNS. This chapter also assumes Certbot has already issued a certificate and that the certificate files are used by nginx.
Let's Encrypt certificates have a limited lifetime. Certbot can renew them automatically before they expire. The renew command checks every certificate managed by Certbot and renews only certificates that are close to expiration. Running it regularly is safe because it does not request a new certificate on every run.
How automatic renewal works
When Certbot obtains a certificate, it stores renewal settings under /etc/letsencrypt/renewal/. These settings include the domain names, the validation method, and the authenticator plugin. A scheduled Certbot job later reads these settings.
The usual process is:
Certbot normally installs a systemd timer or, on some systems, a cron job. Check for a timer with:
systemctl list-timers --all | grep -i certbotThe timer may run twice a day. Frequent checks do not create repeated certificates because Certbot renews only when renewal is due.
Test renewal before relying on it
Use a dry run to test the complete renewal process against the Let's Encrypt staging service:
sudo certbot renew --dry-runA successful result shows that Certbot can read its renewal configuration, contact the ACME service, complete validation, and write the expected files in the test environment. The dry run does not replace the live certificate.
| Check | What it verifies |
|---|---|
| `certbot renew --dry-run` | Validation and renewal configuration |
| `systemctl list-timers --all` | A scheduled renewal job exists |
| `sudo nginx -t` | nginx can load its configuration |
| `openssl s_client` | The public server presents the expected certificate |
A dry run can fail even when the original issuance succeeded. DNS may have changed, port 80 may be blocked, or a firewall may now prevent the validation request from reaching the server.
Reload nginx after renewal
Renewed files do not automatically change the certificate already loaded by nginx. nginx must reload its configuration. A deploy hook runs only after Certbot successfully renews a 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.shA reload is preferable to a restart because existing connections usually remain active. Test the hook through the dry run, then inspect the timer and system logs if renewal fails:
sudo journalctl -u certbot.service --since "7 days ago"
sudo systemctl status certbot.timerExercise
Why does certbot renew normally make no changes when run several times in one day?
Show answer
It checks each certificate's renewal status and requests a replacement only when the certificate is near expiration.