Repository navigation
[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
Draft
[FEATURE] Size PHP-FPM to the CPUs and memory of the container and raise the production defaults#660Kanti wants to merge 1 commit into
Kanti wants to merge 1 commit into
Conversation
… 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
changed the base branch from
feature/cron-jobs-from-environment
to
master
November 23, 2026 08:18
Kanti
force-pushed
the
task/production-defaults
branch
from
November 23, 2026 08:18
9dfda03 to
9e0e81f
Compare
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



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 ontomasterwithout conflicts:make generatechanges nothing, and the PHPUnit tests and PHPStan ofci/pass.Why
The PHP-FPM pool kept the values of
www.confof 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 saidserver 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_fileswas7963, less than PHP's own default of10000. Once the key table is full, OPcache caches no further file (cache_fullistrue,opcache_is_script_cached()of the next filefalse), so in a project with more files, those are compiled at every request.pm.max_requestswas0: 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 tozz-environment.conf, the entrypoint sizes the pool to the CPUs and the memory of the container:cpu.max, else cgroup v1cpu.cfs_quota_us/cpu.cfs_period_us), rounded up, at mostnproc(which followscpuset); without a limit,nproc.memory.max, else cgroup v1memory.limit_in_bytes); without one, or with one aboveMemTotal,MemTotal.pm.max_children: the smaller of(memory - opcache.memory_consumption - 256 MB) / FPM_AUTO_CHILD_MEMORYandCPUs * 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_servershalf the CPUs (at least 1),pm.max_spare_serverstwo per CPU (at mostpm.max_children),pm.start_servershalfway between them, as PHP-FPM computes it when it is not set.FPM_PM_*orfpm.pool.pm.*stays as set, and the others are computed to fit it:FPM_PM_MAX_CHILDREN: "3"lowerspm.max_spare_serversto 3,FPM_PM_MIN_SPARE_SERVERS: "6"raisespm.max_childrento 6.FPM_AUTO_SIZE=0, or when a pool file setspmor one of the four numbers to another value than the image (anapplication.conf,www.conforzzz-pool.confof your own): then the values of the pool files apply, as before.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).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.confgetspm.max_requests = 1000. It goes there and not tozz-environment.conf, so thatFPM_MAX_REQUESTS,fpm.pool.pm.max_requestsand pool files of your own still win.php.webdevops.iniand the OPcache settings of the development images:opcache.max_accelerated_files = 32531(was7963).opcache.memory_consumptionstays at256; the larger key table takes 1.1 MB more of it.ci/image-tests:Container::run()andImageTestCase::runContainer()take further options ofdocker create, for the limits.docs/environment-variables.md: the defaults ofFPM_PM_*are "computed",FPM_MAX_REQUESTS1000,PHP_OPCACHE_MAX_ACCELERATED_FILES32531; 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 ofLOG_STDERRfrom [BUGFIX] Write the logs of PHP-FPM to the files of LOG_STDOUT and LOG_STDERR #656), and thecompose.yamlexample withcpus: 2andmem_limit: 2ginstead ofFPM_PM_MAX_CHILDREN: "20".docs/customization.md:zz-environment.confalso 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 ofphp,php-nginxandphp-apache, so that their Docker Hub pages say it too.make generate.Changes in behaviour for existing users
FPM_PM_*andfpm.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 withcpus: 2andmem_limit: 1g, 2 withcpus: 1andmem_limit: 512m).FPM_AUTO_SIZE: "0"keeps the old values.pm = dynamicstarts workers only under load, but several busy containers on one host can then use more memory than before. The docs recommendcpusandmem_limitin production.FPM_PM_MAX_CHILDRENset, 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_servers3 is more than 2); now it starts.fpm.pool.pm: static, PHP-FPM starts the computed number of workers at once (16 on 4 CPUs and 16 GB) instead of 5.FPM_MAX_REQUESTS: "0"keeps the workers.pm,pm.max_childrenor 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 = 500inapplication.confno longer finds it; the line is nowpm.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 readsnproc,MemTotaland the cgroup files of the container, computes the expected values and compares them withphp-fpm -tt; the container log has the line with the CPUs, the memory and the values;pm.max_requests = 1000.testPhpFpmPoolIsSizedToTheLimitsOfTheContainer: short runs ofphp-fpm -ttwith--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=64MwithPHP_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 awww.confof your own withpm.max_children = 20(20 / 2 / 1 / 3); the configuration test succeeds in each.testOpcacheSettingsForProductionandtestSettingsForDevelopmentexpectopcache.max_accelerated_files32531.php:8.5(Debian)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)php:8.5-alpinephp-dev:8.5-alpinetestSettingsForDevelopment(7963)php-nginx:8.5-alpineOn 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 inphp:8.5and 4 inphp:8.5-alpine:testPhpVersion,testImagickReportsItsVersion, and on AlpinetestZicIsInstalledandtestDockerImageCleanup. 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=0and the pool file of your own keep the old values, andfpm.pool.pm.max_children=3gives 3 / 2 / 1 / 3 there by chance. All other tests give the same result with and without this change, excepttestPhpErrorLogFromTheEnvironmentandtestPhpFpmPoolDirectivesOfDockerConfFromTheEnvironment, which fail on the published images (inphp-dev:8.5-alpineonly 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 themtestPhpVersionand 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.5andphp: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 filescpu.maxandmemory.maxmounted at/sys/fs/cgroup, and the CI runners use cgroup v2.cpus: 1/512m,2/1g,2/2g, no limits on 4 CPUs and 16 GBphp-fpm -ttwith--cpusand--memoryphp-nginx,php-apache(Debian) andphp-devphp-fpm -ttwith--cpus 2 --memory 1gpm.max_requests = 1000cpu.max150000 100000,memory.max2 GiB; thenmax 100000andmax2 CPUs (cgroup v2 limit), 2048 MB memory (cgroup v2 limit), 8 / 2 / 1 / 4; without limits 16 / 5 / 2 / 8--cpuset-cpus 0FPM_AUTO_SIZE,FPM_AUTO_CHILD_MEMORY,FPM_AUTO_CHILDREN_PER_CPU,PHP_OPCACHE_MEMORY_CONSUMPTION,php.opcache.memory_consumption1G,96m,php.opcache.memory_consumption=768abc,0) logs a warning and the default applies, the container runsFPM_PM_MAX_CHILDREN7 and 3,fpm.pool.pm.max_children=3,FPM_PM_MIN_SPARE_SERVERS=6php-fpm -tsucceedswww.confwithpm.max_children = 20,zzz-pool.confwith3,application.confwith12mounted:ro; awww.confwith the image's values(cgroup v1 limit)here and(cgroup v2 limit)with the mounted filesfpm.pool.pm: staticstarts all workers at oncecompose.yamlof "Production" (php-nginx, image instead ofbuild)docker compose up,docker compose exec app php-fpm -tt, 12 slow requests at onceNanoCpus2000000000,Memory2147483648,pm.max_children = 8; the container log showsserver reached pm.max_children setting (8), consider raising itpm.max_childrengoes to the container log, or to the file ofLOG_STDERR(#656)compose.yamlon top of #658, without and withLOG_STDERR: /app/stderr.logopcache.max_accelerated_files32531ini_get()inphp,php-nginx,php-dev32531;opcache.revalidate_freqstill0inphp-dev, Xdebug still loadedREADME.md,docs/andconf/dockerhub.ymlmake generate, which checks the links of the Docker Hub pages#size-of-the-php-fpm-poolexistsThe values of the computation, measured in
php:8.5andphp: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-alpinewith--cpus 2 --memory 2g,ab -c 24from 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 ofci/(341) pass, PHPStan (level 8) finds nothing, and Rector finds nothing in the changed files. OnImageTestCase.phpit proposes to removeparent::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 ofphp-apacheand the other development images (they get the same scripts fromphpandphp-dev). This pull request's CI runs the new tests on all PHP images on amd64 and arm64.