PHP en production
Un projet PHP prêt pour la production, léger, sécurisé, et déployable en un seul fichier.
| Composant | Image |
|---|---|
| PHP | php:8.3-fpm |
| Web | nginx:alpine |
| Orchestration | docker-compose.yml |
1. Structure du projet
Arborescence de nos fichiers pendannt la phase de création des images Docker :
mon-projet/
├── index.php
├── Dockerfile → PHP-FPM
├── nginx.Dockerfile → Nginx personnalisé
├── nginx.conf → Configuration Nginx
└── docker-compose.yml → Développement local
index.php
<?php
echo "<h1>Bienvenue sur PHP 8.3 + FPM + Nginx !</h1>";
echo "<p>Stack moderne, prête pour la production.</p>";
phpinfo();
?>
php.Dockerfile
PHP-FPM uniquement
# Dockerfile
FROM php:8.3-fpm
# Copie du code source
COPY . /var/www/html/
# Droits pour l'utilisateur www-data
RUN chown -R www-data:www-data /var/www/html
nginx.Dockerfile
Nginx avec config embarquée
# nginx.Dockerfile
FROM nginx:alpine
# Remplace la configuration par défaut
COPY nginx.conf /etc/nginx/conf.d/default.conf
nginx.conf
Configuration Nginx pour PHP-FPM
server {
listen 80;
server_name localhost;
root /var/www/html;
index index.php;
# Gestion des fichiers statiques + fallback sur index.php
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# Exécution des scripts PHP via FPM
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# Sécurité : bloquer l'accès aux fichiers sensibles
location ~ /\.(?!well-known).* {
deny all;
}
}
docker-compose.yml (en dev)
services:
php:
build:
context: .
dockerfile: php.Dockerfile
volumes:
- .:/var/www/html
networks:
- app-network
nginx:
build:
context: .
dockerfile: nginx.Dockerfile
ports:
- "8080:80"
volumes:
- .:/var/www/html
depends_on:
- php
networks:
- app-network
networks:
app-network:
driver: bridge
2. Créer et démarrer les conteneurs
docker-compose up -d
- Vérification
Le site Web doit être accessible sur :
http://localhost:8080
3. Arrêter
docker-compose down
4. Publier les images sur Docker Hub
# Image PHP
docker build -f php.Dockerfile -t monuser/monprojet-php:v1 .
docker push monuser/monprojet-php:v1
# Image Nginx personnalisée
docker build -f nginx.Dockerfile -t monuser/monprojet-nginx:v1 .
docker push monuser/monprojet-nginx:v1
Remplacez
monuserpar votre nom d’utilisateur Docker Hub.
Remplacez
monprojetpar le nom de votre projet.
5. Déployer en production
docker-compose.yml (en prod)
Créez un nouveau fichier docker-compose.yml (sur un serveur, en CI/CD, Coolify, etc.) :
services:
php:
image: monuser/monprojet-php:v1
networks:
- app-network
nginx:
image: monuser/monprojet-nginx:v1
ports:
- "8080:80"
volumes:
- php-data:/var/www/html:ro
depends_on:
- php
networks:
- app-network
networks:
app-network:
driver: bridge
volumes:
php-data:
driver: local
Lancer la stack en production
docker-compose up -d
Site disponible : http://localhost:8080
Avantages de cette stack
| Avantage | Explication |
|---|---|
| 1 seul fichier en prod | docker-compose.yml suffit |
| Zéro config locale | nginx.conf embarqué dans l’image |
| Sécurité | Code en lecture seule (:ro) |
| Performances | PHP-FPM + Nginx > Apache |
| Léger | alpine = images < 20 Mo |
| Moderne | Standard DevOps 2025 |
| Scalable | Prêt pour Kubernetes / Swarm |
| CI/CD friendly | Images versionnées, reproductibles |