How PHP-FPM Works
A master process reads the configuration and starts worker processes. Each worker handles one request at a time, then waits for the next. The web server talks to the workers through the FastCGI protocol: Nginx with fastcgi_pass, Apache with mod_proxy_fcgi. Because PHP runs outside the web server, each site can have its own pool, user, and limits. Installation is covered in how to install PHP on Ubuntu.
Where the Files Are
| Item | Debian and Ubuntu | AlmaLinux and Rocky Linux |
|---|---|---|
| Service | php8.3-fpm | php-fpm |
| Pools | /etc/php/8.3/fpm/pool.d/ | /etc/php-fpm.d/ |
| php.ini | /etc/php/8.3/fpm/php.ini | /etc/php.ini |
| Socket | /run/php/php8.3-fpm.sock | /run/php-fpm/www.sock |
| Logs | /var/log/php8.3-fpm.log | /var/log/php-fpm/ |
Replace 8.3 with your version (php -v): 8.3 on Ubuntu 24.04, 8.4 on Debian 13, and 8.5 on Ubuntu 26.04.
A Pool File
The default pool is www.conf. Its main lines on Debian and Ubuntu:
[www]
user = www-data
group = www-data
listen = /run/php/php8.3-fpm.sock
listen.owner = www-data
listen.group = www-data
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
For a second site, copy the file, change the name in brackets, the user, and the socket path, and point that site's web server configuration at the new socket. On AlmaLinux and Rocky Linux the pool runs as apache by default.
The Three pm Modes
- static: always runs
pm.max_childrenworkers. Predictable, and uses that memory all the time. - dynamic: starts
pm.start_serversworkers and keeps betweenpm.min_spare_serversandpm.max_spare_serversidle ones, up topm.max_children. A good default. - ondemand: starts workers only when requests arrive and stops them after
pm.process_idle_timeout. Saves memory on quiet sites; the first request after a pause waits for a worker to start.
pm.max_requests, for example 500, replaces each worker after that many requests, which contains slow memory leaks; the default, 0, never replaces them. The values above are the upstream defaults that Debian and Ubuntu keep; the AlmaLinux and Rocky Linux pool starts with higher numbers.
Sizing pm.max_children
The value is a memory budget. Measure the average size of a worker under normal traffic:
ps --no-headers -o rss -C php-fpm8.3 | awk '{s+=$1; n++} END {print s/n/1024 " MB"}'
Use -C php-fpm on AlmaLinux and Rocky Linux. Then apply this rule of thumb: (memory for PHP) divided by (average worker size). On a 4 GB server, keep about 1 GB for the system and web server and 1 GB for the database. That leaves 2048 MB; at 50 MB per worker, 2048 / 50 is about 40, so set pm.max_children = 40. Set it too high and the server swaps or the OOM killer stops processes; too low, and requests queue. The log then warns that the pool reached pm.max_children, and visitors may see a 503 error.
Socket or TCP
- Unix socket:
listen = /run/php/php8.3-fpm.sock. For a web server on the same machine; access is controlled by the socket's owner and mode. - TCP:
listen = 127.0.0.1:9000. Needed when the web server runs elsewhere. Uselisten.allowed_clientsand the firewall, and never expose the port to the internet.
Status Page and Slow Log
pm.status_path = /fpm-status
slowlog = /var/log/php8.3-fpm-slow.log
request_slowlog_timeout = 5s
Allow the status path only from the server itself, for example with allow 127.0.0.1; and deny all; in its Nginx location. It shows active and idle processes, the listen queue, and how often the pool hit its maximum. The slow log records a stack trace of every request that runs longer than the timeout, which points at the slow code.
Test, Reload, Restart
sudo php-fpm8.3 -t
sudo systemctl reload php8.3-fpm
On AlmaLinux and Rocky Linux: sudo php-fpm -t and sudo systemctl reload php-fpm. Test first: a broken pool file keeps PHP-FPM from starting, and every PHP page fails. A reload makes the master process re-read its configuration and php.ini and replace the workers without stopping the service, so it is enough for pool and php.ini changes. With the default process_control_timeout = 0 in php-fpm.conf, workers are not given time to finish, so a long request can still be cut off; set it to a few seconds, for example 10s, if that matters. Keep a full restart for when a reload is not enough. Upload limits are in how to increase the PHP upload limit.