Last active
August 12, 2021 08:49
-
-
Save simonjenny/f0eceba96c10086534cc338f1b7e7566 to your computer and use it in GitHub Desktop.
Automagically create Letsencrypt Certificates for Nginx Virtual Hosts
This file contains 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 | |
# Name your config Files like this : DOMAIN.conf eg. example.com.conf | |
IP=$(/sbin/ip -o -4 addr list eth0 | awk '{print $4}' | cut -d/ -f1) | |
for filename in /etc/nginx/sites-enabled/*.conf; do | |
if ! grep -q "Certbot" $filename; then | |
CONF=$(echo "$filename" | cut -d'/' -f5) | |
DOMAIN=${CONF%.conf*} | |
if [[ $(dig +short a "$DOMAIN") == "$IP" ]]; then | |
logger -s "Installing SSL Cert for $DOMAIN" | |
certbot -n --redirect --agree-tos --nginx -d "$DOMAIN" -d "www.$DOMAIN" | |
sed -i "s/443 ssl;/443 ssl http2;/" $filename | |
else | |
echo "Ignoring $DOMAIN not pointing to $IP" | |
fi | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment