codice iac per deploy in vm

This commit is contained in:
sbierti committed 2025-11-30 10:13:52 +01:00
commit 5c8442bdd6
5 files changed
+236

No files matched your search

+71
View File
@@ -0,0 +1,71 @@
version: '3.8'
services:
# Java Web Application
app:
image: your-java-app:latest
container_name: java-app
restart: unless-stopped
networks:
- app-network
environment:
- SPRING_PROFILES_ACTIVE=prod
- SERVER_PORT=8080
# ports:
# - "8080:8080" # Non esporre direttamente, usa nginx
volumes:
- app-data:/app/data
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/actuator/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
# Nginx Reverse Proxy
nginx:
image: nginx:alpine
container_name: nginx-proxy
restart: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d:ro
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- certbot-webroot:/var/www/certbot:ro
- certbot-certs:/etc/letsencrypt:ro
- certbot-lib:/var/lib/letsencrypt:ro
networks:
- app-network
depends_on:
- app
healthcheck:
test: ["CMD", "nginx", "-t"]
interval: 30s
timeout: 10s
retries: 3
# Certbot per Let's Encrypt
certbot:
image: certbot/certbot:latest
container_name: certbot
volumes:
- certbot-webroot:/var/www/certbot:rw
- certbot-certs:/etc/letsencrypt:rw
- certbot-lib:/var/lib/letsencrypt:rw
- certbot-logs:/var/log/letsencrypt:rw
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew --webroot --webroot-path=/var/www/certbot --quiet; sleep 12h & wait $${!}; done;'"
networks:
- app-network
networks:
app-network:
driver: bridge
volumes:
app-data:
certbot-webroot:
certbot-certs:
certbot-lib:
certbot-logs:
+84
View File
@@ -0,0 +1,84 @@
#!/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
+14
View File
@@ -0,0 +1,14 @@
server {
listen 80;
listen [::]:80;
server_name elenabaracchini.it www.elenabaracchini.it;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
return 200 'Let\'s Encrypt initialization in progress...';
add_header Content-Type text/plain;
}
}
+39
View File
@@ -0,0 +1,39 @@
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
client_max_body_size 20M;
# Gzip compression
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml text/javascript
application/json application/javascript application/xml+rss
application/rss+xml font/truetype font/opentype
application/vnd.ms-fontobject image/svg+xml;
# Include server configurations
include /etc/nginx/conf.d/*.conf;
}
+28
View File
@@ -0,0 +1,28 @@
#!/bin/bash
# Script per rinnovare manualmente i certificati Let's Encrypt
set -e
echo "### Controllo stato certificati..."
docker-compose run --rm certbot certificates
echo ""
echo "### Test rinnovo (dry-run)..."
docker-compose run --rm certbot renew --dry-run
echo ""
read -p "Vuoi procedere con il rinnovo effettivo? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "### Rinnovo certificati..."
docker-compose run --rm certbot renew
echo "### Ricarico configurazione nginx..."
docker-compose exec nginx nginx -s reload
echo "### Rinnovo completato!"
else
echo "### Rinnovo annullato"
fi