How to Auto Renew SSL Certificate in Nginx Easily
To auto renew SSL certificates in
nginx, use Certbot with its built-in renewal command and set up a cron job or systemd timer to run certbot renew regularly. After renewal, reload nginx to apply the new certificate without downtime.Syntax
The main command to renew SSL certificates automatically is certbot renew. This command checks all installed certificates and renews those expiring soon.
To apply the renewed certificate, reload Nginx with nginx -s reload or systemctl reload nginx.
To automate this, schedule the renewal command in a cron job or systemd timer.
bash
certbot renew systemctl reload nginx
Example
This example shows how to create a cron job that runs twice daily to renew certificates and reload Nginx if renewal happens.
bash
0 0,12 * * * root certbot renew --quiet --post-hook "systemctl reload nginx"
Output
No output if certificates are valid; renews and reloads nginx if certificates are near expiry.
Common Pitfalls
- Not reloading Nginx after renewal causes the server to keep using the old certificate.
- Running
certbot renewwithout root or proper permissions can fail silently. - Forgetting to set up automation means certificates expire and cause HTTPS errors.
- Using
--post-hookincorrectly or missing it can prevent Nginx from updating.
bash
Wrong: 0 0 * * * certbot renew Right: 0 0 * * * certbot renew --quiet --post-hook "systemctl reload nginx"
Quick Reference
Summary tips for auto renewing SSL certificates with Nginx:
- Use
certbot renewto check and renew certificates. - Automate renewal with cron or systemd timers.
- Always reload Nginx after renewal to apply new certificates.
- Run renewal commands with root or sudo privileges.
- Check logs if renewal fails:
/var/log/letsencrypt/letsencrypt.log.
Key Takeaways
Use 'certbot renew' to automatically renew SSL certificates for Nginx.
Schedule renewal commands with cron or systemd timers for automation.
Reload Nginx after renewal to apply the updated certificates.
Run renewal commands with proper permissions to avoid failures.
Monitor renewal logs to troubleshoot any issues.