107 lines
3.9 KiB
Bash
Executable File
107 lines
3.9 KiB
Bash
Executable File
#!/bin/bash
|
|
# Bootstrap del certificato Let's Encrypt unico (multi-dominio / SAN) per il
|
|
# reverse proxy di ifattidinapoli.it.
|
|
#
|
|
# A differenza del progetto Gogs-Reverse (un certificato separato per dominio),
|
|
# qui si richiede UN SOLO certificato che copre tutti i domini serviti da questo
|
|
# nginx. Il primo dominio dell'array e' il primario (CN) e da' il nome alla
|
|
# cartella /etc/letsencrypt/live/<primario>/ referenziata in nginx.conf.
|
|
#
|
|
# Lanciarlo una volta sola, al primo setup. Per i rinnovi usare renew.sh.
|
|
|
|
set -e
|
|
|
|
if ! [ -x "$(command -v docker-compose)" ]; then
|
|
echo 'Error: docker-compose is not installed.' >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Domini coperti dal certificato. Il PRIMO e' il primario (CN) e da' il nome
|
|
# alla cartella live/. Se lo cambi, aggiorna anche i path in nginx.conf.
|
|
domains=(
|
|
"www.ifattidinapoli.it"
|
|
"ifattidinapoli.it"
|
|
"preproduction.ifattidinapoli.it"
|
|
"test.ifattidinapoli.it"
|
|
)
|
|
|
|
primary_domain="${domains[0]}"
|
|
|
|
rsa_key_size=4096
|
|
data_path="./certbot"
|
|
email="riccardo.didato@gmail.com"
|
|
# Metti a 1 per usare l'ambiente di STAGING di Let's Encrypt (test senza
|
|
# consumare i rate limit). Passato il test, rimetti a 0 e rilancia.
|
|
staging=0
|
|
|
|
# Costruisce la sequenza di argomenti -d per certbot
|
|
domain_args=""
|
|
for domain in "${domains[@]}"; do
|
|
domain_args="$domain_args -d $domain"
|
|
done
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# FASE 0: parametri TLS raccomandati (una tantum)
|
|
# ---------------------------------------------------------------------------
|
|
if [ ! -e "$data_path/conf/options-ssl-nginx.conf" ] || [ ! -e "$data_path/conf/ssl-dhparams.pem" ]; then
|
|
echo "### Downloading recommended TLS parameters ..."
|
|
mkdir -p "$data_path/conf"
|
|
curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot-nginx/certbot_nginx/_internal/tls_configs/options-ssl-nginx.conf > "$data_path/conf/options-ssl-nginx.conf"
|
|
curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot/certbot/ssl-dhparams.pem > "$data_path/conf/ssl-dhparams.pem"
|
|
echo
|
|
fi
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# FASE 1: certificato dummy self-signed
|
|
# nginx non parte se i path ssl_certificate non esistono. Creiamo un cert
|
|
# temporaneo cosi' nginx si avvia e puo' servire la validazione ACME su :80.
|
|
# ---------------------------------------------------------------------------
|
|
if [ -d "$data_path/conf/live/$primary_domain" ]; then
|
|
echo "### Existing certificate found for $primary_domain. Skipping dummy generation."
|
|
else
|
|
echo "### Creating dummy certificate for $primary_domain ..."
|
|
path="/etc/letsencrypt/live/$primary_domain"
|
|
mkdir -p "$data_path/conf/live/$primary_domain"
|
|
docker-compose run --rm --entrypoint "\
|
|
openssl req -x509 -nodes -newkey rsa:$rsa_key_size -days 1 \
|
|
-keyout '$path/privkey.pem' \
|
|
-out '$path/fullchain.pem' \
|
|
-subj '/CN=localhost'" certbot
|
|
echo
|
|
fi
|
|
|
|
echo "### Starting nginx ..."
|
|
docker-compose up --force-recreate -d nginx
|
|
echo
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# FASE 2: richiesta del certificato REALE
|
|
# ---------------------------------------------------------------------------
|
|
echo "### Deleting dummy certificate ..."
|
|
docker-compose run --rm --entrypoint "\
|
|
rm -Rf /etc/letsencrypt/live/$primary_domain && \
|
|
rm -Rf /etc/letsencrypt/archive/$primary_domain && \
|
|
rm -Rf /etc/letsencrypt/renewal/$primary_domain.conf" certbot
|
|
echo
|
|
|
|
case "$email" in
|
|
"") email_arg="--register-unsafely-without-email" ;;
|
|
*) email_arg="--email $email" ;;
|
|
esac
|
|
|
|
if [ "$staging" != "0" ]; then staging_arg="--staging"; fi
|
|
|
|
echo "### Requesting Let's Encrypt certificate for: ${domains[*]} ..."
|
|
docker-compose run --rm --entrypoint "\
|
|
certbot certonly --webroot -w /var/www/certbot \
|
|
$staging_arg \
|
|
$email_arg \
|
|
$domain_args \
|
|
--rsa-key-size $rsa_key_size \
|
|
--agree-tos \
|
|
--force-renewal" certbot
|
|
echo
|
|
|
|
echo "### Reloading nginx ..."
|
|
docker-compose exec nginx nginx -s reload
|