Sub topic 13
Reloading the Web Server After Renewal with Hooks
This chapter assumes that you can use sudo, nginx already serves your site, and Certbot has created a certificate under /etc/letsencrypt. It also assumes that you understand systemd services and basic shell commands.
Why nginx must reload
Certbot renews certificates by replacing files in /etc/letsencrypt. The fullchain.pem and privkey.pem paths usually remain the same because Certbot maintains them as symbolic links, or symlinks. A symlink is a file that points to another file.
Nginx normally reads the certificate into memory when it starts or reloads. Replacing the certificate file does not automatically make a running nginx process use the new certificate. Nginx must reload its configuration.
A reload starts new worker processes with the updated certificate and lets existing connections finish. It is normally safer than a full restart because it avoids unnecessarily interrupting active connections.
Create a deploy hook
A deploy hook is a command or script that Certbot runs after a certificate has been renewed successfully. It does not run after every ordinary certbot renew check when no renewal was needed.
Create a hook that tests the nginx configuration before reloading it:
sudo tee /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh >/dev/null <<'EOF'
#!/bin/sh
set -eu
/usr/sbin/nginx -t
/bin/systemctl reload nginx
EOF
sudo chmod 755 /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.shThe directory name matters. Files in renewal-hooks/deploy run after a successful renewal. Certbot runs the hook with root privileges, so the script can access the private key and reload the system service.
The nginx -t command checks the complete nginx configuration. If the test fails, the &&-equivalent behavior provided by set -e prevents the reload command from running. This reduces the risk of replacing a working configuration with a broken one.
Test the hook safely
A normal dry run checks the renewal process against the staging service. To include deploy hooks in that test, add --run-deploy-hooks:
sudo certbot renew --dry-run --run-deploy-hooksCheck the result carefully. A successful renewal test should include a successful nginx configuration test and reload. A dry run does not replace the live production certificate.
You can also run the hook directly after checking its contents:
sudo /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.shRun this only when you are ready to reload nginx. If the script fails, inspect the command output and check the service logs:
sudo systemctl status nginx
sudo journalctl -u nginx --since "15 minutes ago"Common errors
| Symptom | Likely cause | Check |
|---|---|---|
| Hook is never run | Wrong directory or missing execute permission | `ls -l /etc/letsencrypt/renewal-hooks/deploy/` |
| Reload fails | Invalid nginx configuration | `sudo nginx -t` |
| Old certificate remains | Nginx was not reloaded | `sudo systemctl reload nginx` |
| Dry run omits the hook | Missing `--run-deploy-hooks` | Repeat the dry run with that option |
Exercise
Why should a deploy hook run nginx -t before systemctl reload nginx?
Show answer
The test detects configuration errors first, so nginx is not asked to load a broken configuration. The reload runs only when the test succeeds.