Skip to content

Instantly share code, notes, and snippets.

@sahilsk
Created January 20, 2016 14:07
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save sahilsk/d23122a3c103b82b53ec to your computer and use it in GitHub Desktop.
Save sahilsk/d23122a3c103b82b53ec to your computer and use it in GitHub Desktop.
LetsEncrypt auto-renew script
#!/bin/bash
#### Source : https://vincent.composieux.fr/article/install-configure-and-automatically-renew-let-s-encrypt-ssl-certificate
WEB_SERVICE='nginx'
CONFIG_FILE='/usr/local/etc/le-yourdomain-webroot.ini'
LE_PATH='/opt/letsencrypt'
EXP_LIMIT=30;
if [ ! -f $CONFIG_FILE ]; then
echo "[ERROR] config file does not exist: $CONFIG_FILE"
exit 1;
fi
DOMAIN=`grep "^\s*domains" $CONFIG_FILE | sed "s/^\s*domains\s*=\s*//" | sed 's/(\s*)\|,.*$//'`
CERT_FILE="/etc/letsencrypt/live/$DOMAIN/fullchain.pem"
if [ ! -f $CERT_FILE ]; then
echo "[ERROR] certificate file not found for domain $DOMAIN."
fi
DATE_NOW=$(date -d "now" +%s)
EXP_DATE=$(date -d "`openssl x509 -in $CERT_FILE -text -noout | grep "Not After" | cut -c 25-`" +%s)
EXP_DAYS=$(echo \( $EXP_DATE - $DATE_NOW \) / 86400 |bc)
echo "Checking expiration date for $DOMAIN..."
if [ "$EXP_DAYS" -gt "$EXP_LIMIT" ] ; then
echo "The certificate is up to date, no need for renewal ($EXP_LIMIT days left)."
exit 0;
else
echo "The certificate for $DOMAIN is about to expire soon. Starting webroot renewal script..."
$LE_PATH/letsencrypt-auto certonly --renew-by-default --config $CONFIG_FILE
echo "Reloading $WEB_SERVICE"
/usr/sbin/service $WEB_SERVICE reload
echo "Renewal process finished for domain $DOMAIN"
exit 0;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment