Skip to content

[FEATURE] Size PHP-FPM to the CPUs and memory of the container and raise the production defaults - #660

Draft
Kanti wants to merge 1 commit into
masterfrom
task/production-defaults
Draft

Kanti wants to merge 1 commit into
masterfrom
task/production-defaults

Conversation

@Kanti

@Kanti Kanti commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Based on master, on its own: it needs none of the other open pull requests. It was first stacked on #658 and #657; the checks below were made there. Rebased onto master without conflicts: make generate changes nothing, and the PHPUnit tests and PHPStan of ci/ pass.

Why

The PHP-FPM pool kept the values of www.conf of the official PHP image: pm.max_children = 5, pm.start_servers = 2, pm.min_spare_servers = 1, pm.max_spare_servers = 3, whatever the container has. So PHP-FPM handled at most five requests at the same time, also on a server with 8 or 16 CPUs; the sixth waited in the listen queue, and the container log said server reached pm.max_children setting (5). On a small container, five workers can be more than its memory holds.

Two more defaults did not fit production use:

  • opcache.max_accelerated_files was 7963, less than PHP's own default of 10000. Once the key table is full, OPcache caches no further file (cache_full is true, opcache_is_script_cached() of the next file false), so in a project with more files, those are compiled at every request.
  • pm.max_requests was 0: a worker that leaks memory, in an extension or in the application, was never replaced.

No upstream issue asks for this.

What

  • provisioning/php/general/provision/entrypoint.d/20-php-fpm.sh: at every start, before the variables are written to zz-environment.conf, the entrypoint sizes the pool to the CPUs and the memory of the container:
    • CPUs: the CPU limit of the container (cgroup v2 cpu.max, else cgroup v1 cpu.cfs_quota_us / cpu.cfs_period_us), rounded up, at most nproc (which follows cpuset); without a limit, nproc.
    • Memory: the memory limit (cgroup v2 memory.max, else cgroup v1 memory.limit_in_bytes); without one, or with one above MemTotal, MemTotal.
    • pm.max_children: the smaller of (memory - opcache.memory_consumption - 256 MB) / FPM_AUTO_CHILD_MEMORY and CPUs * FPM_AUTO_CHILDREN_PER_CPU, at least 2. OPcache is shared by all workers and counted once; the 256 MB are for the web server, supervisord, cron, syslog-ng and the master process of PHP-FPM.
    • pm.min_spare_servers half the CPUs (at least 1), pm.max_spare_servers two per CPU (at most pm.max_children), pm.start_servers halfway between them, as PHP-FPM computes it when it is not set.
    • A value set with FPM_PM_* or fpm.pool.pm.* stays as set, and the others are computed to fit it: FPM_PM_MAX_CHILDREN: "3" lowers pm.max_spare_servers to 3, FPM_PM_MIN_SPARE_SERVERS: "6" raises pm.max_children to 6.
    • Nothing is computed with FPM_AUTO_SIZE=0, or when a pool file sets pm or one of the four numbers to another value than the image (an application.conf, www.conf or zzz-pool.conf of your own): then the values of the pool files apply, as before.
    • One line in the container log, e.g. PHP-FPM: 2 CPUs (cgroup v1 limit), 1024 MB memory (cgroup v1 limit) -> pm.max_children=4, pm.start_servers=2, pm.min_spare_servers=1, pm.max_spare_servers=4; values that are set are marked (set).
    • New variables: FPM_AUTO_SIZE (1), FPM_AUTO_CHILD_MEMORY (128M), FPM_AUTO_CHILDREN_PER_CPU (4). An invalid value of the last two logs a warning and uses the default, so the container still starts.
  • provisioning/php/general/provision/bootstrap.d/20-setup-php-fpm-pool.sh: application.conf gets pm.max_requests = 1000. It goes there and not to zz-environment.conf, so that FPM_MAX_REQUESTS, fpm.pool.pm.max_requests and pool files of your own still win.
  • php.webdevops.ini and the OPcache settings of the development images: opcache.max_accelerated_files = 32531 (was 7963). opcache.memory_consumption stays at 256; the larger key table takes 1.1 MB more of it.
  • ci/image-tests: Container::run() and ImageTestCase::runContainer() take further options of docker create, for the limits.
  • Docs:
    • docs/environment-variables.md: the defaults of FPM_PM_* are "computed", FPM_MAX_REQUESTS 1000, PHP_OPCACHE_MAX_ACCELERATED_FILES 32531; a new section "Size of the PHP-FPM pool" with the formula, examples, the log line, the three new variables, how set values and pool files of your own count, static, and why to set limits.
    • docs/images/php.md: the PHP configuration table; in "Production", a point on CPU and memory limits, the point on PHP-FPM processes for the computed size (with the log file of LOG_STDERR from [BUGFIX] Write the logs of PHP-FPM to the files of LOG_STDOUT and LOG_STDERR #656), and the compose.yaml example with cpus: 2 and mem_limit: 2g instead of FPM_PM_MAX_CHILDREN: "20".
    • docs/customization.md: zz-environment.conf also holds the computed size; it goes there only while no pool file sets the process manager.
    • README.md: two points in the list at the top: PHP-FPM that sizes itself to the container, and the production defaults. The cron example of [FEATURE] Add cron jobs with CRONJOB_<NAME> variables, without an image build #658 at the end of "Quick start" comes with [FEATURE] Add cron jobs with CRONJOB_<NAME> variables, without an image build #658.
    • conf/dockerhub.yml: one sentence in the intros of php, php-nginx and php-apache, so that their Docker Hub pages say it too.
  • make generate.

Changes in behaviour for existing users

  • Without FPM_PM_* and fpm.pool.pm.*, the pool no longer has 5 / 2 / 1 / 3 workers but the computed values: more on most hosts (16 / 5 / 2 / 8 on 4 CPUs and 16 GB without limits), fewer on small containers (4 with cpus: 2 and mem_limit: 1g, 2 with cpus: 1 and mem_limit: 512m). FPM_AUTO_SIZE: "0" keeps the old values.
  • Without limits, every container sizes itself to the whole host. pm = dynamic starts workers only under load, but several busy containers on one host can then use more memory than before. The docs recommend cpus and mem_limit in production.
  • With only FPM_PM_MAX_CHILDREN set, the spare servers are now computed instead of 2 / 1 / 3. FPM_PM_MAX_CHILDREN: "2" alone made PHP-FPM fail before (pm.max_spare_servers 3 is more than 2); now it starts.
  • With fpm.pool.pm: static, PHP-FPM starts the computed number of workers at once (16 on 4 CPUs and 16 GB) instead of 5.
  • A worker ends after 1000 requests and a new one takes its place; what a worker keeps in its own memory between requests starts anew. FPM_MAX_REQUESTS: "0" keeps the workers.
  • OPcache caches up to 32531 files instead of 7963.
  • Images that build on these: a pool file whose pm, pm.max_children or spare servers differ from the image switches the sizing off, so it works as before. A pool file with exactly the image's values (5 / 2 / 1 / 3) does not count as your own: the computed values win over it. A script that looks for the line ;pm.max_requests = 500 in application.conf no longer finds it; the line is now pm.max_requests = 1000.

Tests

Image tests in PhpFpmEnvironmentChecks, which all PHP images use:

  • testPhpFpmPoolIsSizedToTheContainer: in the container of the class, which has no limits, the test reads nproc, MemTotal and the cgroup files of the container, computes the expected values and compares them with php-fpm -tt; the container log has the line with the CPUs, the memory and the values; pm.max_requests = 1000.
  • testPhpFpmPoolIsSizedToTheLimitsOfTheContainer: short runs of php-fpm -tt with --cpus 2 --memory 1g, seven data sets: the limits alone (4 / 2 / 1 / 4), FPM_PM_MAX_CHILDREN=7 (7 / 2 / 1 / 4), fpm.pool.pm.max_children=3 (3 / 2 / 1 / 3), FPM_AUTO_CHILD_MEMORY=64M with PHP_OPCACHE_MEMORY_CONSUMPTION=384 (6 / 2 / 1 / 4), FPM_AUTO_CHILDREN_PER_CPU=1 (2 / 1 / 1 / 2), FPM_AUTO_SIZE=0 (5 / 2 / 1 / 3) and a www.conf of your own with pm.max_children = 20 (20 / 2 / 1 / 3); the configuration test succeeds in each.
  • testOpcacheSettingsForProduction and testSettingsForDevelopment expect opcache.max_accelerated_files 32531.
  • Two docblocks of the existing tests no longer speak of the defaults 5 / 2 / 1 / 3.
Image Published image With this change (published image with the changed scripts, bootstrap scripts run again)
php:8.5 (Debian) fails: testPhpFpmPoolIsSizedToTheContainer (4 CPUs, 16087 MB: expected 16 / 5 / 2 / 8, got 5 / 2 / 1 / 3), 4 of the 7 data sets (the limits: expected 4 / 2 / 1 / 4, got 5 / 2 / 1 / 3), testOpcacheSettingsForProduction (7963) passes
php:8.5-alpine fails the same way passes
php-dev:8.5-alpine fails the same way, and testSettingsForDevelopment (7963) passes
php-nginx:8.5-alpine fails the same way passes

On top of #658, with the generated conf/ of this branch applied to the published image (the changes of #656, #657, #658 and this one, the bootstrap scripts of the pool run again), the whole suite has 2 failures in php:8.5 and 4 in php:8.5-alpine: testPhpVersion, testImagickReportsItsVersion, and on Alpine testZicIsInstalled and testDockerImageCleanup. They fail on the published image too and depend on its older packages. The published image fails 28 and 31 tests of this suite, among them the new ones above and those of #656 to #658.

Three data sets pass on the published image too, as they should: FPM_AUTO_SIZE=0 and the pool file of your own keep the old values, and fpm.pool.pm.max_children=3 gives 3 / 2 / 1 / 3 there by chance. All other tests give the same result with and without this change, except testPhpErrorLogFromTheEnvironment and testPhpFpmPoolDirectivesOfDockerConfFromTheEnvironment, which fail on the published images (in php-dev:8.5-alpine only the second) and pass with the scripts of master: the published images are older than #632 and #654. The other failures in these local runs (8 to 12 per image, among them testPhpVersion and the healthcheck tests) are the same with and without this change and come from the older local images, not from this change.

How it was verified

In php:8.5 and php:8.5-alpine (amd64), with the changed scripts copied over the published ones and the bootstrap scripts run again; the other images where named. The Docker host of these checks uses cgroup v1; cgroup v2 was checked with the files cpu.max and memory.max mounted at /sys/fs/cgroup, and the CI runners use cgroup v2.

Documented statement Check Result on both
The examples: cpus: 1 / 512m, 2 / 1g, 2 / 2g, no limits on 4 CPUs and 16 GB php-fpm -tt with --cpus and --memory 2 / 1 / 1 / 2, 4 / 2 / 1 / 4, 8 / 2 / 1 / 4, 16 / 5 / 2 / 8
The same in php-nginx, php-apache (Debian) and php-dev php-fpm -tt with --cpus 2 --memory 1g 4 / 2 / 1 / 4, pm.max_requests = 1000
cgroup v2 limits count, a CPU limit is rounded up cpu.max 150000 100000, memory.max 2 GiB; then max 100000 and max 2 CPUs (cgroup v2 limit), 2048 MB memory (cgroup v2 limit), 8 / 2 / 1 / 4; without limits 16 / 5 / 2 / 8
At most the CPUs it may run on --cpuset-cpus 0 1 CPU: 4 / 1 / 1 / 2
FPM_AUTO_SIZE, FPM_AUTO_CHILD_MEMORY, FPM_AUTO_CHILDREN_PER_CPU, PHP_OPCACHE_MEMORY_CONSUMPTION, php.opcache.memory_consumption the data sets above; 1G, 96m, php.opcache.memory_consumption=768 as computed; an invalid value (abc, 0) logs a warning and the default applies, the container runs
Set values stay, the others fit them FPM_PM_MAX_CHILDREN 7 and 3, fpm.pool.pm.max_children=3, FPM_PM_MIN_SPARE_SERVERS=6 7 / 2 / 1 / 4, 3 / 2 / 1 / 3, 3 / 2 / 1 / 3, 6 / 6 / 6 / 6; php-fpm -t succeeds
A pool file of your own switches the sizing off www.conf with pm.max_children = 20, zzz-pool.conf with 3, application.conf with 12 mounted :ro; a www.conf with the image's values 20, 3 and 12 with 2 / 1 / 3 and the log line on the pool file; with the image's values 16 / 5 / 2 / 8
The log line container log with supervisord as documented, with (cgroup v1 limit) here and (cgroup v2 limit) with the mounted files
fpm.pool.pm: static starts all workers at once worker processes 16
compose.yaml of "Production" (php-nginx, image instead of build) docker compose up, docker compose exec app php-fpm -tt, 12 slow requests at once NanoCpus 2000000000, Memory 2147483648, pm.max_children = 8; the container log shows server reached pm.max_children setting (8), consider raising it
The warning on pm.max_children goes to the container log, or to the file of LOG_STDERR (#656) the same compose.yaml on top of #658, without and with LOG_STDERR: /app/stderr.log in the container log, or only in the file; the line of the size of the pool stays in the container log
A worker ends after 1000 requests 1201 requests to a pool with one worker, the process id of each request 1001 is the first of a new worker
opcache.max_accelerated_files 32531 ini_get() in php, php-nginx, php-dev 32531; opcache.revalidate_freq still 0 in php-dev, Xdebug still loaded
Links in README.md, docs/ and conf/dockerhub.yml make generate, which checks the links of the Docker Hub pages the heading #size-of-the-php-fpm-pool exists

The values of the computation, measured in php:8.5 and php:8.5-alpine:

  • Memory of a worker: after a request that loads 1170 classes of Symfony, Twig, PHPUnit and nikic/php-parser through the Composer autoloader, a worker holds 15 to 18 MB of its own memory (RSS 40 to 49 MB, the rest mostly OPcache, which is shared); after a request with 18 MB of data 22 MB, after one with 90 MB 62 MB, and 38 MB once that request ended. 128 MB per worker leaves room above that; memory_limit (512 MB) would allow only 1 worker in 1 GB.

  • The other processes: supervisord, syslog-ng, cron, Nginx or Apache and the master process of PHP-FPM together have 37 to 39 MB (PSS); the memory of the idle container is 47 to 57 MB. The 256 MB leave room for cron jobs, Postfix in the development images and the page cache.

  • OPcache: the 2341 library files of these packages take 29.6 MB of OPcache, about 13 KB per file, so the 256 MB hold about 18,000 such files. With 7963 keys, the key table filled up long before the memory; with 32531, the memory is the limit. The key table of 32531 takes 1.1 MB more than the one of 7963 (17.49 MB instead of 16.36 MB used at start).

  • Throughput: php-nginx:8.5-alpine with --cpus 2 --memory 2g, ab -c 24 from a second container, five runs on a busy shared host (load around 11 on 4 CPUs). A request with 10 ms CPU time and 40 ms of waiting: 59 to 93 requests per second with 5 workers (FPM_AUTO_SIZE=0), 103 to 125 with the computed 8. A request with 50 ms of CPU time only: 26 to 38 with 5 workers, 24 to 38 with 8, no loss within the noise.

  • Checks of ci/: the PHPUnit tests of ci/ (341) pass, PHPStan (level 8) finds nothing, and Rector finds nothing in the changed files. On ImageTestCase.php it proposes to remove parent::tearDown(), the same on master, in a line this pull request does not change.

  • Not verified: the computation on a host with cgroup v2 (only with the mounted files; the CI runners have cgroup v2), on a host with more than 4 CPUs, with --cgroupns=host, on arm64, PHP versions other than 8.5, and the image tests of php-apache and the other development images (they get the same scripts from php and php-dev). This pull request's CI runs the new tests on all PHP images on amd64 and arm64.

… the production defaults

The pool kept the values of www.conf of the official PHP image,
pm.max_children 5, whatever the container has: on a server with 8 or 16
CPUs, the sixth PHP request waited in the listen queue. OPcache took at
most 7963 files, fewer than PHP's own default of 10000; in a project
with more files, the files beyond it were compiled at every request.

At every start, the entrypoint now computes pm.max_children from the
CPU and memory limits of the container (cgroup v2 or v1, else nproc and
MemTotal): the smaller of the workers of FPM_AUTO_CHILD_MEMORY (128M)
that fit beside OPcache and 256 MB for the other processes, and of
FPM_AUTO_CHILDREN_PER_CPU (4) per CPU, at least 2. The spare servers
follow the CPUs, pm.start_servers is PHP-FPM's own default between
them. The values go to zz-environment.conf, and the container log names
them. FPM_PM_* and fpm.pool.pm.* stay as set, and the other values are
computed to fit them. Nothing is computed with FPM_AUTO_SIZE=0, or when a
pool file sets pm or a number of workers to another value than the
image, so an application.conf or www.conf of your own works as before.

application.conf now sets pm.max_requests = 1000, so that a worker that
leaks memory is replaced, and opcache.max_accelerated_files is 32531, in
the development images too.

Image tests in PhpFpmEnvironmentChecks check the values in the container
of the class against its CPUs and memory, and in short runs with --cpus 2
--memory 1g with the new variables, explicit values, FPM_AUTO_SIZE=0 and
a pool file of one's own. runContainer() takes options of docker create
for the limits.
@Kanti
Kanti removed this pull request from stack #659 November 23, 2026 08:17
@Kanti
Kanti changed the base branch from feature/cron-jobs-from-environment to master November 23, 2026 08:18
@Kanti
Kanti force-pushed the task/production-defaults branch from 9dfda03 to 9e0e81f Compare November 23, 2026 08:18
@sonarqubecloud

Copy link
Copy Markdown

@Kanti Kanti changed the title [TASK] Size PHP-FPM to the CPUs and memory of the container and raise the production defaults [FEATURE] Size PHP-FPM to the CPUs and memory of the container and raise the production defaults Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant