Improve deployment process by generating secure passwords

Update deployment scripts to automatically generate secure PostgreSQL passwords, store them securely, and use them in environment configuration.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 99f0fce6-9386-489a-9632-1d81223cab44
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/99f0fce6-9386-489a-9632-1d81223cab44/H8Wilyj
This commit is contained in:
marco370 2025-10-16 07:15:54 +00:00
parent 5a51a65218
commit 3cdc6931bb
4 changed files with 210 additions and 12 deletions

View File

@ -66,12 +66,17 @@ Lo script installerà:
Lo script crea automaticamente: Lo script crea automaticamente:
- Database: `vigilanza_turni` - Database: `vigilanza_turni`
- User: `vigilanza_user` - User: `vigilanza_user`
- Password: `ChangeMe_ProductionPassword123!` ⚠️ **CAMBIALA!** - Password: **Generata automaticamente** (mostrata a fine setup)
⚠️ **IMPORTANTE**: Salva la password mostrata al termine del setup!
```bash ```bash
# Cambia password PostgreSQL # La password è salvata anche in:
cat /root/.vigilanza_db_password
# Se persa, puoi cambiarla con:
sudo -u postgres psql sudo -u postgres psql
ALTER USER vigilanza_user WITH PASSWORD 'TuaPasswordSicura123!'; ALTER USER vigilanza_user WITH PASSWORD 'NuovaPasswordSicura123!';
\q \q
``` ```
@ -178,9 +183,14 @@ cp .env.production.example .env
nano .env nano .env
``` ```
Configura: Configura usando la password PostgreSQL generata durante setup:
```bash ```bash
DATABASE_URL=postgresql://vigilanza_user:TuaPasswordSicura@localhost:5432/vigilanza_turni # Recupera password DB
DB_PASS=$(grep PGPASSWORD /root/.vigilanza_db_password | cut -d= -f2)
# Configura .env
DATABASE_URL=postgresql://vigilanza_user:${DB_PASS}@localhost:5432/vigilanza_turni
SESSION_SECRET=$(openssl rand -base64 32) SESSION_SECRET=$(openssl rand -base64 32)
REPLIT_DOMAINS=vigilanza.tuodominio.it REPLIT_DOMAINS=vigilanza.tuodominio.it
``` ```

172
QUICKSTART-DEPLOYMENT.md Normal file
View File

@ -0,0 +1,172 @@
# 🚀 Quick Start - Deployment VigilanzaTurni
Guida rapida per deployment da Replit → GitLab → Server AlmaLinux 9
## 📝 Checklist Pre-Deployment
- [ ] Server AlmaLinux 9 disponibile (min 2GB RAM)
- [ ] Dominio configurato (es. vigilanza.tuodominio.it)
- [ ] Account GitLab su git.alfacom.it
- [ ] SSH access al server
---
## ⚡ Setup Rapido (15 minuti)
### 1⃣ Setup Server (5 min)
```bash
# SSH nel server
ssh root@ip-del-server
# Download e esegui setup automatico
curl -o setup.sh https://git.alfacom.it/marco/VigilanzaTurni/-/raw/main/deploy/setup-server.sh
chmod +x setup.sh
sudo bash setup.sh
# ⚠️ IMPORTANTE: Salva la password PostgreSQL mostrata!
```
### 2⃣ Configura GitLab CI/CD (3 min)
**Genera SSH Key:**
```bash
# Sul tuo PC
ssh-keygen -t ed25519 -C "gitlab-deploy" -f ~/.ssh/gitlab-deploy
ssh-copy-id -i ~/.ssh/gitlab-deploy.pub root@ip-del-server
cat ~/.ssh/gitlab-deploy # Copia output
```
**GitLab → Settings → CI/CD → Variables:**
| Nome | Valore |
|------|--------|
| `SSH_PRIVATE_KEY` | [chiave privata copiata sopra] |
| `DEPLOY_HOST` | ip-del-server |
| `DEPLOY_USER` | root |
| `DEPLOY_DOMAIN` | vigilanza.tuodominio.it |
### 3⃣ Configura Replit (2 min)
```bash
# In Replit Shell
git remote add production https://git.alfacom.it/marco/VigilanzaTurni.git
# Crea Personal Access Token su GitLab e salvalo in Replit Secrets
# GitLab → User Settings → Access Tokens → write_repository
```
### 4⃣ Configura Server .env (3 min)
```bash
# Sul server
cd /var/www/vigilanza-turni
# Clone iniziale
git clone https://git.alfacom.it/marco/VigilanzaTurni.git .
# Crea .env
cp .env.production.example .env
nano .env
```
**Inserisci:**
```bash
# Password DB dal setup (vedi /root/.vigilanza_db_password)
DATABASE_URL=postgresql://vigilanza_user:PASSWORD_GENERATA@localhost:5432/vigilanza_turni
SESSION_SECRET=$(openssl rand -base64 32)
REPLIT_DOMAINS=vigilanza.tuodominio.it
```
### 5⃣ Nginx e SSL (2 min)
```bash
# Copia config Nginx
sudo cp deploy/nginx.conf /etc/nginx/conf.d/vigilanza-turni.conf
# Modifica con il tuo dominio
sudo nano /etc/nginx/conf.d/vigilanza-turni.conf
# Sostituisci "tuodominio.it" → "vigilanza.tuodominio.it"
# Test e reload
sudo nginx -t
sudo systemctl reload nginx
# SSL Certificate
sudo certbot --nginx -d vigilanza.tuodominio.it
```
### 6⃣ Primo Deploy (1 min)
```bash
# Sul server
cd /var/www/vigilanza-turni
bash deploy/deploy.sh
```
---
## 🔄 Workflow Quotidiano
### Da Replit → Produzione
```bash
# 1. Fai modifiche in Replit
# 2. Push a GitLab
bash push-to-gitlab.sh
# 3. Vai su GitLab
https://git.alfacom.it/marco/VigilanzaTurni/-/pipelines
# 4. Clicca su "deploy_production" quando pronto
```
---
## 📊 Comandi Utili
```bash
# Status applicazione
pm2 status
# Logs real-time
pm2 logs vigilanza-turni
# Restart
pm2 restart vigilanza-turni
# Backup database
sudo -u postgres pg_dump vigilanza_turni > backup_$(date +%Y%m%d).sql
```
---
## 🆘 Problemi Comuni
**App non risponde:**
```bash
pm2 restart vigilanza-turni
sudo systemctl reload nginx
```
**Errore database:**
```bash
# Verifica password in .env corrisponde a quella in /root/.vigilanza_db_password
cat /root/.vigilanza_db_password
```
**SSL scaduto:**
```bash
sudo certbot renew
sudo systemctl reload nginx
```
---
## 📚 Documentazione Completa
Per dettagli completi: [DEPLOYMENT.md](./DEPLOYMENT.md)
---
**Setup completato?** ✅ Vai su https://vigilanza.tuodominio.it

12
deploy/deploy.sh Executable file → Normal file
View File

@ -15,19 +15,23 @@ cd $APP_DIR
# Pull ultime modifiche (già fatto da GitLab Runner) # Pull ultime modifiche (già fatto da GitLab Runner)
echo "📦 Repository aggiornato" echo "📦 Repository aggiornato"
# Installa dipendenze # Installa TUTTE le dipendenze (serve per build e migrations)
echo "📥 Installazione dipendenze..." echo "📥 Installazione dipendenze (include devDependencies)..."
npm ci --production npm ci
# Build frontend # Build frontend
echo "🏗️ Build frontend Vite..." echo "🏗️ Build frontend Vite..."
export NODE_ENV=production export NODE_ENV=production
npm run build npm run build
# Database migrations (se necessario) # Database migrations (serve Drizzle che è devDependency)
echo "🗄️ Verifica database schema..." echo "🗄️ Verifica database schema..."
npm run db:push || true npm run db:push || true
# Rimuovi devDependencies dopo build e migrations
echo "🧹 Pulizia devDependencies (mantiene solo production)..."
npm prune --production
# Restart applicazione con PM2 # Restart applicazione con PM2
echo "🔄 Restart applicazione..." echo "🔄 Restart applicazione..."
if pm2 show $APP_NAME > /dev/null 2>&1; then if pm2 show $APP_NAME > /dev/null 2>&1; then

