make LE work

This commit is contained in:
Matthew Hodgson
2024-11-08 18:01:09 +00:00
parent 71f75417ac
commit fe2192166e
3 changed files with 44 additions and 38 deletions

View File

@@ -92,7 +92,13 @@ services:
volumes: volumes:
- ${VOLUME_PATH}/data/certbot/conf:/etc/letsencrypt - ${VOLUME_PATH}/data/certbot/conf:/etc/letsencrypt
- ${VOLUME_PATH}/data/certbot/www:/var/www/certbot - ${VOLUME_PATH}/data/certbot/www:/var/www/certbot
entrypoint: "/bin/sh -c 'trap exit TERM; while [ -e /etc/letsencrypt/live ]; do certbot renew; sleep 12h & wait $${!}; done;'" - ${VOLUME_PATH}/data/ssl:/data/ssl
entrypoint: "/bin/sh -c 'trap exit TERM; \
while [ -e /etc/letsencrypt/live ]; \
do certbot --webroot -w /var/www/certbot renew; \
cp /etc/letsencrypt/live/$DOMAIN/*.pem /data/ssl; \
sleep 12h & wait $${!}; \
done;'"
postgres: postgres:
image: postgres:latest image: postgres:latest

View File

@@ -5,7 +5,7 @@
set -x set -x
if ! [ -x "$(command -v docker)" ]; then if ! [ -x "$(command -v docker)" ]; then
echo 'Error: dockeris not installed.' >&2 echo 'Error: docker is not installed.' >&2
exit 1 exit 1
fi fi
@@ -14,7 +14,7 @@ domains=$DOMAINS
rsa_key_size=4096 rsa_key_size=4096
data_path="./data/certbot" data_path="./data/certbot"
read -p "admin email address for letsencrypt: " email read -p "admin email address for letsencrypt: " email
staging=1 # Set to 1 if you're testing your setup to avoid hitting request limits staging=0 # Set to 1 if you're testing your setup to avoid hitting request limits
if [ -d "$data_path" ]; then if [ -d "$data_path" ]; then
read -p "Existing data found for $domains. Continue and replace existing certificate? (y/N) " decision read -p "Existing data found for $domains. Continue and replace existing certificate? (y/N) " decision
@@ -31,31 +31,30 @@ if [ ! -e "data/ssl/options-ssl-nginx.conf" ] || [ ! -e "data/ssl/ssl-dhparams.p
echo echo
fi fi
echo "### Creating dummy certificate for $domains ..." # echo "### Creating dummy certificate for $domains ..."
# N.B. in bash, $domains will return the first value of the array in string context, so this is not a bug: # # N.B. in bash, $domains will return the first value of the array in string context, so this is not a bug:
path="/etc/letsencrypt/live/$domains" # mkdir -p "$data_path/conf/live/$domains"
mkdir -p "$data_path/conf/live/$domains" # path="/etc/letsencrypt/live/$domains"
docker compose run --rm --entrypoint "\ # docker compose run --rm --entrypoint "\
openssl req -x509 -nodes -newkey rsa:$rsa_key_size -days 1\ # openssl req -x509 -nodes -newkey rsa:$rsa_key_size -days 1\
-keyout '$path/privkey.pem' \ # -keyout '$path/privkey.pem' \
-out '$path/fullchain.pem' \ # -out '$path/fullchain.pem' \
-subj '/CN=localhost'; \ # -subj '/CN=localhost'; \
cp /etc/ssl/certs/ca-certificates.crt '$path'" certbot # cp /etc/ssl/certs/ca-certificates.crt '$path'" certbot
echo # echo
#
echo "### Starting nginx ..." # echo "### Starting nginx ..."
docker compose up --force-recreate -d nginx # docker compose up --force-recreate -d nginx
echo # echo
#
exit # exit
#
echo "### Deleting dummy certificate for $domains ..." # echo "### Deleting dummy certificate for $domains ..."
docker compose run --rm --entrypoint "\ # docker compose run --rm --entrypoint "\
rm -Rf /etc/letsencrypt/live/$domains && \ # rm -Rf /etc/letsencrypt/live/$domains && \
rm -Rf /etc/letsencrypt/archive/$domains && \ # rm -Rf /etc/letsencrypt/archive/$domains && \
rm -Rf /etc/letsencrypt/renewal/$domains.conf" certbot # rm -Rf /etc/letsencrypt/renewal/$domains.conf" certbot
echo # echo
echo "### Requesting Let's Encrypt certificate for $domains ..." echo "### Requesting Let's Encrypt certificate for $domains ..."
#Join $domains to -d args #Join $domains to -d args
@@ -73,15 +72,19 @@ esac
# Enable staging mode if needed # Enable staging mode if needed
if [ $staging != "0" ]; then staging_arg="--staging"; fi if [ $staging != "0" ]; then staging_arg="--staging"; fi
docker compose run --rm --entrypoint "\ docker compose run -p 80:80 --rm --entrypoint " \
certbot certonly --webroot -w /var/www/certbot \ sh -c \"certbot certonly --standalone \
$staging_arg \ $staging_arg \
$email_arg \ $email_arg \
$domain_args \ $domain_args \
--rsa-key-size $rsa_key_size \ --rsa-key-size $rsa_key_size \
--agree-tos \ --agree-tos \
--force-renewal" certbot --force-renewal;
cp /etc/letsencrypt/live/$domains/*.pem /data/ssl; \
cp /etc/ssl/certs/ca-certificates.crt /data/ssl; \
chown -R $USER_ID:$GROUP_ID /data/ssl; \
\"" certbot
echo echo
echo "### Reloading nginx ..." # echo "### Reloading nginx ..."
docker compose exec nginx nginx -s reload # docker compose exec nginx nginx -s reload

View File

@@ -44,12 +44,9 @@ if [[ ! -e .env ]]; then
else else
read -p "Use letsencrypt for SSL? [y/n] " use_letsencrypt read -p "Use letsencrypt for SSL? [y/n] " use_letsencrypt
if [[ use_letsencrypt =~ [Yy] ]]; then if [[ use_letsencrypt =~ [Yy] ]]; then
mkdir -p data/certbot/conf/live/$DOMAIN mkdir -p data/ssl
if [[ ! -L data/ssl ]]; then
ln -s ../data/certbot/conf/live/$DOMAIN data/ssl
fi
touch data/ssl/ca-certificates.crt # will get overwritten by init-letsencrypt.sh touch data/ssl/ca-certificates.crt # will get overwritten by init-letsencrypt.sh
exec ./init-letsencrypt.sh source ./init-letsencrypt.sh
else else
echo "Please put a valid {privkey,fullchain}.pem and ca-certificates.crt into data/ssl/" echo "Please put a valid {privkey,fullchain}.pem and ca-certificates.crt into data/ssl/"
fi fi