84 lines
1.9 KiB
Bash
84 lines
1.9 KiB
Bash
#!/bin/bash
|
|||
|
|
|
||
|
|
# Script per inizializzare i certificati Let's Encrypt
|
||
|
|
# Uso: ./init-letsencrypt.sh your-domain.com your-email@example.com
|
||
|
|
|
||
|
|
set -e
|
||
|
|
|
||
|
|
DOMAIN=$1
|
||
|
|
EMAIL=$2
|
||
|
|
STAGING=${3:-0} # 0 = production, 1 = staging
|
||
|
|
|
||
|
|
if [ -z "$DOMAIN" ] || [ -z "$EMAIL" ]; then
|
||
|
|
echo "Uso: $0 <domain> <email> [staging]"
|
||
|
|
echo "Esempio: $0 example.com admin@example.com 0"
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
echo "### Inizializzazione certificati Let's Encrypt per $DOMAIN"
|
||
|
|
|
||
|
|
# Crea directory temporanea per configurazione nginx
|
||
|
|
mkdir -p ./nginx/conf.d
|
||
|
|
|
||
|
|
# Crea configurazione nginx temporanea (solo HTTP)
|
||
|
|
cat >./nginx/conf.d/app.conf <<EOF
|
||
|
|
server {
|
||
|
|
listen 80;
|
||
|
|
listen [::]:80;
|
||
|
|
server_name $DOMAIN www.$DOMAIN;
|
||
|
|
|
||
|
|
location /.well-known/acme-challenge/ {
|
||
|
|
root /var/www/certbot;
|
||
|
|
}
|
||
|
|
|
||
|
|
location / {
|
||
|
|
return 200 'Let\'s Encrypt initialization in progress...';
|
||
|
|
add_header Content-Type text/plain;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
EOF
|
||
|
|
|
||
|
|
echo "### Avvio nginx e certbot..."
|
||
|
|
docker-compose up -d nginx
|
||
|
|
|
||
|
|
echo "### Attendo che nginx sia pronto..."
|
||
|
|
sleep 5
|
||
|
|
|
||
|
|
echo "### Richiesta certificato per $DOMAIN..."
|
||
|
|
|
||
|
|
# Determina se usare staging o production
|
||
|
|
if [ "$STAGING" != "0" ]; then
|
||
|
|
STAGING_ARG="--staging"
|
||
|
|
echo "### Modalità STAGING (test)"
|
||
|
|
else
|
||
|
|
STAGING_ARG=""
|
||
|
|
echo "### Modalità PRODUCTION"
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Richiedi il certificato
|
||
|
|
docker-compose run --rm certbot certonly \
|
||
|
|
--webroot \
|
||
|
|
--webroot-path=/var/www/certbot \
|
||
|
|
--email $EMAIL \
|
||
|
|
--agree-tos \
|
||
|
|
--no-eff-email \
|
||
|
|
$STAGING_ARG \
|
||
|
|
-d $DOMAIN \
|
||
|
|
-d www.$DOMAIN
|
||
|
|
|
||
|
|
if [ $? -eq 0 ]; then
|
||
|
|
echo "### Certificato ottenuto con successo!"
|
||
|
|
echo "### Aggiornamento configurazione nginx con HTTPS..."
|
||
|
|
|
||
|
|
# Sostituisci example.com con il dominio reale nella configurazione
|
||
|
|
sed -i "s/elenabaracchini\.it/$DOMAIN/g" ./nginx/conf.d/app.conf
|
||
|
|
|
||
|
|
# Ricarica nginx
|
||
|
|
docker-compose exec nginx nginx -s reload
|
||
|
|
|
||
|
|
echo "### Configurazione completata!"
|
||
|
|
echo "### Il rinnovo automatico è attivo (ogni 12 ore)"
|
||
|
|
else
|
||
|
|
echo "### Errore nell'ottenimento del certificato"
|
||
|
|
exit 1
|
||
|
|
fi
|