Skip to content

Instantly share code, notes, and snippets.

@bendechrai
Created July 11, 2016 01:33
Show Gist options
  • Save bendechrai/79e10d85d056274c989ebf4d6c4d0b88 to your computer and use it in GitHub Desktop.
Save bendechrai/79e10d85d056274c989ebf4d6c4d0b88 to your computer and use it in GitHub Desktop.
LetsEncrypt Nginx Renew
#!/bin/bash
for meta in /etc/letsencrypt/renewal/*conf
do
DOMAIN=`basename $meta | sed 's,.conf$,,'`
# Does the domain appear as a servername in any config file
if [ $(grep -E "^[ \t]*server_name\W+(.*[ \t])?$DOMAIN[ \t;]" /etc/nginx/sites-enabled/*| wc -l) -gt 0 ]
then
# Get the cert file and calculate days remaining
CERTFILE=`cat $meta | grep -E '^cert = ' | sed 's,^cert = ,,'`
EXPIRES=`openssl x509 -in $CERTFILE -noout -enddate | awk -F "=" '{print $2}'`
EXPIRES=`date +%s -d "$EXPIRES"`
NOW=`date +%s`
REMAINING=$(($EXPIRES - $NOW))
REMAINING=$(($REMAINING / 60 / 60 / 24))
# If due in less than 5 days, renew
if [ $REMAINING -lt 5 ]
then
WEBROOT=`cat $meta | grep -E '^webroot_path = ' | sed 's,^webroot_path = ,,' | sed 's/,//'`
~/letsencrypt/letsencrypt-auto certonly --webroot -w $WEBROOT -d $DOMAIN
fi
fi
done
nginx -t && service nginx reload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment