Skip to content

Instantly share code, notes, and snippets.

@simonjenny
Last active August 12, 2021 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonjenny/f0eceba96c10086534cc338f1b7e7566 to your computer and use it in GitHub Desktop.
Save simonjenny/f0eceba96c10086534cc338f1b7e7566 to your computer and use it in GitHub Desktop.
Automagically create Letsencrypt Certificates for Nginx Virtual Hosts
#!/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