Skip to content

Instantly share code, notes, and snippets.

@cgtarmenta
Last active January 21, 2023 11:25
Show Gist options
  • Save cgtarmenta/f48ca1e8654bad9383af5f61169e0195 to your computer and use it in GitHub Desktop.
Save cgtarmenta/f48ca1e8654bad9383af5f61169e0195 to your computer and use it in GitHub Desktop.
A simple cloudflare-LetsEncrypt auto issue/renew script
#!/bin/bash
# Setting variables to run
DOMAIN="exampledomain.com"
CLOUDFLAREEMAIL="clouflare.account@email.com"
CLOUDFLAREAPIKEY="0192837465918234650192837465"
RANDOMLEVEL4=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 6 | head -n 1)
# Verifying script is run as root
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit
fi
#Verifying script runns on /root
if [ "$PWD" != "/root" ]
then echo "Please run on /root"
exit
fi
# make some cleaning for previous testings
rm cloudflare.ini renewcert 2>&1 >/dev/null
# Verifying certbot is in the system
certbot --version 2>&1 >/dev/null # improvement by tripleee
CERBOT_IS_AVAILABLE=$?
if [ $CERBOT_IS_AVAILABLE -ne 0 ]; then
echo "Certbot is not installed. Installing it..."
# Installing certbot
apt update
apt install -y nginx certbot python3-certbot-nginx python3-certbot-dns-cloudflare
fi
echo "dns_cloudflare_email = "$CLOUDFLAREEMAIL > cloudflare.ini
echo "dns_cloudflare_api_key = "$CLOUDFLAREAPIKEY >> cloudflare.ini
chmod 600 cloudflare.ini
echo "#!/bin/bash" > renewcert
echo "source /root/certbot/venv/bin/activate" >> renewcert
echo "certbot renew" >> renewcert
chmod +x renewcert
ln /root/renewcert /etc/cron.weekly/renewcert
certbot certonly \
--agree-tos --email $CLOUDFLAREEMAIL --noninteractive \
--server "https://acme-v02.api.letsencrypt.org/directory" \
--dns-cloudflare \
--dns-cloudflare-propagation-seconds 60 \
--dns-cloudflare-credentials "/root/cloudflare.ini" \
-d $DOMAIN -d "*."$DOMAIN -d $RANDOMLEVEL4".discard."$DOMAIN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment