Hey, I was busy with some issues, so I wasn’t able to be active in online spaces. I’m sharing the final tailored draft I documented for my personal use. Please let me know if something doesn’t seem to be on point or needs explanation.
I will also attach the workflow image if possible.
for dev and prod we have different configuration which is used depending on the environment.
Issues arises when we are deploying the project to production and server(nginx) is booting for the first time inside the docker. We can’t use prod configuration as it require updating some variables and path such as ssl(TLS) cert path, which isn’t the big problem as such because we can do it anyway and refresh the nginx once we are done with acme challenges but that would be just quick fix than doing it right way and will be creating too many loose ends.
fixing it
With some testing and looking here and there came up with
While booting/creating nginx container use nginx/manager.sh which verify if certificate is present, if not load the nginx.dev.conf.
Nginx container is up and running.
Certbot container is created and managed by certbot-init.sh
#!/bin/shset -e
echo"Getting certificate..."
certbot certonly \
--webroot \
--webroot-path "/vol/www/" \
-d "$DOMAIN" \
--email $EMAIL \
--rsa-key-size 4096 \
--agree-tos \
--noninteractive
if [ $? -ne 0 ]; thenecho"Certbot encountered an error. Exiting."exit 1
fi#for copying the certificate and configuration to the volumeif [ -f "/etc/letsencrypt/live/${DOMAIN}/fullchain.pem" ]; thenecho"SSL cert exists, enabling HTTPS..."
envsubst '${DOMAIN}' < /etc/nginx/nginx.prod.conf > /etc/nginx/conf.d/default.conf
elseecho"Certbot unable to get SSL cert,server HTTP only..."fiecho"Setting up auto-renewal..."
apk add --no-cache dcron
echo"0 12 * * * certbot renew --quiet" | crontab -
crond -f
and acme challenges is completed.
New certificate is placed at "/etc/letsencrypt/live/${DOMAIN}/fullchain.pem".
Production conf( nginx.prod.conf ) is loaded as
Meanwhile this whole operation is monitored by a script which is setup during the creation of nginx container. This script make sure that nginx refresh when ever there is changes in the configuration file.
Hey, I was busy with some issues, so I wasn’t able to be active in online spaces. I’m sharing the final tailored draft I documented for my personal use. Please let me know if something doesn’t seem to be on point or needs explanation.
I will also attach the workflow image if possible.
for dev and prod we have different configuration which is used depending on the environment.
for dev
for prod we use configuration
So what’s the issue ?
Issues arises when we are deploying the project to production and server(nginx) is booting for the first time inside the docker. We can’t use prod configuration as it require updating some variables and path such as ssl(TLS) cert path, which isn’t the big problem as such because we can do it anyway and refresh the nginx once we are done with acme challenges but that would be just quick fix than doing it right way and will be creating too many loose ends.
fixing it
With some testing and looking here and there came up with
nginx/manager.sh
which verify if certificate is present, if not load thenginx.dev.conf
.certbot-init.sh
#!/bin/sh set -e echo "Getting certificate..." certbot certonly \ --webroot \ --webroot-path "/vol/www/" \ -d "$DOMAIN" \ --email $EMAIL \ --rsa-key-size 4096 \ --agree-tos \ --noninteractive if [ $? -ne 0 ]; then echo "Certbot encountered an error. Exiting." exit 1 fi #for copying the certificate and configuration to the volume if [ -f "/etc/letsencrypt/live/${DOMAIN}/fullchain.pem" ]; then echo "SSL cert exists, enabling HTTPS..." envsubst '${DOMAIN}' < /etc/nginx/nginx.prod.conf > /etc/nginx/conf.d/default.conf else echo "Certbot unable to get SSL cert,server HTTP only..." fi echo "Setting up auto-renewal..." apk add --no-cache dcron echo "0 12 * * * certbot renew --quiet" | crontab - crond -f
"/etc/letsencrypt/live/${DOMAIN}/fullchain.pem"
.nginx.prod.conf
) is loaded asinotifywait -m -r -e modify,move,close_write /etc/nginx/conf.d/default.conf /etc/letsencrypt | while read path action file; do echo "Change detected in $file: $action" echo "Reloading nginx!" nginx -s reload done &
Auto-renew the certificate
Auto-renew is easy to setup we just mash up a cron job and a certification change detection script along with
nginx/manager.sh
andcertbot-init.sh
.nginx/manager.sh
inotifywait -m -r -e modify,move,close_write /etc/nginx/conf.d/default.conf /etc/letsencrypt | #--> '/etc/letsencrypt' for certificate renew while read path action file; do echo "Change detected in $file: $action" echo "Reloading nginx!" nginx -s reload done &
certbot-init.sh
echo "Setting up auto-renewal..." apk add --no-cache dcron echo "0 12 * * * certbot renew --quiet" | crontab - crond -f