Automating Let’s Encrypt Renewals With Certbot and systemd Timers

An expired TLS certificate is an outage you scheduled ninety days ago and then forgot about. The fix is not a diary reminder. It is a renewal that runs itself, reloads the web server only when something actually changed, and tells you before it fails rather than after.

Let’s Encrypt certificates are valid for ninety days. Certbot is built to renew them unattended, and on any modern Linux distribution the plumbing to do that is already installed. The work is not writing a renewal script — certbot ships one — it is confirming the timer is live, wiring a deploy hook so nginx picks up the new certificate, proving the whole thing with a dry run, and monitoring days-to-expiry so a silent failure still pages someone. This is the setup I use, and the order I check it in.

Install certbot and obtain the first certificate

Install certbot from your distribution’s package manager or from a snap; both register the renewal timer for you. Then obtain the certificate. The choice that matters here is the challenge type, because it decides how renewal has to run.

Free · 4 minutes

Would you survive contact with a determined attacker — or an auditor?

Fourteen questions on access, patching, detection, and recovery — the basics that prevent most real incidents, and the ones most often assumed rather than verified. Banded finding on screen, full sheet by email.

  • webroot serves the ACME challenge file from a directory your running web server already exposes. Nothing stops, nothing binds a privileged port. This is the default I reach for when nginx is already serving the domain.
  • standalone makes certbot bind port 80 itself. Fine for a box with no web server, useless on one where nginx already holds the port unless you stop it — which defeats the point of unattended renewal.
  • DNS-01 proves control by writing a TXT record. It is the only challenge that issues wildcard certificates, and the only sensible option when the host is not reachable on port 80 from the public internet. It needs a DNS provider plugin and API credentials.

A webroot issuance for a single host looks like this:

# Obtain a certificate without touching the running nginx
sudo certbot certonly \
  --webroot -w /var/www/html \
  -d example.com -d www.example.com \
  --non-interactive --agree-tos -m ops@example.com

I use certonly rather than the --nginx installer because I want to own the nginx configuration and the reload behaviour myself, not have certbot rewrite server blocks. The certificate lands under /etc/letsencrypt/live/example.com/ and certbot records the renewal parameters in /etc/letsencrypt/renewal/example.com.conf, so every future certbot renew repeats exactly these options.

Confirm the systemd timer is actually running

Certbot’s package installs certbot.timer and certbot.service. The timer fires the service twice a day; the service runs certbot renew, which is a no-op for any certificate with more than thirty days of life left and a real renewal for anything inside that window. Do not add a cron job on top of this — you will get two renewal mechanisms racing each other. Confirm the one that ships is enabled:

# Is the timer active, and when does it next fire?
sudo systemctl status certbot.timer
sudo systemctl list-timers certbot.timer

# If a distribution ships it disabled, enable and start it
sudo systemctl enable --now certbot.timer

The randomised delay built into the timer spreads load away from Let’s Encrypt’s servers, which is exactly the kind of behaviour that makes systemd timers a better fit than a plain cron line. I have written before about why I moved scheduled jobs off cron, and unattended certificate renewal is the case that makes the argument on its own.

Reload nginx only when the certificate changed

Renewing the certificate does nothing on its own; nginx keeps serving the old one until it is reloaded. The wrong way to handle this is a blanket reload on every timer run. The right way is a deploy hook, which certbot runs only after a certificate is actually renewed. Drop an executable script into the deploy-hook directory and every renewal, present and future, picks it up:

# /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh
#!/bin/sh
# Runs ONLY after a certificate is renewed.
# certbot sets RENEWED_LINEAGE to the live path of the renewed cert.
set -e

# Validate the config before reloading so a bad file cannot take nginx down
nginx -t
systemctl reload nginx

logger -t certbot-deploy "reloaded nginx for ${RENEWED_LINEAGE}"

Make it executable with chmod +x. Because it lives in renewal-hooks/deploy/, it applies to every certificate on the box without editing any renewal config. The nginx -t check is not optional — a reload against an invalid config is how a renewal turns into the outage it was meant to prevent. The same discipline applies if you are terminating TLS for internal services rather than public ones; the reload target changes but the hook pattern does not, which is worth keeping in mind alongside mutual TLS between internal APIs.

Prove it with a dry run

Never wait for the real renewal to find out whether your hook works. certbot renew --dry-run runs the full renewal against the Let’s Encrypt staging environment — it exercises the challenge and the deploy hook but issues a throwaway certificate that is never installed, so it does not count against rate limits:

# Full rehearsal against staging; confirms the deploy hook fires
sudo certbot renew --dry-run

# Trigger the real timer path manually to test it end to end
sudo systemctl start certbot.service
sudo journalctl -u certbot.service --since "5 min ago"

Watch for the deploy-hook log line. If the dry run renews but you never see nginx reload, the hook is not executable or not in the right directory. Fix that before you rely on it. This is the same principle as building a baseline systemd service and timer you can actually trust: rehearse the failure path before it is live.

Monitor expiry, because silent failure is the real risk

A dry run proves the mechanism works today. It does not protect you against the DNS credential that gets rotated, the webroot that moves, or the rate limit you hit after a bad deploy. The only thing that protects you is monitoring the certificate itself — not the timer, not the logs, the actual served certificate — and alerting on days remaining. Check it from outside the box, so a dead host still fires the alert:

# Days until the served certificate expires; alert under 21
domain=example.com
end=$(echo | openssl s_client -servername "$domain" -connect "$domain:443" 2>/dev/null \
  | openssl x509 -noout -enddate | cut -d= -f2)
expiry=$(date -d "$end" +%s)
days=$(( (expiry - $(date +%s)) / 86400 ))
echo "$domain expires in $days days"
test "$days" -ge 21

Wire that into whatever already pages you and set the threshold well above certbot’s thirty-day renewal window, so an alert means “renewal has been failing for a while” and not “renewal is about to run”. On the rate limits: Let’s Encrypt permits a bounded number of certificates per registered domain per week and a small number of identical certificates per week, so if a broken renewal loop keeps retrying you can lock yourself out of issuance for days. The --dry-run staging path exists precisely so you test without spending that budget.

Set this up once and the ninety-day clock stops being your problem. The certificate renews, nginx reloads only when it should, and the one failure mode left — silent breakage — is the one thing you are now watching for.

Free interactive tool

Website compliance checklist

What your site has to do, based on what it actually does

Answer as much or as little as you like — the list builds as you go. Nothing is stored against your name and no email is required.

Most technology problems are not technology problems. They are control problems.

The systems exist. The investment has been made. The question is whether leadership can understand, direct, evidence, and sustain what those systems produce. Find out where control exists — and where it only appears to.