From 5c8442bdd6c5a38a7a2a9aa7a4466dd1484500e0 Mon Sep 17 00:00:00 2001 From: Simone Bierti Date: Sun, 30 Nov 2025 10:13:52 +0100 Subject: [PATCH] codice iac per deploy in vm --- docker-compose.yml | 71 ++++++++++++++++++++++++++++++++++++ init-letsencrypt.sh | 84 +++++++++++++++++++++++++++++++++++++++++++ nginx/conf.d/app.conf | 14 ++++++++ nginx/nginx.conf | 39 ++++++++++++++++++++ renew-certificates.sh | 28 +++++++++++++++ 5 files changed, 236 insertions(+) create mode 100644 docker-compose.yml create mode 100755 init-letsencrypt.sh create mode 100644 nginx/conf.d/app.conf create mode 100644 nginx/nginx.conf create mode 100755 renew-certificates.sh diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..1cabc99 --- /dev/null +++ b/docker-compose.yml @@ -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: diff --git a/init-letsencrypt.sh b/init-letsencrypt.sh new file mode 100755 index 0000000..bcf1212 --- /dev/null +++ b/init-letsencrypt.sh @@ -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 [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 <