# OPTIM-02 — Apache MPM Event + PHP-FPM + OPcache JIT

**Date** : 2026-06-24
**Serveur** : 51.158.96.239 — Apache 2.4.58, PHP 8.3.6, 3 vCPU / 3.8 Go RAM
**Objectif** : passer de `mpm_prefork`+`mod_php` (gourmand, peu concurrent) à `mpm_event`+**PHP-FPM**,
activer **OPcache JIT**. ⚠️ Changement runtime sensible → rollback auto intégré.

## État avant (audit)
- Apache : `mpm_prefork` + `php_module` (mod_php).
- `php8.3-fpm` **non installé** (seul `libapache2-mod-php8.3`).
- `mpm_event`, `proxy_fcgi`, `setenvif` disponibles.
- **Aucune** directive `php_value/php_flag` dans `.htaccess`/vhost → migration FPM sûre.
- OPcache ON (128M), **JIT off** (`opcache.jit=off`, `jit_buffer_size=0`).
- `innodb_buffer_pool_size` = 1 Go (déjà fait, OPTIM-01).

## Pourquoi
- `mpm_event` + FPM : process PHP découplés d'Apache → bien moins de RAM par connexion, meilleure concurrence,
  Apache sert le statique en event (threads légers).
- OPcache JIT (`tracing`) : compilation native du PHP chaud (gain CPU sur le rendu).

## Changements
1. `apt install php8.3-fpm` (fournit le service + `conf-available/php8.3-fpm.conf`).
2. OPcache/JIT (`/etc/php/8.3/fpm/conf.d/99-tlo-opcache.ini`) :
   ```ini
   opcache.enable=1
   opcache.memory_consumption=256
   opcache.interned_strings_buffer=16
   opcache.max_accelerated_files=20000
   opcache.validate_timestamps=1
   opcache.revalidate_freq=2
   opcache.jit=tracing
   opcache.jit_buffer_size=128M
   ```
3. Pool FPM (`pool.d/www.conf`) : `pm=dynamic`, `max_children=25`, `start_servers=4`,
   `min_spare_servers=4`, `max_spare_servers=10` (RAM ~2.5 Go libre, ~40-60 Mo/process).
4. Bascule Apache :
   `a2dismod php8.3 mpm_prefork` ; `a2enmod mpm_event proxy_fcgi setenvif` ; `a2enconf php8.3-fpm`.

## Procédure & ROLLBACK
1. `apache2ctl configtest` **avant** tout restart.
2. `systemctl enable --now php8.3-fpm` ; `systemctl restart apache2`.
3. Vérif : site HTTP 200, SAPI = `fpm-fcgi`, JIT actif, pas d'erreur dans error.log.
4. **Rollback si échec** :
   `a2dismod mpm_event proxy_fcgi` ; `a2disconf php8.3-fpm` ; `a2enmod mpm_prefork php8.3` ;
   `systemctl restart apache2` → retour mod_php.

## Versioning
- Snapshot avant : `snapshots/apache/` (mods-enabled, ports.conf), `php.opcache.before.txt`
- Snapshot après : `php.sapi-jit.after.txt`, `99-tlo-opcache.ini`, `www.conf`

## Résultat (vérifié 2026-06-24)
- Apache : `mpm_event` + `proxy_fcgi` (mod_php retiré). `php8.3-fpm` actif.
- OPcache (SAPI FPM) : `enable=On`, `jit=tracing`, `jit_buffer_size=128M`, `memory_consumption=256`.
- Pages `/`, `/artiste/…`, `/moteursession` → **HTTP 200**.
- Sécurité : PHP dans `uploads/` → **403** (blocage vhost intact).
- RAM utilisée ~1.3 Go → **988 Mo** (gain prefork→event/FPM). Load stable ~0.8.
- Rollback dispo : réactiver `mpm_prefork`+`php8.3`, désactiver event/proxy_fcgi/fpm conf.

## ⚠️ Correctif 2026-06-24 : JIT DÉSACTIVÉ (cassait l'admin)
Le JIT `tracing` (PHP 8.3) faisait **crasher le worker FPM sur `wp-admin/load-styles.php`**
(HTTP 500 corps vide = segfault, pas d'erreur PHP loguée) → tout le CSS du dashboard tombait
(tableau de bord affiché en HTML brut sans styles). `load-scripts.php` (chemin différent) passait.
- **Fix** : `opcache.jit=off`, `opcache.jit_buffer_size=0` dans `99-tlo-opcache.ini` + restart FPM.
- **Vérifié** : `load-styles.php` 500→200 (text/css), admin de nouveau stylé, front 200.
- **OPcache bytecode reste actif** (`opcache.enable=1`, memory 256M) = le vrai gain. Le JIT n'apporte
  quasi rien à WordPress (I/O-bound) et introduisait un crash → désactivé volontairement.

## Statut : ✅ APPLIQUÉ (JIT off ; mpm_event + PHP-FPM + OPcache conservés)
