Sub topic 13
Reloading the web server after renewal with hooks
This chapter assumes you already have a working certificate from certbot certonly --nginx, an nginx server block that points at /etc/letsencrypt/live/example.com/, and a renewal timer you can see in systemctl list-timers. You should also know basic systemd and shell scripting.
Why renewal is not finished when the file changes
nginx reads certificate and key files once, when it starts or reloads. Its worker processes keep the loaded certificate in memory. When Certbot renews a certificate, it writes new files and repoints the symlinks in /etc/letsencrypt/live/example.com/. A running nginx knows nothing about this and keeps serving the old certificate until it expires.
The result is a failure that appears about 90 days after setup. Renewal succeeds and the logs look clean, yet browsers report an expired certificate. The fix is to make nginx reload after every successful renewal. Certbot does this with hooks.
If you use certbot --nginx (the installer mode), Certbot reloads nginx itself. With certonly, as in this tutorial, it only obtains the certificate and you must add the reload yourself.
The three kinds of hooks
A hook is a command that Certbot runs at a defined point in certbot renew.
| Hook | Runs | Typical use |
|---|---|---|
| pre | Before any renewal attempt, once per `renew` run, only if at least one certificate is due | Stop a service that holds port 80 |
| deploy | After each certificate that renewed successfully | Reload the web server |
| post | After all renewal attempts, once per run, even if some failed | Start a service you stopped in pre |
Pre and post hooks run once per invocation. A deploy hook runs once per renewed certificate. If a run finds nothing due for renewal, none of the hooks run.
Placing hooks in the renewal-hooks directories
Certbot scans three directories and runs every executable file in each one:
/etc/letsencrypt/renewal-hooks/pre//etc/letsencrypt/renewal-hooks/deploy//etc/letsencrypt/renewal-hooks/post/
Files run in alphabetical order. A file that is not executable is skipped, so a missing chmod +x is a common cause of a hook that never fires. Hooks run as the user that runs Certbot, which is root under the systemd timer.
This approach applies to all certificates on the machine. It is usually better than putting --deploy-hook on the command line, because the hook does not depend on how a certificate was first requested.
Here is the deploy hook from the earlier setup, with a safety check added:
sudo tee /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh >/dev/null <<'EOF'
#!/bin/sh
set -eu
# Refuse to reload if the configuration is broken.
nginx -t
systemctl reload nginx
logger -t certbot-deploy "Reloaded nginx after renewing: ${RENEWED_DOMAINS:-unknown}"
EOF
sudo chmod +x /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.shThree details matter here:
set -eustops the script at the first failing command and on unset variables.nginx -ttests the configuration. If another admin left a typo in a different file, the reload is skipped, and the running nginx keeps working on its old configuration.reloadasks nginx to start new workers with the new certificate and let the old workers finish their open connections.restartdrops those connections, so avoid it for this purpose.
Variables that Certbot passes to a deploy hook
Certbot sets two environment variables before it runs a deploy hook:
RENEWED_LINEAGEis the path to the live directory of the renewed certificate, for example/etc/letsencrypt/live/example.com.RENEWED_DOMAINSis a space-separated list of the names on that certificate.
You need these when one script serves several certificates. For example, you can copy a certificate to another service only when its lineage matches:
sudo tee /etc/letsencrypt/renewal-hooks/deploy/10-copy-for-mail.sh >/dev/null <<'EOF'
#!/bin/sh
set -eu
case "$RENEWED_LINEAGE" in
/etc/letsencrypt/live/mail.example.com)
install -m 0640 -o root -g ssl-cert \
"$RENEWED_LINEAGE/fullchain.pem" /etc/ssl/mail/fullchain.pem
install -m 0640 -o root -g ssl-cert \
"$RENEWED_LINEAGE/privkey.pem" /etc/ssl/mail/privkey.pem
systemctl reload postfix
;;
esac
EOF
sudo chmod +x /etc/letsencrypt/renewal-hooks/deploy/10-copy-for-mail.shThe 10- prefix makes this script run before reload-nginx.sh. Copying a key is also a place where permissions go wrong, so review the key protection chapter before you do it for real.
When to use pre and post hooks
If you obtained a certificate with the standalone authenticator, Certbot starts its own temporary web server on port 80. That fails while nginx holds the port. Pre and post hooks solve this by stopping nginx and then starting it again:
sudo tee /etc/letsencrypt/renewal-hooks/pre/stop-nginx.sh >/dev/null <<'EOF'
#!/bin/sh
systemctl stop nginx
EOF
sudo tee /etc/letsencrypt/renewal-hooks/post/start-nginx.sh >/dev/null <<'EOF'
#!/bin/sh
systemctl start nginx
EOF
sudo chmod +x /etc/letsencrypt/renewal-hooks/pre/stop-nginx.sh \
/etc/letsencrypt/renewal-hooks/post/start-nginx.shThe post hook runs even when renewal fails, so nginx comes back either way. You do not need these hooks with the nginx plugin, because it answers the HTTP-01 challenge through the running server. Stopping the site for every renewal causes a short outage, so prefer the plugin or the webroot method when you can.
The order of events
Testing hooks without waiting 60 days
A plain certbot renew --dry-run is not enough to test the deploy hook. A dry run uses the staging server, and Certbot skips deploy hooks in that mode. It does run pre and post hooks. You have two ways to test the reload logic.
The first is to run the script by hand with the variables set as Certbot would set them:
sudo RENEWED_LINEAGE=/etc/letsencrypt/live/example.com \
RENEWED_DOMAINS="example.com www.example.com" \
/etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh
journalctl -t certbot-deploy -n 1The second is to force a real renewal of a certificate that you obtained from staging:
sudo certbot renew --force-renewal --cert-name staging-test.example.comDo not use --force-renewal on production certificates as a routine test. Each forced renewal counts against Let's Encrypt rate limits.
Hooks given on the command line
You can also pass hooks as options:
sudo certbot certonly --nginx -d example.com -d www.example.com \
--deploy-hook "systemctl reload nginx"Certbot stores the option in /etc/letsencrypt/renewal/example.com.conf, so it applies to later renewals of that certificate only. Note that this and the directory scripts can both exist. In that case both run, and nginx reloads twice. That is harmless, but it makes the logs confusing, so choose one method.
Failure modes
- The hook is not executable. Certbot skips it silently. Check with
ls -l /etc/letsencrypt/renewal-hooks/deploy/. - The shebang line is missing or wrong. The kernel cannot run the file. Start every script with
#!/bin/sh. nginx -tfails. The script exits before the reload, and nginx keeps serving the old certificate. Read the Certbot log in/var/log/letsencrypt/letsencrypt.log, which records hook output and exit codes.- Hook uses a command that is not in the PATH. Under systemd the PATH is short. Use full paths such as
/usr/sbin/nginxif a command is not found. - The reload works but the site still shows the old certificate. Another server block, or a different service such as a load balancer, terminates TLS. Check which process owns port 443 with
sudo ss -ltnp 'sport = :443'.
Always confirm the result from outside, using the openssl s_client command from the verification chapter. The validity dates it prints are the proof that the new certificate is live.
Exercise
A server uses certbot certonly --nginx. Renewal logs show success every 60 days, but users see an expired certificate on day 90. The reload-nginx.sh file exists in /etc/letsencrypt/renewal-hooks/deploy/. Name two likely causes and a command that checks each.
Show answer
One cause is that the script is not executable, so Certbot skips it. Check with ls -l /etc/letsencrypt/renewal-hooks/deploy/ and look for the x bits. The other is that the script runs but exits early, for example because nginx -t fails. Check with sudo grep -i hook /var/log/letsencrypt/letsencrypt.log for the hook's output and exit code, and run sudo nginx -t yourself. A third possibility is that nginx is not the process terminating TLS, which sudo ss -ltnp 'sport = :443' would show.
Exercise
Why does certbot renew --dry-run not prove that your deploy hook works, and how can you test it safely?
Show answer
In dry-run mode Certbot skips deploy hooks, although it runs pre and post hooks. To test safely, run the hook script by hand with RENEWED_LINEAGE and RENEWED_DOMAINS set, or force-renew a staging certificate with certbot renew --force-renewal --cert-name NAME. Do not force-renew production certificates as a test, because that uses rate-limit quota.