# Déploiement JoPer sur O2switch

## Connexion SSH

```bash
ssh bege3009@bege3009.odns.fr
cd ~/joper.fr
source ~/virtualenv/joper.fr/3.12/bin/activate
```

## Commandes Django (sur O2switch)

Toutes les commandes Django doivent spécifier le fichier settings de production :

```bash
# Migrations
python src/manage.py migrate --settings=config.settings.production

# Collectstatic
python src/manage.py collectstatic --noinput --settings=config.settings.production

# Créer un superuser
python src/manage.py createsuperuser --settings=config.settings.production

# Shell Django
python src/manage.py shell --settings=config.settings.production

# Vérifier la configuration
python src/manage.py check --settings=config.settings.production
```

## Mise à jour du site

```bash
# 1. Se connecter
ssh bege3009@bege3009.odns.fr
cd ~/joper.fr
source ~/virtualenv/joper.fr/3.12/bin/activate

# 2. Récupérer les changements
git pull

# 3. Installer les nouvelles dépendances (si besoin)
pip install -r requirements.txt

# 4. Appliquer les migrations (si besoin)
python src/manage.py migrate --settings=config.settings.production

# 5. Collecter les fichiers statiques (si besoin)
python src/manage.py collectstatic --noinput --settings=config.settings.production

# 6. Redémarrer l'application
touch tmp/restart.txt
```

## Fichier .env (sur O2switch)

Le fichier `.env` doit être à la racine `~/joper.fr/.env` :

```
SECRET_KEY=xxx
DEBUG=False
DB_NAME=bege3009_joper
DB_USER=bege3009_joper
DB_PASSWORD=xxx
DB_HOST=localhost
DB_PORT=3306
ALLOWED_HOSTS=joper.fr,www.joper.fr
```

## Logs et debugging

```bash
# Voir les erreurs récentes
zcat ~/logs/app.joper.fr-*.gz | tail -50

# Tester si Django démarre
python -c "
import os, sys
sys.path.insert(0, 'src')
os.environ['DJANGO_SETTINGS_MODULE'] = 'config.settings.production'
with open('.env') as f:
    for line in f:
        line = line.strip()
        if line and not line.startswith('#') and '=' in line:
            key, value = line.split('=', 1)
            os.environ[key] = value
from django.core.wsgi import get_wsgi_application
app = get_wsgi_application()
print('Django OK!')
"
```

## Structure importante

```
~/joper.fr/
├── .env                    # Variables d'environnement (non versionné)
├── passenger_wsgi.py       # Point d'entrée Passenger
├── tmp/restart.txt         # Touch pour redémarrer
├── src/
│   ├── manage.py
│   ├── config/settings/
│   │   ├── base.py
│   │   ├── local.py        # Dev (Docker)
│   │   └── production.py   # Prod (O2switch)
│   └── ...
└── staticfiles/            # Fichiers statiques collectés
```

## Problèmes courants

### passenger_wsgi.py écrasé par cPanel
Si l'app Python est recréée dans cPanel, le fichier est écrasé. Solution :
```bash
git checkout passenger_wsgi.py
touch tmp/restart.txt
```

### Bad Request 400
Vérifier que `ALLOWED_HOSTS` contient le domaine dans `.env`.

### Erreur Passenger "something went wrong"
Tester Django manuellement (voir section Logs).
