Skip to content

Instantly share code, notes, and snippets.

@angeloreale
Last active August 17, 2019 05:03
Show Gist options
  • Save angeloreale/0cc08ad4ed390e922652a60b6327480a to your computer and use it in GitHub Desktop.
Save angeloreale/0cc08ad4ed390e922652a60b6327480a to your computer and use it in GitHub Desktop.
Dockerizing Certbot and automatically renewing Let’s Encrypt certificates with Shell Script and Crontab
### certbot-autorenewal.sh
#!/bin/bash
cd /home/{your-docker-compose-folder}
echo "--------------- RENEWING CERTS ---------------"
docker-compose up certbot-site1.com
docker-compose up certbot-site2.com
etc...
echo "--------------- RESTARTING NGINX ---------------"
service nginx restart
### sudo crontab
# make script executable via bash
cd /path/to/script
sudo chmod +x ./certbot-autorenewal.sh
# open sudo crontab from bash
sudo crontab -e
# add the following line to the bottom
0 0 10 * * /bin/bash /home/{path-to-shell-script}/certbot-autorenewal.sh >> /home/{crontab-logs-path}/crontab.log
### docker-compose.yml
services:
certbot-site1.com:
container_name: certbot-site1.com
image: certbot/certbot
volumes:
- ./certs/etc/letsencrypt/:/etc/letsencrypt/
- ./sites/site1/:/var/www/site1.com
command: certonly --webroot --cert-name site1.admin.com --email adm@site1.com --agree-tos --no-eff-email --renew-by-default -w /var/www/site1.com/{webroot} -d site1.com,www.site1.com
certbot-site2.com:
container_name: certbot-site2.com
image: certbot/certbot
volumes:
- ./certs/etc/letsencrypt/:/etc/letsencrypt/
- ./sites/site2.com/:/var/www/site2.com
command: certonly --webroot --cert-name site2.admin.com --email adm@site2.com --agree-tos --no-eff-email --renew-by-default -w /var/www/site2.com -d site2.com,www.site2.com
etc...
--------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment