PHP-FPM configuration
Learn PHP-FPM pool settings and the process manager.
The PHP-FPM pool
PHP-FPM handles requests with worker processes. The pm = dynamic process manager keeps the pool small when idle and grows it under load.
pm.max_children sets the upper bound of concurrent workers. memory_limit is the most memory each worker may consume.
Wrong settings cause 502 Bad Gateway or out-of-memory crashes; pool configuration is critical in production.
Worker counts and memory
A per-worker memory_limit = 128M is enough for typical APIs; heavy job queues need 256M or more.
pm.start_servers is the worker count to start at boot. pm.min_spare_servers and pm.max_spare_servers bound the idle worker range.
Rough guide: pm.max_children × memory_limit must not exceed server RAM. If it does, the OS starts killing processes.
Exercise Generate PHP-FPM pool configuration
Build a PHP-FPM pool config text with PHP: lines pm = dynamic, pm.max_children = 20, pm.start_servers = 4 and memory_limit = 128M. Write it to php-fpm-pool.conf via file_put_contents and echo the text.
Show example solution
Build $cfg with single-quoted parts joined by "\n". Then call file_put_contents("php-fpm-pool.conf", $cfg); and echo $cfg;.
Example solution:
<?php
// Generate PHP-FPM pool config
$cfg = 'pm = dynamic' . "\n" .
'pm.max_children = 20' . "\n" .
'pm.start_servers = 4' . "\n" .
'memory_limit = 128M' . "\n";
file_put_contents("php-fpm-pool.conf", $cfg);
echo $cfg;
Test yourself
-
Write the memory_limit value
<?php // 128 MB per worker $cfg = ["memory_limit" => "____"]; print_r($cfg);Show answer
<?php // 128 MB per worker $cfg = ["memory_limit" => "128M"]; print_r($cfg); -
Which PHP-FPM setting caps memory per worker?
- memory_limit
- pm.max_children
- max_execution_time
- upload_max_filesize
Show answer
memory_limit — Correct. memory_limit is the most memory each worker may use.
-
Match the PHP-FPM directives
- pm.max_children
- Maximum worker count
- memory_limit
- Memory per worker
- pm.start_servers
- Workers at startup
- pm = dynamic
- Dynamic process manager