# 07 — Déploiement & infrastructure

## Ressources serveur (état constaté)

- **Apache2** en reverse proxy (mods actifs : `proxy_http`, `proxy_wstunnel`, `rewrite`, `ssl`,
  `headers`), SSL via **Let's Encrypt** (certbot, plugin apache).
- **PostgreSQL 17** (`postgresql@17-main`, port 5432).
- **PM2** gère les process Node de chaque site.
- **Sites existants à préserver** : `woogalf.fr`, `planifik.fr`, `glamz`, `/var/www/html`.

## Allocation des ports (ne pas réutiliser)

| Site         | Frontend | Backend |
|--------------|----------|---------|
| woogalf.fr   | 3000     | 4000    |
| planifik.fr  | 3001     | 4001    |
| **leperelion.fr** | **3002** | **4002** |

## Base de données dédiée

- Base : `leperelion` — Rôle propriétaire : `leperelion` (attribut `CREATEDB` pour la shadow DB
  Prisma, **sans** superuser).
- Connexion locale : `postgresql://leperelion:<motdepasse>@localhost:5432/leperelion`.
- **Aucune** modification des bases `woogalf` / `planifik` / `glamz`.

## Variables d'environnement backend (`backend/.env`)

```
# Postgres
DATABASE_URL=postgresql://leperelion:<motdepasse>@localhost:5432/leperelion

# App
NODE_ENV=production
PORT=4002
CORS_ORIGIN=https://leperelion.fr

# Sécurité / sessions
JWT_ACCESS_SECRET=<aléatoire>
JWT_REFRESH_SECRET=<aléatoire>
JWT_ACCESS_EXPIRES=15m
JWT_REFRESH_EXPIRES=30d
COOKIE_DOMAIN=leperelion.fr
COOKIE_SECURE=true

# Chiffrement des tokens Google (clé 32 octets en base64/hex)
LEPERELION_ENCRYPTION_KEY=<aléatoire 32o>

# Google OAuth
GOOGLE_CLIENT_ID=<console google>
GOOGLE_CLIENT_SECRET=<console google>
GOOGLE_REDIRECT_URI=https://leperelion.fr/backend/google/oauth/callback
```

> `.env` est en `.gitignore`. Un `.env.example` (sans secrets) sera fourni dans `backend/`.

## Vhost Apache (modèle, calqué sur woogalf)

Fichier `/etc/apache2/sites-available/leperelion.fr.conf` (HTTP → redirige vers HTTPS) :

```apache
<VirtualHost *:80>
    ServerName leperelion.fr
    ServerAlias www.leperelion.fr
    DocumentRoot /var/www/leperelion.fr
    ErrorLog ${APACHE_LOG_DIR}/leperelion.fr-error.log
    CustomLog ${APACHE_LOG_DIR}/leperelion.fr-access.log combined
    RewriteEngine on
    RewriteCond %{SERVER_NAME} =www.leperelion.fr [OR]
    RewriteCond %{SERVER_NAME} =leperelion.fr
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
```

Fichier SSL `leperelion.fr-le-ssl.conf` (généré/complété par certbot) :

```apache
<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerName leperelion.fr
    ServerAlias www.leperelion.fr
    ErrorLog ${APACHE_LOG_DIR}/leperelion.fr-error.log
    CustomLog ${APACHE_LOG_DIR}/leperelion.fr-access.log combined

    Alias /uploads /var/www/leperelion.fr/uploads
    <Directory /var/www/leperelion.fr/uploads>
        Options -Indexes
        AllowOverride None
        Require all granted
    </Directory>
    ProxyPass /uploads/ !

    ProxyPreserveHost On
    ProxyPass /backend/ http://localhost:4002/
    ProxyPassReverse /backend/ http://localhost:4002/

    # WebSocket (hot reload Next.js en dev)
    RewriteEngine On
    RewriteCond %{HTTP:Upgrade} websocket [NC]
    RewriteCond %{HTTP:Connection} upgrade [NC]
    RewriteRule ^/?(.*) ws://localhost:3002/$1 [P,L]

    ProxyPass / http://localhost:3002/
    ProxyPassReverse / http://localhost:3002/

    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateFile /etc/letsencrypt/live/leperelion.fr/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/leperelion.fr/privkey.pem
    Header always unset X-Powered-By
</VirtualHost>
</IfModule>
```

### Procédure d'activation (sans perturber les autres sites)

```bash
# 1. Déposer le vhost HTTP
sudo a2ensite leperelion.fr.conf
sudo apache2ctl configtest          # IMPÉRATIF avant tout reload
sudo systemctl reload apache2

# 2. Obtenir le certificat (DNS doit pointer vers le serveur)
sudo certbot --apache -d leperelion.fr -d www.leperelion.fr

# 3. Vérifier
sudo apache2ctl configtest && curl -I https://leperelion.fr
```

> `certbot --apache` ne modifie que le vhost de ce domaine ; il ne touche pas aux autres sites.
> Toujours `configtest` avant `reload`. En cas de doute, ne pas recharger Apache.

## PM2

```bash
# Backend
cd /var/www/leperelion.fr/backend && npm run build
pm2 start dist/index.js --name leperelion-backend
# Frontend
cd /var/www/leperelion.fr/frontend && npm run build
pm2 start "npm run start" --name leperelion-frontend
# Persistance
pm2 save
```

> Ne jamais utiliser `pm2 restart all` / `pm2 stop all` : cibler uniquement
> `leperelion-backend` et `leperelion-frontend`.

## DNS

- `leperelion.fr` et `www.leperelion.fr` doivent pointer (A/AAAA) vers l'IP du serveur.
  D'après l'utilisateur, le DNS est **déjà pointé** (à confirmer avant certbot).
