Automatically Renew Let’s Encrypt Certificates (Nginx)

Posted on August 5, 2019 at 5:38 pm

Here I made a simple bash script that automatically renew all Let’s Encrypt certificates and reload Nginx only once, if at least one certificate was renewed successfully:

#!/bin/sh
 
# ===DESCRIPTION
# This script is used to renew Let's Encrypt certificates via certbot
# And reload Nginx web server only once if at least one certificate was renewed
 
# ===HOW TO RUN THIS SCRIPT EVERY DAY? [METHOD 1]
# Save this script as /root/certbot-renew.sh
# And set execution permissions with chmod +x /root/certbot-renew.sh
# Then add this line on /etc/crontab file:
# 0 0 * * *   root    nohup /root/certbot-renew.sh > /dev/null 2>&1 &
 
# ===HOW TO RUN THIS SCRIPT EVERY DAY? [METHOD 2]
# Save this script as /etc/cron.daily/certbot-renew
# And set correct permissions with chmod 0755 /etc/cron.daily/certbot-renew
 
# ===IMPORTANT
# Make sure to remove the following two files: 
# /etc/cron.d/certbot.dpkg-dist
# /etc/cron.d/certbot
 
RENEW_OUTPUT=$(/usr/bin/certbot renew 2>&1 | /bin/grep 'The following certs have been renewed')
 
if [ ! -z "$RENEW_OUTPUT" ]
then
    echo "Reloading Nginx web server..."
 
    /etc/init.d/nginx reload
else
    echo "No action taken..."
fi

Updated on August 11, 2019 at 12:19 pm

Receive updates via email

Other Posts

Updated Posts