18
deploy/setup-server.sh Executable file → Normal file
View File

@ -48,16 +48,24 @@ postgresql-setup --initdb
systemctl enable postgresql systemctl enable postgresql
systemctl start postgresql systemctl start postgresql
# Generazione password sicura PostgreSQL
DB_PASSWORD=$(openssl rand -base64 32 | tr -d "=+/" | cut -c1-25)
# Creazione database e utente # Creazione database e utente
log_info "Configurazione database..." log_info "Configurazione database..."
sudo -u postgres psql << EOF sudo -u postgres psql << EOF
CREATE DATABASE vigilanza_turni; CREATE DATABASE vigilanza_turni;
CREATE USER vigilanza_user WITH ENCRYPTED PASSWORD 'ChangeMe_ProductionPassword123!'; CREATE USER vigilanza_user WITH ENCRYPTED PASSWORD '${DB_PASSWORD}';
GRANT ALL PRIVILEGES ON DATABASE vigilanza_turni TO vigilanza_user; GRANT ALL PRIVILEGES ON DATABASE vigilanza_turni TO vigilanza_user;
\c vigilanza_turni \c vigilanza_turni
GRANT ALL ON SCHEMA public TO vigilanza_user; GRANT ALL ON SCHEMA public TO vigilanza_user;
EOF EOF
# Salva password in file sicuro
echo "PGPASSWORD=${DB_PASSWORD}" > /root/.vigilanza_db_password
chmod 600 /root/.vigilanza_db_password
log_info "Password PostgreSQL salvata in: /root/.vigilanza_db_password"
# Configurazione PostgreSQL per connessioni locali # Configurazione PostgreSQL per connessioni locali
log_info "Configurazione autenticazione PostgreSQL..." log_info "Configurazione autenticazione PostgreSQL..."
PG_HBA="/var/lib/pgsql/data/pg_hba.conf" PG_HBA="/var/lib/pgsql/data/pg_hba.conf"
@ -100,9 +108,13 @@ log_info "================================================"
log_info "" log_info ""
log_warn "PROSSIMI PASSI:" log_warn "PROSSIMI PASSI:"
echo "1. Configura il DNS per puntare questo server" echo "1. Configura il DNS per puntare questo server"
echo "2. Modifica la password PostgreSQL in /var/www/vigilanza-turni/.env" echo "2. Copia DATABASE_URL qui sotto nel file /var/www/vigilanza-turni/.env"
echo "3. Ottieni certificato SSL: sudo certbot --nginx -d tuodominio.it" echo "3. Ottieni certificato SSL: sudo certbot --nginx -d tuodominio.it"
echo "4. Esegui il primo deployment con GitLab CI/CD" echo "4. Esegui il primo deployment con GitLab CI/CD"
echo "" echo ""
log_warn "⚠️ IMPORTANTE - Salva questa password (disponibile in /root/.vigilanza_db_password):"
echo ""
log_info "DATABASE_URL per .env:" log_info "DATABASE_URL per .env:"
echo "postgresql://vigilanza_user:ChangeMe_ProductionPassword123!@localhost:5432/vigilanza_turni" echo "postgresql://vigilanza_user:${DB_PASSWORD}@localhost:5432/vigilanza_turni"
echo ""
log_warn "Password PostgreSQL generata automaticamente: ${DB_PASSWORD}"