Sub topic 6
PHP-FPM: pools and worker processes
Assume you know basic Linux permissions, systemd commands, HTTP status codes, and how Nginx sends PHP requests to FastCGI. PHP-FPM, or PHP FastCGI Process Manager, runs PHP code in separate processes so Nginx does not need to execute PHP itself.
The PHP-FPM process model
A PHP-FPM service has one master process and one or more worker processes. The master reads configuration, creates workers, and manages their lifecycle. A worker receives a request, runs the PHP application, returns the response, and then waits for another request.
A pool is a group of workers with its own configuration. A typical installation has a pool named www. You can create separate pools for separate applications when they need different users, limits, or sockets.
Pool settings and worker counts
On Debian or Ubuntu, pool files commonly live in /etc/php/8.3/fpm/pool.d/. The PHP version in the path depends on the installed version.
A pool can use a Unix socket and run under a restricted account:
[myapp]
user = myapp
group = myapp
listen = /run/php/myapp.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
pm = dynamic
pm.max_children = 12
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 5
clear_env = yespm.max_children is the maximum number of simultaneous workers. Each worker can consume memory, so increasing this value does not always improve performance. If the server runs out of memory, the kernel may terminate processes or the whole service.
The dynamic mode starts some workers immediately and creates or removes idle workers within the configured limits. Other modes include:
| Mode | Behavior | Typical use |
|---|---|---|
| `dynamic` | Adjusts idle workers between minimum and maximum values | General-purpose servers |
| `ondemand` | Starts workers only when requests arrive | Infrequent applications |
| `static` | Keeps exactly `pm.max_children` workers running | Predictable, continuously busy workloads |
Start with a conservative worker count. Measure memory use and request latency before increasing it. A slow database query can occupy a worker for its entire duration, so worker count cannot compensate for an unhealthy dependency.
Applying and checking configuration
After changing a pool file, validate the configuration and restart or reload PHP-FPM. The service name includes the PHP version.
sudo php-fpm8.3 --test
sudo systemctl reload php8.3-fpm
sudo systemctl status php8.3-fpm
sudo ls -l /run/php/myapp.sockA 502 Bad Gateway from Nginx often means the socket does not exist, has the wrong owner or mode, or PHP-FPM is stopped. Check the PHP-FPM journal and confirm that Nginx uses the same socket path as the pool's listen setting.
Exercise
A PHP application returns 502 after you changed its pool from /run/php/myapp.sock to /run/php/newapp.sock. What should you check first?
Show answer
Check that PHP-FPM loaded the new pool configuration, that /run/php/newapp.sock exists, and that Nginx's fastcgi_pass points to the same path. Then inspect the PHP-FPM and Nginx error logs.