Created
July 11, 2016 01:33
-
-
Save bendechrai/79e10d85d056274c989ebf4d6c4d0b88 to your computer and use it in GitHub Desktop.
LetsEncrypt Nginx Renew
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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