Sub topic 6
PHP-FPM: pools and worker processes
This chapter assumes you can edit files and manage packages on a Debian or Ubuntu server, and that you know the basic HTTP request cycle. It also assumes you have seen the FastCGI model from the previous chapters. Here you will learn what PHP-FPM runs, how pools divide that work, and how to configure and size them. Connecting Nginx to a pool is covered in the next chapter.
What PHP-FPM is
PHP-FPM (FastCGI Process Manager) is a service that keeps PHP interpreters running as long-lived processes. A web server such as Nginx does not execute PHP itself. It forwards the request to PHP-FPM over FastCGI, and a worker process runs the script and returns the output.
PHP-FPM has two kinds of processes:
- The master process runs as root. It reads the configuration, opens the listening sockets, starts and stops workers, and handles reload signals. It never runs your code.
- Worker processes run your PHP scripts. Each worker handles one request at a time, then waits for the next one. They run as the user and group configured for their pool.
A pool is a named group of workers with its own listener, user, and limits. One PHP-FPM service can run several pools.
Where the configuration lives
On Debian and Ubuntu, with PHP 8.3 as the example version, the files are:
| Path | Purpose |
|---|---|
| `/etc/php/8.3/fpm/php-fpm.conf` | Global settings: PID file, error log, and an include of the pool directory |
| `/etc/php/8.3/fpm/pool.d/*.conf` | One file per pool |
| `/etc/php/8.3/fpm/pool.d/www.conf` | The default pool, named `www` |
| `/etc/php/8.3/fpm/php.ini` | PHP settings such as `memory_limit` |
The package name, service name, and paths include the PHP version. Adjust them to match your installation.
A pool definition
This pool belongs to one site and runs under its own user.
[example]
user = example
group = example
listen = /run/php/php8.3-fpm-example.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
pm = dynamic
pm.max_children = 10
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 5
pm.max_requests = 500
request_terminate_timeout = 60s
request_slowlog_timeout = 5s
slowlog = /var/log/php8.3-fpm-example.slow.logBefore using it, create the system user and check the file:
sudo adduser --system --group --no-create-home example
sudo php-fpm8.3 -t
sudo systemctl reload php8.3-fpmphp-fpm8.3 -t tests the configuration and prints any errors. A reload restarts workers gracefully, so requests already in progress can finish.
Identity and listener settings
[example]is the pool name. It must be unique across all pool files.userandgroupset the identity of the workers. File access by your PHP code is limited by this identity.listenis where the pool accepts connections. It can be a Unix socket path or a TCP address such as127.0.0.1:9000.listen.owner,listen.group, andlisten.modeset who may connect to a Unix socket. Nginx runs aswww-dataon Debian, so that user needs access. If these settings are missing, the socket belongs to the pool user and Nginx may be refused.
The three process manager modes
The pm setting controls how the master decides how many workers exist.
| Mode | Behavior | Best for |
|---|---|---|
| `static` | Always keeps exactly `pm.max_children` workers | Busy, dedicated servers with predictable memory |
| `dynamic` | Keeps between `min_spare_servers` and `max_spare_servers` idle workers, up to `max_children` total | General use, the default |
| `ondemand` | Starts no workers at idle and spawns them per request, then ends them after `pm.process_idle_timeout` | Many small, rarely used pools |
In dynamic mode, the master watches the number of idle workers. If idle workers fall below pm.min_spare_servers, it starts more. If they exceed pm.max_spare_servers, it stops some. pm.start_servers is the number created at startup. Keep this relationship: min_spare_servers ≤ start_servers ≤ max_spare_servers ≤ max_children. A violation prevents the pool from starting.
ondemand saves memory on idle sites, but the first request after a quiet period pays the cost of starting a process.
Limits that matter
pm.max_children is the hard cap on simultaneous workers, and so on simultaneous PHP requests for the pool. When all workers are busy, new connections wait in the listen queue (listen.backlog) and are served as workers free up. If the queue fills or the wait is too long, the web server returns an error, usually a 502 or 504.
pm.max_requests makes a worker exit and be replaced after serving that many requests. It limits the damage from memory leaks in extensions or libraries. A value of 0 means workers never recycle. A value such as 500 is a common starting point.
request_terminate_timeout kills a worker that runs a single request longer than the limit. This is separate from PHP's max_execution_time, which counts only CPU-side script time in many cases and does not cover time spent waiting on I/O such as database calls or sleep(). Setting both gives you a reliable ceiling.
request_slowlog_timeout and slowlog write a PHP stack trace for any request that runs longer than the threshold. This shows which function a slow request was stuck in. The log directory must be writable by the master, which runs as root, so this usually works without extra setup.
Sizing max_children
Each worker uses memory, and max_children multiplied by the worker size must fit in RAM. Measure the real size on your server after the application has served traffic:
ps -C php-fpm8.3 -o rss= | awk '{ s += $1; n++ } END { printf "%d processes, average %.0f MB\n", n, s / n / 1024 }'rss is the resident memory of each process in kilobytes. The count includes the master, which is smaller than a worker, so the average is slightly low. Shared pages are counted once per process, so the result overstates the true total a little. That error is on the safe side.
The formula is:
max_children = RAM reserved for PHP / average worker memoryFor example, a server with 2 GB of RAM might keep 1.2 GB for PHP-FPM and leave the rest for Nginx, the database, and the operating system. With workers averaging 60 MB, the cap is 1200 / 60 = 20.
Setting the cap higher than memory allows is a common mistake. Under load, the kernel starts swapping or the out-of-memory killer ends processes. A lower cap makes requests queue, which is slower but safe.
Multiple pools and isolation
Separate pools let you run different sites under different users. If one site is compromised, the attacker holds only that site's identity and cannot read the other site's files, provided file permissions deny access to other users. Each pool also has its own limits, so a traffic spike on one site cannot use all the workers of another.
Give each pool a unique name, a unique socket path, and its own user. Then point each Nginx server block at the matching socket.
Common errors
WARNING: [pool example] server reached pm.max_children setting (10), consider raising itappears in the FPM log (/var/log/php8.3-fpm.log). All workers were busy. Check for slow requests with the slow log before simply raising the cap.connect() to unix:/run/php/... failed (13: Permission denied)appears in the Nginx error log. The web server user is not allowed to use the socket. Checklisten.owner,listen.group, andlisten.mode.(2: No such file or directory)means the socket does not exist. The service may be down, or thelistenpath differs from the one in the Nginx configuration.- The pool fails to start after an edit. Run
php-fpm8.3 -tand read the message. Typical causes are a missing user, a duplicate pool name, or inconsistentpm.*values.
Exercise
A pool is configured with pm = dynamic, pm.max_children = 20, and workers that average 80 MB each. The server has 4 GB of RAM, and you have reserved 1 GB for the operating system, Nginx, and the database. Is this configuration safe, and what value would you set?
Show answer
Not safe. 20 workers × 80 MB = 1600 MB, but only 3 GB (about 3072 MB) is left for PHP, so the cap actually fits: 3072 / 80 is about 38. The configuration will not run out of memory. It is safe, but it may be too low. With these numbers you could raise pm.maxchildren to around 35, keeping a small margin, and then adjust pm.startservers, pm.minspareservers, and pm.maxspareservers so that minspare ≤ start ≤ maxspare ≤ max_children. Verify with the ps command under real load before relying on the number.
Exercise
Nginx logs "connect() to unix:/run/php/php8.3-fpm-example.sock failed (13: Permission denied)". Which pool settings do you check first, and why?
Show answer
Check listen.owner, listen.group, and listen.mode. Nginx runs as www-data on Debian, and a Unix socket can only be opened by users the owner, group, and mode allow. Set listen.owner = www-data (or listen.group = www-data with listen.mode = 0660), then reload php8.3-fpm.