PHP-FPM
What is PHP-FPM?
Section titled “What is PHP-FPM?”PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with features useful for high-load websites.
Installation
Section titled “Installation”# Ubuntu/Debiansudo apt install php-fpm
# Check versionphp-fpm -v
# Check statussudo systemctl status php8.2-fpmNginx + PHP-FPM
Section titled “Nginx + PHP-FPM”server { listen 80; server_name example.com; root /var/www/html; index index.php index.html;
location / { try_files $uri $uri/ /index.php?$query_string; }
location ~ \.php$ { fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }}Pool Configuration
Section titled “Pool Configuration”/etc/php/8.2/fpm/pool.d/www.conf:
[www]user = www-datagroup = www-datalisten = /var/run/php/php8.2-fpm.sock
; Process managementpm = dynamicpm.max_children = 50pm.start_servers = 5pm.min_spare_servers = 5pm.max_spare_servers = 35
; Loggingaccess.log = /var/log/php-fpm/access.logslowlog = /var/log/php-fpm/slow.logrequest_slowlog_timeout = 5sProcess Modes
Section titled “Process Modes”| Mode | Description | Best For |
|---|---|---|
static | Fixed number of children | Predictable load |
dynamic | Scales based on demand | Most cases |
ondemand | Spawns on request | Low traffic |
Management
Section titled “Management”sudo systemctl start php8.2-fpmsudo systemctl stop php8.2-fpmsudo systemctl restart php8.2-fpmsudo systemctl reload php8.2-fpm # Graceful reloadvs Python/Node
Section titled “vs Python/Node”| Aspect | PHP-FPM | Python | Node.js |
|---|---|---|---|
| Model | Process pool | Single process | Event loop |
| Memory | Per-request | Persistent | Persistent |
| Scaling | FPM handles | Supervisor/PM2 | PM2 cluster |
| Best For | Web pages | APIs, scripts | Real-time |