Aller au contenu principal

PHP en production

Un projet PHP prêt pour la production, léger, sécurisé, et déployable en un seul fichier.

ComposantImage
PHPphp:8.3-fpm
Webnginx:alpine
Orchestrationdocker-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

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 monuser par votre nom d’utilisateur Docker Hub.

Remplacez monprojet par 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

AvantageExplication
1 seul fichier en proddocker-compose.yml suffit
Zéro config localenginx.conf embarqué dans l’image
SécuritéCode en lecture seule (:ro)
PerformancesPHP-FPM + Nginx > Apache
Légeralpine = images < 20 Mo
ModerneStandard DevOps 2025
ScalablePrêt pour Kubernetes / Swarm
CI/CD friendlyImages versionnées, reproductibles