I have this repository that will basically automatically create SSL certificates for your domains using Nginx and Certbot to handler this.
The main script (project/ssl.sh
inside repository) basically:
- Create temporary certificates to be able to up the Nginx container:
docker compose run --rm --entrypoint " \
openssl req -x509 -nodes -newkey rsa:$RSA_KEY_SIZE -days 1 \
-keyout '$DOMAIN_PATH/privkey.pem' \
-out '$DOMAIN_PATH/fullchain.pem' \
-subj '/CN=#!COMMONNAME!#' \
" certbot
- Start the Nginx container:
docker compose up -d --build --force-recreate nginx
- Remove the temporary certificate folders:
rm -rf "$CERTBOT_PATH/live/$domain"
rm -rf "$CERTBOT_PATH/archive/$domain"
rm -rf "$CERTBOT_PATH/renewal/$domain.conf"
- Now with Nginx up and running, request Let's Encrypt certificates:
docker compose run --rm --entrypoint " \
certbot certonly --webroot -w /var/www/certbot \
--rsa-key-size $RSA_KEY_SIZE \
--agree-tos \
--force-renewal \
$DOMAIN_ARGS \
${IS_STAGING:+--staging} \
`[ "$EMAIL" ] && echo "--email $EMAIL" || echo '--register-unsafely-without-email'` \
" certbot
However, step 4 of this walkthrough fails and Let's Encrypt is unable to recover the challenge file.
After a LONG time trying to understand and debug the problem I understood that what is happening is: although Nginx can see the certificate files (that's why the container is up) it cannot read them... what it does is return permission error (as the certificates are from root):
[error] 20#20: *73 cannot load certificate key "/etc/letsencrypt/live/domain.xyz/privkey.pem": BIO_new_file() failed (SSL: error:8000000D:system library::Permission denied:calling fopen(/etc/letsencrypt/live/domain.xyz/privkey.pem, r) error:10080002:BIO routines::system lib) while SSL handshaking, client: 123.456.789.010, server: 0.0.0.0:443
From what I understand, this occurs because the certificates generated in step 1 and step 4 are generated by certbot which runs from root.
In fact, if I log into the container that is running Nginx, as I am normally root, in addition to seeing, I can read the contents of the certificates, however, when I log in with the nginx user, I see but do not read the files.
Someone have some ideia to help me?
PS: Guys, I really don't know if this is the right forum to ask, so if it's not, I'm sorry!
PPS: My project is inspired by several forums but mainly from this tutorial.