2021-05-30 12:40:12 +00:00
|
|
|
#!/usr/bin/env bash
|
2024-02-07 15:21:14 +00:00
|
|
|
if [[ -z ${LETSENCRYPT_DOMAINS} ]]; then
|
|
|
|
|
echo "LetsEncrypt not enabled"
|
|
|
|
|
while true; do
|
|
|
|
|
sleep infinity
|
|
|
|
|
done
|
2021-05-30 12:40:12 +00:00
|
|
|
fi
|
|
|
|
|
|
2024-02-07 15:21:14 +00:00
|
|
|
if [[ -z ${LETSENCRYPT_EMAIL} ]]; then
|
|
|
|
|
echo "LetsEncrypt not enabled - You must set LETSENCRYPT_EMAIL"
|
|
|
|
|
while true; do
|
|
|
|
|
sleep infinity
|
|
|
|
|
done
|
2021-05-30 12:40:12 +00:00
|
|
|
fi
|
|
|
|
|
|
2024-02-07 15:21:14 +00:00
|
|
|
if [[ ${LETSENCRYPT_MODE,,} == "production" ]]; then
|
|
|
|
|
echo -e "LetsEncrypt is running against the \e[32mPRODUCTION\e[0m servers."
|
|
|
|
|
LETSENCRYPT_MODE=""
|
2021-05-30 12:40:12 +00:00
|
|
|
else
|
2024-02-07 15:21:14 +00:00
|
|
|
echo -e "LetsEncrypt is running against the \e[31mSTAGING\e[0m servers."
|
|
|
|
|
LETSENCRYPT_MODE="--test-cert"
|
2021-05-30 12:40:12 +00:00
|
|
|
fi
|
2021-05-30 17:05:44 +00:00
|
|
|
echo -e "To change this, change the value of LETSENCRYPT_MODE"
|
2021-05-30 12:40:12 +00:00
|
|
|
|
2021-05-30 17:10:47 +00:00
|
|
|
# Give Nginx a moment to start before we kill it again.
|
2024-02-07 15:21:14 +00:00
|
|
|
sleep 30
|
2021-05-30 17:09:16 +00:00
|
|
|
|
2021-05-30 19:25:14 +00:00
|
|
|
echo -e "Certbot is running for \e[33m${LETSENCRYPT_EMAIL}\e[0m / \e[33m${LETSENCRYPT_DOMAINS}\e[0m..."
|
2024-02-07 15:21:14 +00:00
|
|
|
(
|
|
|
|
|
set -x
|
|
|
|
|
certbot \
|
|
|
|
|
certonly \
|
|
|
|
|
--nginx \
|
|
|
|
|
"${LETSENCRYPT_MODE}" \
|
|
|
|
|
-d "${LETSENCRYPT_DOMAINS}" \
|
|
|
|
|
-n \
|
|
|
|
|
-m "${LETSENCRYPT_EMAIL}" \
|
|
|
|
|
--agree-tos
|
2021-05-30 18:39:40 +00:00
|
|
|
)
|
|
|
|
|
|
2021-05-30 19:25:14 +00:00
|
|
|
echo -e "Certbot complete!"
|
2021-05-30 17:12:37 +00:00
|
|
|
|
|
|
|
|
# replace the self-certs with these lovely new certs.
|
2024-02-07 15:21:14 +00:00
|
|
|
if [[ -f "/etc/letsencrypt/live/${LETSENCRYPT_DOMAINS}/fullchain.pem" ]]; then
|
|
|
|
|
sed -i "s|ssl_certificate .*|ssl_certificate /etc/letsencrypt/live/${LETSENCRYPT_DOMAINS}/fullchain.pem;|g" /etc/nginx/sites-enabled/default-ssl
|
|
|
|
|
sed -i "s|ssl_certificate_key .*|ssl_certificate_key /etc/letsencrypt/live/${LETSENCRYPT_DOMAINS}/privkey.pem;|g" /etc/nginx/sites-enabled/default-ssl
|
2021-05-30 17:12:37 +00:00
|
|
|
|
2024-02-07 15:21:14 +00:00
|
|
|
echo "Reloading Nginx"
|
|
|
|
|
nginx -s reload
|
|
|
|
|
# Sleep for 24 hours and try again tomorrow with a renewal, just in case.
|
|
|
|
|
sleep 86400
|
2021-05-30 18:42:18 +00:00
|
|
|
else
|
2024-02-07 15:21:14 +00:00
|
|
|
echo -e "LetsEncrypt \e[31mFAILED TO GENERATE CERTS\e[0m. Will try again in an hour."
|
|
|
|
|
sleep 3600
|
|
|
|
|
fi
|