Sub topic 12
Automating renewal
This chapter assumes you already have a working Certbot certificate for nginx, and that you know the basics of systemd services and timers. It covers how renewal works, how to confirm that it is scheduled, how to test it, and how to handle the cases that break it.
Why renewal must be automatic
Let's Encrypt certificates are valid for 90 days. The short lifetime limits the damage from a leaked key. It also means you cannot renew by hand and expect to remember. A certificate that expires gives every visitor a browser warning, so renewal needs to run unattended.
Certbot's rule is simple. Each time certbot renew runs, it checks every certificate it manages. If a certificate expires in 30 days or fewer, Certbot renews it. Otherwise it skips that certificate. Because of this, you can run the command often, such as twice a day, and it only does real work near expiry.
What `certbot renew` does
For each certificate, certbot renew reads a renewal configuration file from /etc/letsencrypt/renewal/. That file records the options you used when you first obtained the certificate: the authenticator (for example nginx), the domains, and the challenge type. Certbot repeats the same validation, receives a new certificate, and updates the symlinks in /etc/letsencrypt/live/example.com/ so that fullchain.pem and privkey.pem point at the new files.
This has a consequence that is easy to miss. Renewal only replaces the files on disk. A running nginx process keeps the old certificate in memory until it reloads its configuration. Renewal therefore has two halves: getting the new certificate, and making the server use it. The next chapter covers the second half in detail through hooks.
Finding the scheduled job
The Debian and Ubuntu certbot package installs a scheduler for you. Depending on the release, it is a systemd timer, a cron entry in /etc/cron.d/certbot, or both. Check for the timer first:
systemctl list-timers | grep -i certbot
sudo certbot renew --dry-runThe first command should print a line for certbot.timer with the time of its next and last runs. If it prints nothing, look at /etc/cron.d/certbot. The packaged job runs certbot renew and is skipped when systemd is running, so the two do not overlap.
You can inspect the timer and the service it starts:
systemctl cat certbot.timer
systemctl status certbot.service
journalctl -u certbot.service --since "7 days ago"The journal is the best place to look when you ask "did it actually run?" A healthy timer fires twice a day, and most runs report that no certificates are due.
If you installed Certbot another way
Certbot installed from snap or pip may not set up a timer in the same way. The snap package installs its own snap.certbot.renew.timer. A pip install usually installs nothing. In that case, create the schedule yourself. A cron line is enough:
echo '17 3,15 * * * root certbot renew --quiet' | sudo tee /etc/cron.d/certbot-renewChoose an odd minute rather than 0. Many servers renew on the hour, and spreading the load is courteous to the CA.
Testing with `--dry-run`
The command sudo certbot renew --dry-run simulates a renewal against the Let's Encrypt staging environment. It performs the real validation and runs your hooks, but it does not change the live certificate and does not count against production rate limits. A success message for each certificate means the whole path works: the renewal file, the challenge, the port 80 access, and the plugin.
Run a dry run after any of these changes:
- You edit the nginx configuration.
- You change the firewall or a load balancer.
- You move DNS records.
- You upgrade Certbot or nginx.
A dry run does not prove that nginx will reload at the end, unless your deploy hook is in place. See the hooks chapter for that.
Why renewals fail
Renewal repeats validation, so anything that breaks first-time issuance can also break renewal months later, after you have forgotten the setup. Common causes are in the table.
| Symptom in the log | Likely cause | Fix |
|---|---|---|
| Timeout during connect | Port 80 blocked by a new firewall rule or security group | Reopen TCP 80 |
| 404 on `/.well-known/acme-challenge/` | A redirect or `location` block now intercepts the path | Let the plugin handle the challenge, or serve the path from a webroot |
| DNS problem: NXDOMAIN | The record was removed or the domain expired | Restore the DNS record |
| Could not bind to port 80 | Standalone mode, but nginx already holds the port | Use the `nginx` or `webroot` authenticator |
| Too many failed authorizations | Repeated failures against production | Fix the cause, then test with `--dry-run` |
When a run fails, Certbot writes details to /var/log/letsencrypt/letsencrypt.log. Read the end of that file first.
Reading the state of your certificates
To see what Certbot manages and when each certificate expires, use:
sudo certbot certificatesThe output lists each certificate name, its domains, the expiry date, and the number of valid days remaining. Anything under 30 days that has not renewed means the timer is not working, and you should investigate.
Monitoring so that failure is not silent
A timer that fails quietly leads to an expired certificate on day 90. Let's Encrypt used to send expiry emails to the address you gave at registration, but it has ended that service. Do not rely on it. Add an independent check instead:
- An external monitor that connects to port 443 and alerts when the certificate has fewer than 14 days left.
- A cron job or timer that runs
openssl x509 -checkendagainst the live certificate file. - A systemd
OnFailure=handler oncertbot.servicethat sends you a message.
A simple local check is this one:
sudo openssl x509 -checkend 1209600 -noout \
-in /etc/letsencrypt/live/example.com/fullchain.pem \
&& echo "OK: valid for at least 14 days" \
|| echo "WARNING: expires within 14 days"The value 1209600 is 14 days in seconds. The command exits with status 0 if the certificate is still valid after that period.
Putting it together
A reliable setup has four parts:
- A timer or cron job that runs
certbot renewat least twice a day. - A successful
certbot renew --dry-runafter every change to the server. - A deploy hook that reloads nginx after each renewal.
- An independent expiry check that alerts you before the certificate runs out.
Exercise
Your server's certbot.timer shows a last run yesterday, and certbot certificates reports a certificate with 12 days left. Why is this a problem, and which two commands would you run first to find the cause?
Show answer
Certbot renews a certificate once it has 30 days or fewer left, so a certificate at 12 days means several runs have failed or skipped it. The timer firing is not enough, because the renewal itself can fail. First run sudo certbot renew --dry-run to reproduce the failure and see the error. Then read sudo journalctl -u certbot.service --since "7 days ago" or the end of /var/log/letsencrypt/letsencrypt.log for the cause, such as a blocked port 80 or a changed nginx redirect.
Exercise
After a successful renewal, browsers still show the old certificate's expiry date. The files in /etc/letsencrypt/live/example.com/ are new. What is missing?
Show answer
nginx has not reloaded. It keeps the old certificate in memory until it receives a reload. Add a deploy hook in /etc/letsencrypt/renewal-hooks/deploy/ that runs systemctl reload nginx, or reload manually with sudo nginx -t && sudo systemctl reload nginx.