diff --git a/README.md b/README.md index 5a6844f7b..f97adad27 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ We add additional stuff like: * almost any PHP module preinstalled * configuration based on ENV variables * run multiple services (like php-fpm and nginx) with supervisord +* PHP-FPM that sizes itself to the container: at every start, the number of PHP-FPM workers follows the CPU and memory limits of the container (`cpus`, `mem_limit`), or the host without limits ([details](docs/environment-variables.md#size-of-the-php-fpm-pool)) +* production defaults for larger applications: `opcache.max_accelerated_files` `32531`, and PHP-FPM replaces a worker after 1000 requests ([production](docs/images/php.md#production)) [![GitHub issues](https://img.shields.io/github/issues/webdevops/Dockerfile.svg)](https://github.com/webdevops/Dockerfile/issues)[![GitHub forks](https://img.shields.io/github/forks/webdevops/Dockerfile.svg)](https://github.com/webdevops/Dockerfile/network)[![GitHub stars](https://img.shields.io/github/stars/webdevops/Dockerfile.svg)](https://github.com/webdevops/Dockerfile/stargazers)[![GitHub license](https://img.shields.io/github/license/webdevops/Dockerfile.svg)](https://github.com/webdevops/Dockerfile/blob/master/LICENSE) [![build](https://github.com/webdevops/Dockerfile/actions/workflows/build.yaml/badge.svg)](https://github.com/webdevops/Dockerfile/actions/workflows/build.yaml) diff --git a/ci/image-tests/Checks/PhpDevelopmentChecks.php b/ci/image-tests/Checks/PhpDevelopmentChecks.php index 91e3f9907..39eca8dab 100644 --- a/ci/image-tests/Checks/PhpDevelopmentChecks.php +++ b/ci/image-tests/Checks/PhpDevelopmentChecks.php @@ -43,7 +43,7 @@ public function testSettingsForDevelopment(): void 'opcache.revalidate_freq' => '0', 'opcache.validate_timestamps' => '1', 'opcache.interned_strings_buffer' => '16', - 'opcache.max_accelerated_files' => '7963', + 'opcache.max_accelerated_files' => '32531', ], $this->phpSettings([ 'xdebug.mode', 'xdebug.start_with_request', 'opcache.memory_consumption', 'opcache.revalidate_freq', 'opcache.validate_timestamps', 'opcache.interned_strings_buffer', 'opcache.max_accelerated_files', diff --git a/ci/image-tests/Checks/PhpFpmEnvironmentChecks.php b/ci/image-tests/Checks/PhpFpmEnvironmentChecks.php index 6454f57a8..db5af407c 100644 --- a/ci/image-tests/Checks/PhpFpmEnvironmentChecks.php +++ b/ci/image-tests/Checks/PhpFpmEnvironmentChecks.php @@ -4,18 +4,28 @@ namespace Webdevops\Build\ImageTests\Checks; +use PHPUnit\Framework\Attributes\DataProvider; use Webdevops\Build\ImageTests\Docker\Container; use Webdevops\Build\ImageTests\ImageTestCase; use function array_map; +use function ctype_digit; +use function escapeshellarg; +use function explode; +use function implode; +use function intdiv; use function json_decode; use function max; use function microtime; +use function min; use function preg_match; +use function preg_quote; use function range; use function sleep; use function sprintf; +use function trim; use function usleep; +use function vsprintf; /** * The PHP-FPM variables, see docs/environment-variables.md#php-fpm, and LOG_STDOUT and LOG_STDERR for PHP-FPM, see @@ -67,7 +77,7 @@ public function testPhpFpmRequestTerminateTimeoutFromTheEnvironment(): void /** * FPM_PROCESS_MAX=2: with four slow requests at once, PHP-FPM runs no more than two workers, although the pool would - * allow five. + * allow more. */ public function testPhpFpmProcessMaxFromTheEnvironment(): void { @@ -160,7 +170,8 @@ public function testPhpFpmPoolDirectivesOfDockerConfFromTheEnvironment(): void /** * FPM_PM_START_SERVERS=3, FPM_PM_MIN_SPARE_SERVERS=3, FPM_PM_MAX_SPARE_SERVERS=4 and FPM_PM_MAX_CHILDREN=8, in a * container of their own: three workers at the start, three idle ones besides three busy ones, eight at most - * under load, and four once the load is gone. The defaults would give 2, 4, 5 and 3. + * under load, and four once the load is gone. The values of the official image, with FPM_AUTO_SIZE=0, would give 2, + * 4, 5 and 3. */ public function testPhpFpmProcessManagerFromTheEnvironment(): void { @@ -209,6 +220,73 @@ public function testPhpFpmMaxRequestsAndIdleTimeoutFromTheEnvironment(): void self::assertSame(0, $this->phpFpmWorkers($container), 'the idle worker exits after 5 s'); } + /** + * In the container of the class, which has no CPU or memory limit, the process manager is sized to the CPUs and the + * memory of the runner: pm.max_children is the smaller of the workers of 128 MB that fit into the memory beside + * OPcache (256 MB) and 256 MB for the other processes, and of four per CPU, at least two; the spare servers follow + * the CPUs, and pm.start_servers lies in between. The container log names the CPUs, the memory and the values. A + * worker ends after 1000 requests. + */ + public function testPhpFpmPoolIsSizedToTheContainer(): void + { + $container = self::container(); + [$cpus, $memory] = $this->containerResources($container); + $children = max(2, min(intdiv($memory - 256 - 256, 128), 4 * $cpus)); + $minSpare = max(1, min(intdiv($cpus, 2), $children)); + $maxSpare = max($minSpare, min($children, 2 * $cpus)); + $expected = [$children, $minSpare + intdiv($maxSpare - $minSpare, 2), $minSpare, $maxSpare]; + + $configuration = $this->assertCommandSucceeds('php-fpm -tt')->stderr; + self::assertSame($expected, self::phpFpmProcessManager($configuration), sprintf('%d CPUs, %d MB memory', $cpus, $memory)); + self::assertMatchesRegularExpression('/NOTICE: \tpm\.max_requests = 1000$/m', $configuration); + self::assertMatchesRegularExpression(vsprintf( + '/^PHP-FPM: %d CPUs \([^)]+\), %d MB memory \([^)]+\) -> pm\.max_children=%d, pm\.start_servers=%d, pm\.min_spare_servers=%d, pm\.max_spare_servers=%d$/m', + [$cpus, $memory, ...$expected], + ), $container->logs()); + } + + /** + * Settings of the sizing, see testPhpFpmPoolIsSizedToTheLimitsOfTheContainer(): the environment, a pool file + * www.conf of your own if not empty, and pm.max_children, pm.start_servers, pm.min_spare_servers and + * pm.max_spare_servers. + * + * @return iterable, string, list}> + */ + public static function phpFpmPoolSizes(): iterable + { + yield 'the limits' => [[], '', [4, 2, 1, 4]]; + yield 'FPM_PM_MAX_CHILDREN' => [['FPM_PM_MAX_CHILDREN' => '7'], '', [7, 2, 1, 4]]; + yield 'fpm.pool.pm.max_children' => [['fpm.pool.pm.max_children' => '3'], '', [3, 2, 1, 3]]; + yield 'FPM_AUTO_CHILD_MEMORY and PHP_OPCACHE_MEMORY_CONSUMPTION' => [['FPM_AUTO_CHILD_MEMORY' => '64M', 'PHP_OPCACHE_MEMORY_CONSUMPTION' => '384'], '', [6, 2, 1, 4]]; + yield 'FPM_AUTO_CHILDREN_PER_CPU' => [['FPM_AUTO_CHILDREN_PER_CPU' => '1'], '', [2, 1, 1, 2]]; + yield 'FPM_AUTO_SIZE' => [['FPM_AUTO_SIZE' => '0'], '', [5, 2, 1, 3]]; + yield 'a pool file of your own' => [[], "[www]\npm.max_children = 20\n", [20, 2, 1, 3]]; + } + + /** + * With --cpus 2 --memory 1g, in short runs of php-fpm -tt: (1024 MB - 256 MB of OPcache - 256 MB) / 128 MB give + * four workers, two CPUs eight, so pm.max_children is 4, pm.min_spare_servers 1 (half the CPUs), + * pm.max_spare_servers 4 (two per CPU, at most pm.max_children) and pm.start_servers 2. A value that is set stays, + * and the others fit it: with seven workers, still at most four spare ones; with three, three. + * FPM_AUTO_CHILD_MEMORY=64M and PHP_OPCACHE_MEMORY_CONSUMPTION=384 give (1024 - 384 - 256) / 64 = 6 workers, + * FPM_AUTO_CHILDREN_PER_CPU=1 two. FPM_AUTO_SIZE=0 and a pool file of your own keep the values of the pool files. + * The configuration test succeeds. + * + * @param array $environment + * @param list $expected + */ + #[DataProvider('phpFpmPoolSizes')] + public function testPhpFpmPoolIsSizedToTheLimitsOfTheContainer(array $environment, string $poolFile, array $expected): void + { + // the pool file has to be there when the entrypoint sizes the pool, so the command runs the entrypoint again + $command = $poolFile === '' ? ['php-fpm', '-tt'] : [ + 'bash', '-c', 'printf %s ' . escapeshellarg($poolFile) . ' > /opt/docker/etc/php/fpm/pool.d/www.conf && exec /entrypoint php-fpm -tt', + ]; + $result = $this->runContainer($command, $environment, ['--cpus', '2', '--memory', '1g']); + self::assertSame(0, $result->exitCode, $result->describe()); + self::assertSame($expected, self::phpFpmProcessManager($result->stderr)); + } + /** * Sends $requests requests that sleep for $seconds to PHP-FPM at once, and returns the largest number of workers * seen until $within seconds after they started. Waits until the requests ended. @@ -233,4 +311,49 @@ private function phpFpmWorkersDuring(Container $container, int $requests, int $s return $workers; } + + /** + * The CPUs and the memory in MB of $container as the entrypoint sees them: the CPUs it may run on and the memory of + * the host, or the limits of cgroup v2 or v1 where they are lower. A CPU limit is rounded up. + * + * @return array{int, int} + */ + private function containerResources(Container $container): array + { + $lines = explode("\n", trim($this->stdout( + "nproc; awk '/^MemTotal:/ { print \$2 }' /proc/meminfo;" + . ' cat /sys/fs/cgroup/cpu.max 2>/dev/null || echo "$(cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us) $(cat /sys/fs/cgroup/cpu/cpu.cfs_period_us)";' + . ' cat /sys/fs/cgroup/memory.max 2>/dev/null || cat /sys/fs/cgroup/memory/memory.limit_in_bytes', + $container, + ))); + self::assertCount(4, $lines, implode("\n", $lines)); + $cpus = (int) $lines[0]; + $memory = intdiv((int) $lines[1], 1024); + [$quota, $period] = explode(' ', $lines[2]) + ['', '']; + if (ctype_digit($quota) && ctype_digit($period) && (int) $period > 0) { + $cpus = min($cpus, intdiv((int) $quota + (int) $period - 1, (int) $period)); + } + + if (ctype_digit($lines[3])) { + $memory = min($memory, intdiv((int) $lines[3], 1048576)); + } + + return [$cpus, $memory]; + } + + /** + * pm.max_children, pm.start_servers, pm.min_spare_servers and pm.max_spare_servers in the output of php-fpm -tt. + * + * @return list + */ + private static function phpFpmProcessManager(string $configuration): array + { + return array_map(static function (string $directive) use ($configuration): int { + if (preg_match('/NOTICE: \t' . preg_quote($directive, '/') . ' = (\d+)$/m', $configuration, $matches) !== 1) { + self::fail($directive . ' should be in the configuration: ' . $configuration); + } + + return (int) $matches[1]; + }, ['pm.max_children', 'pm.start_servers', 'pm.min_spare_servers', 'pm.max_spare_servers']); + } } diff --git a/ci/image-tests/Checks/PhpProductionChecks.php b/ci/image-tests/Checks/PhpProductionChecks.php index 8a0397a5f..fa491c40c 100644 --- a/ci/image-tests/Checks/PhpProductionChecks.php +++ b/ci/image-tests/Checks/PhpProductionChecks.php @@ -37,7 +37,7 @@ public function testOpcacheSettingsForProduction(): void self::assertSame([ 'opcache.memory_consumption' => '256', 'opcache.interned_strings_buffer' => '16', - 'opcache.max_accelerated_files' => '7963', + 'opcache.max_accelerated_files' => '32531', ], $this->phpSettings(['opcache.memory_consumption', 'opcache.interned_strings_buffer', 'opcache.max_accelerated_files'])); } } diff --git a/ci/image-tests/Docker/Container.php b/ci/image-tests/Docker/Container.php index 35c11df6c..8bd4fb066 100644 --- a/ci/image-tests/Docker/Container.php +++ b/ci/image-tests/Docker/Container.php @@ -77,16 +77,18 @@ public static function start(string $image, array $directories, array $labels = /** * Creates a container like start(), runs it until its command exits and removes it. Returns the output and the - * exit code of the command, e.g. of ['root', 'id', '-un'] through the entrypoint of the image. + * exit code of the command, e.g. of ['root', 'id', '-un'] through the entrypoint of the image. $options are further + * options of docker create, e.g. ['--cpus', '2']. * * @param list $directories * @param array $labels * @param array $environment * @param list $command + * @param list $options */ - public static function run(string $image, array $directories, array $labels, array $environment, array $command): CommandResult + public static function run(string $image, array $directories, array $labels, array $environment, array $command, array $options = []): CommandResult { - $container = self::create([], $image, $directories, $labels, $environment, $command); + $container = self::create($options, $image, $directories, $labels, $environment, $command); try { $process = new Process(['docker', 'start', '--attach', $container->id]); $process->setTimeout(self::COMMAND_TIMEOUT); diff --git a/ci/image-tests/ImageTestCase.php b/ci/image-tests/ImageTestCase.php index 92bd9c3a9..e46e943d1 100644 --- a/ci/image-tests/ImageTestCase.php +++ b/ci/image-tests/ImageTestCase.php @@ -345,14 +345,16 @@ protected function commandLines(?Container $container = null): array /** * Runs $command in a new container of the image, through its entrypoint, and returns the result once the command - * exits. The container gets the files of Fixtures/ too and is removed afterwards. + * exits. The container gets the files of Fixtures/ too and is removed afterwards. $options are further options of + * docker create, e.g. limits such as ['--cpus', '2']. * * @param list $command * @param array $environment + * @param list $options */ - protected function runContainer(array $command, array $environment = []): CommandResult + protected function runContainer(array $command, array $environment = [], array $options = []): CommandResult { - return Container::run(self::image()->reference, [self::FIXTURES, self::HOOKS], self::labels(), $environment, $command); + return Container::run(self::image()->reference, [self::FIXTURES, self::HOOKS], self::labels(), $environment, $command, $options); } /** diff --git a/conf/dockerhub.yml b/conf/dockerhub.yml index 3a77d47be..302e46740 100644 --- a/conf/dockerhub.yml +++ b/conf/dockerhub.yml @@ -70,7 +70,7 @@ built: - { label: 'Known issues', path: 'docs/known-issues.md' } php: short: 'PHP-FPM on the official PHP image, configured with environment variables; Debian and Alpine' - intro: 'PHP-FPM on top of the official PHP image, with the webdevops entrypoint, `supervisord`, cron, syslog-ng and the `application` user. PHP-FPM listens on port `9000`. For a web server in the same container, use [`webdevops/php-nginx`](https://hub.docker.com/r/webdevops/php-nginx) or [`webdevops/php-apache`](https://hub.docker.com/r/webdevops/php-apache).' + intro: 'PHP-FPM on top of the official PHP image, with the webdevops entrypoint, `supervisord`, cron, syslog-ng and the `application` user. PHP-FPM listens on port `9000`. At every start, PHP-FPM sizes its pool of workers to the CPU and memory limits of the container, or to the host without limits, see [Size of the PHP-FPM pool](docs/environment-variables.md#size-of-the-php-fpm-pool). For a web server in the same container, use [`webdevops/php-nginx`](https://hub.docker.com/r/webdevops/php-nginx) or [`webdevops/php-apache`](https://hub.docker.com/r/webdevops/php-apache).' note: 'Always use one of the tags below: an image without a tag (`latest`) and the `alpine` tag are old builds from 2019 and 2020 that get no updates, see [Known issues](docs/known-issues.md#latest-and-alpine-tags-of-the-php-images).' example: lang: 'yaml' @@ -113,7 +113,7 @@ built: - { label: 'Known issues', path: 'docs/known-issues.md' } php-nginx: short: 'PHP-FPM and Nginx in one container on the official PHP image; Debian and Alpine' - intro: '[`webdevops/php`](https://hub.docker.com/r/webdevops/php) plus Nginx. Nginx serves `/app` and passes PHP requests to PHP-FPM in the same container. Ports: `80` (HTTP) and `443` (HTTPS with a self-signed certificate).' + intro: '[`webdevops/php`](https://hub.docker.com/r/webdevops/php) plus Nginx. Nginx serves `/app` and passes PHP requests to PHP-FPM in the same container. Ports: `80` (HTTP) and `443` (HTTPS with a self-signed certificate). At every start, PHP-FPM sizes its pool of workers to the CPU and memory limits of the container, or to the host without limits, see [Size of the PHP-FPM pool](docs/environment-variables.md#size-of-the-php-fpm-pool).' note: 'Always use one of the tags below: an image without a tag (`latest`) and the `alpine` tag are old builds from 2019 and 2020 that get no updates, see [Known issues](docs/known-issues.md#latest-and-alpine-tags-of-the-php-images).' example: lang: 'yaml' @@ -158,7 +158,7 @@ built: - { label: 'Known issues', path: 'docs/known-issues.md' } php-apache: short: 'PHP-FPM and Apache in one container on the official PHP image; Debian and Alpine' - intro: '[`webdevops/php`](https://hub.docker.com/r/webdevops/php) plus Apache. Apache serves `/app` and passes PHP requests to PHP-FPM in the same container. Ports: `80` (HTTP) and `443` (HTTPS with a self-signed certificate).' + intro: '[`webdevops/php`](https://hub.docker.com/r/webdevops/php) plus Apache. Apache serves `/app` and passes PHP requests to PHP-FPM in the same container. Ports: `80` (HTTP) and `443` (HTTPS with a self-signed certificate). At every start, PHP-FPM sizes its pool of workers to the CPU and memory limits of the container, or to the host without limits, see [Size of the PHP-FPM pool](docs/environment-variables.md#size-of-the-php-fpm-pool).' note: 'Always use one of the tags below: an image without a tag (`latest`) and the `alpine` tag are old builds from 2019 and 2020 that get no updates, see [Known issues](docs/known-issues.md#latest-and-alpine-tags-of-the-php-images).' example: lang: 'yaml' diff --git a/docker/php-apache-dev/8.2-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-apache-dev/8.2-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-apache-dev/8.2-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-apache-dev/8.2-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-apache-dev/8.2/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-apache-dev/8.2/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-apache-dev/8.2/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-apache-dev/8.2/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-apache-dev/8.3-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-apache-dev/8.3-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-apache-dev/8.3-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-apache-dev/8.3-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-apache-dev/8.3/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-apache-dev/8.3/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-apache-dev/8.3/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-apache-dev/8.3/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-apache-dev/8.4-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-apache-dev/8.4-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-apache-dev/8.4-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-apache-dev/8.4-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-apache-dev/8.4/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-apache-dev/8.4/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-apache-dev/8.4/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-apache-dev/8.4/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-apache-dev/8.5-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-apache-dev/8.5-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-apache-dev/8.5-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-apache-dev/8.5-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-apache-dev/8.5/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-apache-dev/8.5/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-apache-dev/8.5/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-apache-dev/8.5/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-apache-dev/8.6-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-apache-dev/8.6-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-apache-dev/8.6-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-apache-dev/8.6-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-apache-dev/8.6/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-apache-dev/8.6/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-apache-dev/8.6/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-apache-dev/8.6/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-dev/8.2-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-dev/8.2-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-dev/8.2-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-dev/8.2-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-dev/8.2/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-dev/8.2/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-dev/8.2/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-dev/8.2/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-dev/8.3-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-dev/8.3-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-dev/8.3-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-dev/8.3-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-dev/8.3/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-dev/8.3/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-dev/8.3/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-dev/8.3/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-dev/8.4-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-dev/8.4-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-dev/8.4-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-dev/8.4-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-dev/8.4/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-dev/8.4/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-dev/8.4/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-dev/8.4/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-dev/8.5-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-dev/8.5-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-dev/8.5-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-dev/8.5-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-dev/8.5/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-dev/8.5/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-dev/8.5/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-dev/8.5/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-dev/8.6-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-dev/8.6-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-dev/8.6-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-dev/8.6-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-dev/8.6/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-dev/8.6/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-dev/8.6/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-dev/8.6/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-nginx-dev/8.2-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-nginx-dev/8.2-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-nginx-dev/8.2-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-nginx-dev/8.2-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-nginx-dev/8.2/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-nginx-dev/8.2/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-nginx-dev/8.2/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-nginx-dev/8.2/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-nginx-dev/8.3-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-nginx-dev/8.3-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-nginx-dev/8.3-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-nginx-dev/8.3-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-nginx-dev/8.3/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-nginx-dev/8.3/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-nginx-dev/8.3/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-nginx-dev/8.3/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-nginx-dev/8.4-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-nginx-dev/8.4-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-nginx-dev/8.4-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-nginx-dev/8.4-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-nginx-dev/8.4/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-nginx-dev/8.4/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-nginx-dev/8.4/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-nginx-dev/8.4/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-nginx-dev/8.5-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-nginx-dev/8.5-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-nginx-dev/8.5-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-nginx-dev/8.5-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-nginx-dev/8.5/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-nginx-dev/8.5/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-nginx-dev/8.5/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-nginx-dev/8.5/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-nginx-dev/8.6-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-nginx-dev/8.6-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-nginx-dev/8.6-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-nginx-dev/8.6-alpine/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php-nginx-dev/8.6/conf/provision/bootstrap.d/20-php-module-opcache.sh b/docker/php-nginx-dev/8.6/conf/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/docker/php-nginx-dev/8.6/conf/provision/bootstrap.d/20-php-module-opcache.sh +++ b/docker/php-nginx-dev/8.6/conf/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/docker/php/8.2-alpine/conf/etc/php/php.webdevops.ini b/docker/php/8.2-alpine/conf/etc/php/php.webdevops.ini index 829c21e23..8b134c192 100644 --- a/docker/php/8.2-alpine/conf/etc/php/php.webdevops.ini +++ b/docker/php/8.2-alpine/conf/etc/php/php.webdevops.ini @@ -33,4 +33,4 @@ mysqli.default_host = mysql opcache.memory_consumption = 256 opcache.interned_strings_buffer = 16 -opcache.max_accelerated_files = 7963 +opcache.max_accelerated_files = 32531 diff --git a/docker/php/8.2-alpine/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh b/docker/php/8.2-alpine/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh index e04580266..de4f1cc83 100644 --- a/docker/php/8.2-alpine/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh +++ b/docker/php/8.2-alpine/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh @@ -47,3 +47,9 @@ go-replace --mode=line --regex \ go-replace --mode=lineinfile --regex \ -s '^[\s;]*clear_env[\s]*=' -r 'clear_env = no' \ -- /opt/docker/etc/php/fpm/pool.d/application.conf + +# A worker ends after 1000 requests and a new one takes its place, so that memory that leaks in a PHP extension or in +# the application does not add up +go-replace --mode=lineinfile --regex \ + -s '^[\s;]*pm.max_requests[\s]*=' -r 'pm.max_requests = 1000' \ + -- /opt/docker/etc/php/fpm/pool.d/application.conf diff --git a/docker/php/8.2-alpine/conf/provision/entrypoint.d/20-php-fpm.sh b/docker/php/8.2-alpine/conf/provision/entrypoint.d/20-php-fpm.sh index dfbc12179..9563b51df 100644 --- a/docker/php/8.2-alpine/conf/provision/entrypoint.d/20-php-fpm.sh +++ b/docker/php/8.2-alpine/conf/provision/entrypoint.d/20-php-fpm.sh @@ -31,6 +31,172 @@ FPM_POOL_ENVIRONMENT_CONF=/opt/docker/etc/php/fpm/pool.d/zz-environment.conf echo '; container env settings, written at every start' > "$FPM_POOL_ENVIRONMENT_CONF" echo '[www]' >> "$FPM_POOL_ENVIRONMENT_CONF" +# Whether a pool file sets the process manager to other values than application.conf of the image, which has those of +# www.conf of the official PHP image: e.g. an application.conf or a www.conf of your own. zz-environment.conf does not +# count, it is being written. +fpmPoolFilesSetTheProcessManager() { + local file + local files=(/dev/null) + for file in /opt/docker/etc/php/fpm/pool.d/*.conf; do + if [[ "$file" != "$FPM_POOL_ENVIRONMENT_CONF" ]]; then + files+=("$file") + fi + done + + awk ' + BEGIN { + split("pm=dynamic pm.max_children=5 pm.start_servers=2 pm.min_spare_servers=1 pm.max_spare_servers=3", values, " ") + for (i in values) image[values[i]] = 1 + } + { sub(/;.*/, ""); gsub(/[ \t\r"]/, "") } + /^pm(\.max_children|\.start_servers|\.min_spare_servers|\.max_spare_servers)?=/ && !($0 in image) { other = 1 } + END { exit !other } + ' "${files[@]}" +} + +# The value set for pm.<$1>: FPM_PM_<$2>, which is written after fpm.pool.pm.<$1> and wins, else fpm.pool.pm.<$1>. +fpmPoolSetValue() { + local variable="FPM_PM_$2" + if [[ -n "${!variable+x}" ]]; then + echo "${!variable}" + else + envGetValue "fpm.pool.pm.$1" + fi +} + +# Sizes the process manager to the CPUs and the memory of the container, see docs/environment-variables.md#php-fpm: +# pm.max_children is the smaller of the workers of FPM_AUTO_CHILD_MEMORY that fit into the memory beside OPcache and +# 256 MB for the other processes, and of FPM_AUTO_CHILDREN_PER_CPU per CPU, at least 2; the spare servers follow the +# CPUs. A value set with FPM_PM_* or fpm.pool.pm.* stays, and the others are computed to fit it. +fpmPoolSize() { + local cpus memory quota="" period="" limit="" cpuSource="no limit" memorySource="no limit" + local childMemory perCpu opcacheMemory value + local setChildren setStart setMin setMax children start min max + local positive='^[1-9][0-9]*$' + + # the CPU limit of the container, rounded up, at most the CPUs it may run on + cpus=$(nproc) + if [[ -r /sys/fs/cgroup/cpu.max ]]; then + read -r quota period < /sys/fs/cgroup/cpu.max || true + limit="cgroup v2 limit" + elif [[ -r /sys/fs/cgroup/cpu/cpu.cfs_quota_us && -r /sys/fs/cgroup/cpu/cpu.cfs_period_us ]]; then + quota=$(cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us) + period=$(cat /sys/fs/cgroup/cpu/cpu.cfs_period_us) + limit="cgroup v1 limit" + fi + if [[ "$quota" =~ $positive && "$period" =~ $positive ]] && (( (quota + period - 1) / period < cpus )); then + cpus=$(( (quota + period - 1) / period )) + cpuSource=$limit + fi + + # the memory limit of the container; without one, or a larger one, the memory of the host + memory=$(awk '/^MemTotal:/ { print int($2 / 1024) }' /proc/meminfo) + limit="" + if [[ -r /sys/fs/cgroup/memory.max ]]; then + limit=$(cat /sys/fs/cgroup/memory.max) + memorySource="cgroup v2 limit" + elif [[ -r /sys/fs/cgroup/memory/memory.limit_in_bytes ]]; then + limit=$(cat /sys/fs/cgroup/memory/memory.limit_in_bytes) + memorySource="cgroup v1 limit" + fi + if [[ "$limit" =~ $positive ]] && (( limit / 1048576 < memory )); then + memory=$(( limit / 1048576 )) + else + memorySource="no limit" + fi + + if [[ "${FPM_AUTO_CHILD_MEMORY:-128M}" =~ ^([1-9][0-9]*)([MmGg]?)$ ]]; then + childMemory=${BASH_REMATCH[1]} + if [[ "${BASH_REMATCH[2]}" == [Gg] ]]; then + childMemory=$(( childMemory * 1024 )) + fi + else + echo "PHP-FPM: FPM_AUTO_CHILD_MEMORY=${FPM_AUTO_CHILD_MEMORY} is not a size such as 128M, using 128M" >&2 + childMemory=128 + fi + + perCpu=${FPM_AUTO_CHILDREN_PER_CPU:-4} + if [[ ! "$perCpu" =~ $positive ]]; then + echo "PHP-FPM: FPM_AUTO_CHILDREN_PER_CPU=${perCpu} is not a positive number, using 4" >&2 + perCpu=4 + fi + + # PHP_OPCACHE_MEMORY_CONSUMPTION is written after php.opcache.memory_consumption and wins + opcacheMemory=${PHP_OPCACHE_MEMORY_CONSUMPTION-$(envGetValue php.opcache.memory_consumption)} + if [[ ! "$opcacheMemory" =~ $positive ]]; then + opcacheMemory=256 + fi + + setChildren=$(fpmPoolSetValue max_children MAX_CHILDREN) + setStart=$(fpmPoolSetValue start_servers START_SERVERS) + setMin=$(fpmPoolSetValue min_spare_servers MIN_SPARE_SERVERS) + setMax=$(fpmPoolSetValue max_spare_servers MAX_SPARE_SERVERS) + + children=$(( (memory - opcacheMemory - 256) / childMemory )) + if (( cpus * perCpu < children )); then + children=$(( cpus * perCpu )) + fi + for value in 2 "$setStart" "$setMin" "$setMax"; do + if [[ "$value" =~ $positive ]] && (( value > children )); then + children=$value + fi + done + if [[ "$setChildren" =~ $positive ]]; then + children=$setChildren + fi + + min=$(( cpus / 2 )) + for value in "$children" "$setMax" "$setStart"; do + if [[ "$value" =~ $positive ]] && (( value < min )); then + min=$value + fi + done + if (( min < 1 )); then + min=1 + fi + if [[ "$setMin" =~ $positive ]]; then + min=$setMin + fi + + max=$(( cpus * 2 )) + if (( children < max )); then + max=$children + fi + for value in "$min" "$setStart"; do + if [[ "$value" =~ $positive ]] && (( value > max )); then + max=$value + fi + done + if [[ "$setMax" =~ $positive ]]; then + max=$setMax + fi + + # the default of PHP-FPM + start=$(( min + (max - min) / 2 )) + if [[ "$setStart" =~ $positive ]]; then + start=$setStart + fi + + # only the values that are not set; zz-environment.conf gets the ones that are set below + if [[ -z "$setChildren" ]]; then echo "pm.max_children = ${children}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setStart" ]]; then echo "pm.start_servers = ${start}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setMin" ]]; then echo "pm.min_spare_servers = ${min}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setMax" ]]; then echo "pm.max_spare_servers = ${max}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + + echo "PHP-FPM: ${cpus} CPUs (${cpuSource}), ${memory} MB memory (${memorySource}) ->" \ + "pm.max_children=${children}${setChildren:+ (set)}, pm.start_servers=${start}${setStart:+ (set)}," \ + "pm.min_spare_servers=${min}${setMin:+ (set)}, pm.max_spare_servers=${max}${setMax:+ (set)}" +} + +if [[ "${FPM_AUTO_SIZE:-1}" != "0" ]]; then + if fpmPoolFilesSetTheProcessManager; then + echo "PHP-FPM: a pool file sets pm.* to other values than the image, so the pool is not sized to the container" + else + fpmPoolSize + fi +fi +unset -f fpmPoolFilesSetTheProcessManager fpmPoolSetValue fpmPoolSize + # General fpm pool setting for ENV_VAR in $(envListVars "fpm\.pool\."); do env_key=${ENV_VAR#fpm.pool.} diff --git a/docker/php/8.2/conf/etc/php/php.webdevops.ini b/docker/php/8.2/conf/etc/php/php.webdevops.ini index 829c21e23..8b134c192 100644 --- a/docker/php/8.2/conf/etc/php/php.webdevops.ini +++ b/docker/php/8.2/conf/etc/php/php.webdevops.ini @@ -33,4 +33,4 @@ mysqli.default_host = mysql opcache.memory_consumption = 256 opcache.interned_strings_buffer = 16 -opcache.max_accelerated_files = 7963 +opcache.max_accelerated_files = 32531 diff --git a/docker/php/8.2/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh b/docker/php/8.2/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh index e04580266..de4f1cc83 100644 --- a/docker/php/8.2/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh +++ b/docker/php/8.2/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh @@ -47,3 +47,9 @@ go-replace --mode=line --regex \ go-replace --mode=lineinfile --regex \ -s '^[\s;]*clear_env[\s]*=' -r 'clear_env = no' \ -- /opt/docker/etc/php/fpm/pool.d/application.conf + +# A worker ends after 1000 requests and a new one takes its place, so that memory that leaks in a PHP extension or in +# the application does not add up +go-replace --mode=lineinfile --regex \ + -s '^[\s;]*pm.max_requests[\s]*=' -r 'pm.max_requests = 1000' \ + -- /opt/docker/etc/php/fpm/pool.d/application.conf diff --git a/docker/php/8.2/conf/provision/entrypoint.d/20-php-fpm.sh b/docker/php/8.2/conf/provision/entrypoint.d/20-php-fpm.sh index dfbc12179..9563b51df 100644 --- a/docker/php/8.2/conf/provision/entrypoint.d/20-php-fpm.sh +++ b/docker/php/8.2/conf/provision/entrypoint.d/20-php-fpm.sh @@ -31,6 +31,172 @@ FPM_POOL_ENVIRONMENT_CONF=/opt/docker/etc/php/fpm/pool.d/zz-environment.conf echo '; container env settings, written at every start' > "$FPM_POOL_ENVIRONMENT_CONF" echo '[www]' >> "$FPM_POOL_ENVIRONMENT_CONF" +# Whether a pool file sets the process manager to other values than application.conf of the image, which has those of +# www.conf of the official PHP image: e.g. an application.conf or a www.conf of your own. zz-environment.conf does not +# count, it is being written. +fpmPoolFilesSetTheProcessManager() { + local file + local files=(/dev/null) + for file in /opt/docker/etc/php/fpm/pool.d/*.conf; do + if [[ "$file" != "$FPM_POOL_ENVIRONMENT_CONF" ]]; then + files+=("$file") + fi + done + + awk ' + BEGIN { + split("pm=dynamic pm.max_children=5 pm.start_servers=2 pm.min_spare_servers=1 pm.max_spare_servers=3", values, " ") + for (i in values) image[values[i]] = 1 + } + { sub(/;.*/, ""); gsub(/[ \t\r"]/, "") } + /^pm(\.max_children|\.start_servers|\.min_spare_servers|\.max_spare_servers)?=/ && !($0 in image) { other = 1 } + END { exit !other } + ' "${files[@]}" +} + +# The value set for pm.<$1>: FPM_PM_<$2>, which is written after fpm.pool.pm.<$1> and wins, else fpm.pool.pm.<$1>. +fpmPoolSetValue() { + local variable="FPM_PM_$2" + if [[ -n "${!variable+x}" ]]; then + echo "${!variable}" + else + envGetValue "fpm.pool.pm.$1" + fi +} + +# Sizes the process manager to the CPUs and the memory of the container, see docs/environment-variables.md#php-fpm: +# pm.max_children is the smaller of the workers of FPM_AUTO_CHILD_MEMORY that fit into the memory beside OPcache and +# 256 MB for the other processes, and of FPM_AUTO_CHILDREN_PER_CPU per CPU, at least 2; the spare servers follow the +# CPUs. A value set with FPM_PM_* or fpm.pool.pm.* stays, and the others are computed to fit it. +fpmPoolSize() { + local cpus memory quota="" period="" limit="" cpuSource="no limit" memorySource="no limit" + local childMemory perCpu opcacheMemory value + local setChildren setStart setMin setMax children start min max + local positive='^[1-9][0-9]*$' + + # the CPU limit of the container, rounded up, at most the CPUs it may run on + cpus=$(nproc) + if [[ -r /sys/fs/cgroup/cpu.max ]]; then + read -r quota period < /sys/fs/cgroup/cpu.max || true + limit="cgroup v2 limit" + elif [[ -r /sys/fs/cgroup/cpu/cpu.cfs_quota_us && -r /sys/fs/cgroup/cpu/cpu.cfs_period_us ]]; then + quota=$(cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us) + period=$(cat /sys/fs/cgroup/cpu/cpu.cfs_period_us) + limit="cgroup v1 limit" + fi + if [[ "$quota" =~ $positive && "$period" =~ $positive ]] && (( (quota + period - 1) / period < cpus )); then + cpus=$(( (quota + period - 1) / period )) + cpuSource=$limit + fi + + # the memory limit of the container; without one, or a larger one, the memory of the host + memory=$(awk '/^MemTotal:/ { print int($2 / 1024) }' /proc/meminfo) + limit="" + if [[ -r /sys/fs/cgroup/memory.max ]]; then + limit=$(cat /sys/fs/cgroup/memory.max) + memorySource="cgroup v2 limit" + elif [[ -r /sys/fs/cgroup/memory/memory.limit_in_bytes ]]; then + limit=$(cat /sys/fs/cgroup/memory/memory.limit_in_bytes) + memorySource="cgroup v1 limit" + fi + if [[ "$limit" =~ $positive ]] && (( limit / 1048576 < memory )); then + memory=$(( limit / 1048576 )) + else + memorySource="no limit" + fi + + if [[ "${FPM_AUTO_CHILD_MEMORY:-128M}" =~ ^([1-9][0-9]*)([MmGg]?)$ ]]; then + childMemory=${BASH_REMATCH[1]} + if [[ "${BASH_REMATCH[2]}" == [Gg] ]]; then + childMemory=$(( childMemory * 1024 )) + fi + else + echo "PHP-FPM: FPM_AUTO_CHILD_MEMORY=${FPM_AUTO_CHILD_MEMORY} is not a size such as 128M, using 128M" >&2 + childMemory=128 + fi + + perCpu=${FPM_AUTO_CHILDREN_PER_CPU:-4} + if [[ ! "$perCpu" =~ $positive ]]; then + echo "PHP-FPM: FPM_AUTO_CHILDREN_PER_CPU=${perCpu} is not a positive number, using 4" >&2 + perCpu=4 + fi + + # PHP_OPCACHE_MEMORY_CONSUMPTION is written after php.opcache.memory_consumption and wins + opcacheMemory=${PHP_OPCACHE_MEMORY_CONSUMPTION-$(envGetValue php.opcache.memory_consumption)} + if [[ ! "$opcacheMemory" =~ $positive ]]; then + opcacheMemory=256 + fi + + setChildren=$(fpmPoolSetValue max_children MAX_CHILDREN) + setStart=$(fpmPoolSetValue start_servers START_SERVERS) + setMin=$(fpmPoolSetValue min_spare_servers MIN_SPARE_SERVERS) + setMax=$(fpmPoolSetValue max_spare_servers MAX_SPARE_SERVERS) + + children=$(( (memory - opcacheMemory - 256) / childMemory )) + if (( cpus * perCpu < children )); then + children=$(( cpus * perCpu )) + fi + for value in 2 "$setStart" "$setMin" "$setMax"; do + if [[ "$value" =~ $positive ]] && (( value > children )); then + children=$value + fi + done + if [[ "$setChildren" =~ $positive ]]; then + children=$setChildren + fi + + min=$(( cpus / 2 )) + for value in "$children" "$setMax" "$setStart"; do + if [[ "$value" =~ $positive ]] && (( value < min )); then + min=$value + fi + done + if (( min < 1 )); then + min=1 + fi + if [[ "$setMin" =~ $positive ]]; then + min=$setMin + fi + + max=$(( cpus * 2 )) + if (( children < max )); then + max=$children + fi + for value in "$min" "$setStart"; do + if [[ "$value" =~ $positive ]] && (( value > max )); then + max=$value + fi + done + if [[ "$setMax" =~ $positive ]]; then + max=$setMax + fi + + # the default of PHP-FPM + start=$(( min + (max - min) / 2 )) + if [[ "$setStart" =~ $positive ]]; then + start=$setStart + fi + + # only the values that are not set; zz-environment.conf gets the ones that are set below + if [[ -z "$setChildren" ]]; then echo "pm.max_children = ${children}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setStart" ]]; then echo "pm.start_servers = ${start}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setMin" ]]; then echo "pm.min_spare_servers = ${min}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setMax" ]]; then echo "pm.max_spare_servers = ${max}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + + echo "PHP-FPM: ${cpus} CPUs (${cpuSource}), ${memory} MB memory (${memorySource}) ->" \ + "pm.max_children=${children}${setChildren:+ (set)}, pm.start_servers=${start}${setStart:+ (set)}," \ + "pm.min_spare_servers=${min}${setMin:+ (set)}, pm.max_spare_servers=${max}${setMax:+ (set)}" +} + +if [[ "${FPM_AUTO_SIZE:-1}" != "0" ]]; then + if fpmPoolFilesSetTheProcessManager; then + echo "PHP-FPM: a pool file sets pm.* to other values than the image, so the pool is not sized to the container" + else + fpmPoolSize + fi +fi +unset -f fpmPoolFilesSetTheProcessManager fpmPoolSetValue fpmPoolSize + # General fpm pool setting for ENV_VAR in $(envListVars "fpm\.pool\."); do env_key=${ENV_VAR#fpm.pool.} diff --git a/docker/php/8.3-alpine/conf/etc/php/php.webdevops.ini b/docker/php/8.3-alpine/conf/etc/php/php.webdevops.ini index 829c21e23..8b134c192 100644 --- a/docker/php/8.3-alpine/conf/etc/php/php.webdevops.ini +++ b/docker/php/8.3-alpine/conf/etc/php/php.webdevops.ini @@ -33,4 +33,4 @@ mysqli.default_host = mysql opcache.memory_consumption = 256 opcache.interned_strings_buffer = 16 -opcache.max_accelerated_files = 7963 +opcache.max_accelerated_files = 32531 diff --git a/docker/php/8.3-alpine/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh b/docker/php/8.3-alpine/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh index e04580266..de4f1cc83 100644 --- a/docker/php/8.3-alpine/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh +++ b/docker/php/8.3-alpine/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh @@ -47,3 +47,9 @@ go-replace --mode=line --regex \ go-replace --mode=lineinfile --regex \ -s '^[\s;]*clear_env[\s]*=' -r 'clear_env = no' \ -- /opt/docker/etc/php/fpm/pool.d/application.conf + +# A worker ends after 1000 requests and a new one takes its place, so that memory that leaks in a PHP extension or in +# the application does not add up +go-replace --mode=lineinfile --regex \ + -s '^[\s;]*pm.max_requests[\s]*=' -r 'pm.max_requests = 1000' \ + -- /opt/docker/etc/php/fpm/pool.d/application.conf diff --git a/docker/php/8.3-alpine/conf/provision/entrypoint.d/20-php-fpm.sh b/docker/php/8.3-alpine/conf/provision/entrypoint.d/20-php-fpm.sh index dfbc12179..9563b51df 100644 --- a/docker/php/8.3-alpine/conf/provision/entrypoint.d/20-php-fpm.sh +++ b/docker/php/8.3-alpine/conf/provision/entrypoint.d/20-php-fpm.sh @@ -31,6 +31,172 @@ FPM_POOL_ENVIRONMENT_CONF=/opt/docker/etc/php/fpm/pool.d/zz-environment.conf echo '; container env settings, written at every start' > "$FPM_POOL_ENVIRONMENT_CONF" echo '[www]' >> "$FPM_POOL_ENVIRONMENT_CONF" +# Whether a pool file sets the process manager to other values than application.conf of the image, which has those of +# www.conf of the official PHP image: e.g. an application.conf or a www.conf of your own. zz-environment.conf does not +# count, it is being written. +fpmPoolFilesSetTheProcessManager() { + local file + local files=(/dev/null) + for file in /opt/docker/etc/php/fpm/pool.d/*.conf; do + if [[ "$file" != "$FPM_POOL_ENVIRONMENT_CONF" ]]; then + files+=("$file") + fi + done + + awk ' + BEGIN { + split("pm=dynamic pm.max_children=5 pm.start_servers=2 pm.min_spare_servers=1 pm.max_spare_servers=3", values, " ") + for (i in values) image[values[i]] = 1 + } + { sub(/;.*/, ""); gsub(/[ \t\r"]/, "") } + /^pm(\.max_children|\.start_servers|\.min_spare_servers|\.max_spare_servers)?=/ && !($0 in image) { other = 1 } + END { exit !other } + ' "${files[@]}" +} + +# The value set for pm.<$1>: FPM_PM_<$2>, which is written after fpm.pool.pm.<$1> and wins, else fpm.pool.pm.<$1>. +fpmPoolSetValue() { + local variable="FPM_PM_$2" + if [[ -n "${!variable+x}" ]]; then + echo "${!variable}" + else + envGetValue "fpm.pool.pm.$1" + fi +} + +# Sizes the process manager to the CPUs and the memory of the container, see docs/environment-variables.md#php-fpm: +# pm.max_children is the smaller of the workers of FPM_AUTO_CHILD_MEMORY that fit into the memory beside OPcache and +# 256 MB for the other processes, and of FPM_AUTO_CHILDREN_PER_CPU per CPU, at least 2; the spare servers follow the +# CPUs. A value set with FPM_PM_* or fpm.pool.pm.* stays, and the others are computed to fit it. +fpmPoolSize() { + local cpus memory quota="" period="" limit="" cpuSource="no limit" memorySource="no limit" + local childMemory perCpu opcacheMemory value + local setChildren setStart setMin setMax children start min max + local positive='^[1-9][0-9]*$' + + # the CPU limit of the container, rounded up, at most the CPUs it may run on + cpus=$(nproc) + if [[ -r /sys/fs/cgroup/cpu.max ]]; then + read -r quota period < /sys/fs/cgroup/cpu.max || true + limit="cgroup v2 limit" + elif [[ -r /sys/fs/cgroup/cpu/cpu.cfs_quota_us && -r /sys/fs/cgroup/cpu/cpu.cfs_period_us ]]; then + quota=$(cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us) + period=$(cat /sys/fs/cgroup/cpu/cpu.cfs_period_us) + limit="cgroup v1 limit" + fi + if [[ "$quota" =~ $positive && "$period" =~ $positive ]] && (( (quota + period - 1) / period < cpus )); then + cpus=$(( (quota + period - 1) / period )) + cpuSource=$limit + fi + + # the memory limit of the container; without one, or a larger one, the memory of the host + memory=$(awk '/^MemTotal:/ { print int($2 / 1024) }' /proc/meminfo) + limit="" + if [[ -r /sys/fs/cgroup/memory.max ]]; then + limit=$(cat /sys/fs/cgroup/memory.max) + memorySource="cgroup v2 limit" + elif [[ -r /sys/fs/cgroup/memory/memory.limit_in_bytes ]]; then + limit=$(cat /sys/fs/cgroup/memory/memory.limit_in_bytes) + memorySource="cgroup v1 limit" + fi + if [[ "$limit" =~ $positive ]] && (( limit / 1048576 < memory )); then + memory=$(( limit / 1048576 )) + else + memorySource="no limit" + fi + + if [[ "${FPM_AUTO_CHILD_MEMORY:-128M}" =~ ^([1-9][0-9]*)([MmGg]?)$ ]]; then + childMemory=${BASH_REMATCH[1]} + if [[ "${BASH_REMATCH[2]}" == [Gg] ]]; then + childMemory=$(( childMemory * 1024 )) + fi + else + echo "PHP-FPM: FPM_AUTO_CHILD_MEMORY=${FPM_AUTO_CHILD_MEMORY} is not a size such as 128M, using 128M" >&2 + childMemory=128 + fi + + perCpu=${FPM_AUTO_CHILDREN_PER_CPU:-4} + if [[ ! "$perCpu" =~ $positive ]]; then + echo "PHP-FPM: FPM_AUTO_CHILDREN_PER_CPU=${perCpu} is not a positive number, using 4" >&2 + perCpu=4 + fi + + # PHP_OPCACHE_MEMORY_CONSUMPTION is written after php.opcache.memory_consumption and wins + opcacheMemory=${PHP_OPCACHE_MEMORY_CONSUMPTION-$(envGetValue php.opcache.memory_consumption)} + if [[ ! "$opcacheMemory" =~ $positive ]]; then + opcacheMemory=256 + fi + + setChildren=$(fpmPoolSetValue max_children MAX_CHILDREN) + setStart=$(fpmPoolSetValue start_servers START_SERVERS) + setMin=$(fpmPoolSetValue min_spare_servers MIN_SPARE_SERVERS) + setMax=$(fpmPoolSetValue max_spare_servers MAX_SPARE_SERVERS) + + children=$(( (memory - opcacheMemory - 256) / childMemory )) + if (( cpus * perCpu < children )); then + children=$(( cpus * perCpu )) + fi + for value in 2 "$setStart" "$setMin" "$setMax"; do + if [[ "$value" =~ $positive ]] && (( value > children )); then + children=$value + fi + done + if [[ "$setChildren" =~ $positive ]]; then + children=$setChildren + fi + + min=$(( cpus / 2 )) + for value in "$children" "$setMax" "$setStart"; do + if [[ "$value" =~ $positive ]] && (( value < min )); then + min=$value + fi + done + if (( min < 1 )); then + min=1 + fi + if [[ "$setMin" =~ $positive ]]; then + min=$setMin + fi + + max=$(( cpus * 2 )) + if (( children < max )); then + max=$children + fi + for value in "$min" "$setStart"; do + if [[ "$value" =~ $positive ]] && (( value > max )); then + max=$value + fi + done + if [[ "$setMax" =~ $positive ]]; then + max=$setMax + fi + + # the default of PHP-FPM + start=$(( min + (max - min) / 2 )) + if [[ "$setStart" =~ $positive ]]; then + start=$setStart + fi + + # only the values that are not set; zz-environment.conf gets the ones that are set below + if [[ -z "$setChildren" ]]; then echo "pm.max_children = ${children}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setStart" ]]; then echo "pm.start_servers = ${start}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setMin" ]]; then echo "pm.min_spare_servers = ${min}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setMax" ]]; then echo "pm.max_spare_servers = ${max}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + + echo "PHP-FPM: ${cpus} CPUs (${cpuSource}), ${memory} MB memory (${memorySource}) ->" \ + "pm.max_children=${children}${setChildren:+ (set)}, pm.start_servers=${start}${setStart:+ (set)}," \ + "pm.min_spare_servers=${min}${setMin:+ (set)}, pm.max_spare_servers=${max}${setMax:+ (set)}" +} + +if [[ "${FPM_AUTO_SIZE:-1}" != "0" ]]; then + if fpmPoolFilesSetTheProcessManager; then + echo "PHP-FPM: a pool file sets pm.* to other values than the image, so the pool is not sized to the container" + else + fpmPoolSize + fi +fi +unset -f fpmPoolFilesSetTheProcessManager fpmPoolSetValue fpmPoolSize + # General fpm pool setting for ENV_VAR in $(envListVars "fpm\.pool\."); do env_key=${ENV_VAR#fpm.pool.} diff --git a/docker/php/8.3/conf/etc/php/php.webdevops.ini b/docker/php/8.3/conf/etc/php/php.webdevops.ini index 829c21e23..8b134c192 100644 --- a/docker/php/8.3/conf/etc/php/php.webdevops.ini +++ b/docker/php/8.3/conf/etc/php/php.webdevops.ini @@ -33,4 +33,4 @@ mysqli.default_host = mysql opcache.memory_consumption = 256 opcache.interned_strings_buffer = 16 -opcache.max_accelerated_files = 7963 +opcache.max_accelerated_files = 32531 diff --git a/docker/php/8.3/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh b/docker/php/8.3/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh index e04580266..de4f1cc83 100644 --- a/docker/php/8.3/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh +++ b/docker/php/8.3/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh @@ -47,3 +47,9 @@ go-replace --mode=line --regex \ go-replace --mode=lineinfile --regex \ -s '^[\s;]*clear_env[\s]*=' -r 'clear_env = no' \ -- /opt/docker/etc/php/fpm/pool.d/application.conf + +# A worker ends after 1000 requests and a new one takes its place, so that memory that leaks in a PHP extension or in +# the application does not add up +go-replace --mode=lineinfile --regex \ + -s '^[\s;]*pm.max_requests[\s]*=' -r 'pm.max_requests = 1000' \ + -- /opt/docker/etc/php/fpm/pool.d/application.conf diff --git a/docker/php/8.3/conf/provision/entrypoint.d/20-php-fpm.sh b/docker/php/8.3/conf/provision/entrypoint.d/20-php-fpm.sh index dfbc12179..9563b51df 100644 --- a/docker/php/8.3/conf/provision/entrypoint.d/20-php-fpm.sh +++ b/docker/php/8.3/conf/provision/entrypoint.d/20-php-fpm.sh @@ -31,6 +31,172 @@ FPM_POOL_ENVIRONMENT_CONF=/opt/docker/etc/php/fpm/pool.d/zz-environment.conf echo '; container env settings, written at every start' > "$FPM_POOL_ENVIRONMENT_CONF" echo '[www]' >> "$FPM_POOL_ENVIRONMENT_CONF" +# Whether a pool file sets the process manager to other values than application.conf of the image, which has those of +# www.conf of the official PHP image: e.g. an application.conf or a www.conf of your own. zz-environment.conf does not +# count, it is being written. +fpmPoolFilesSetTheProcessManager() { + local file + local files=(/dev/null) + for file in /opt/docker/etc/php/fpm/pool.d/*.conf; do + if [[ "$file" != "$FPM_POOL_ENVIRONMENT_CONF" ]]; then + files+=("$file") + fi + done + + awk ' + BEGIN { + split("pm=dynamic pm.max_children=5 pm.start_servers=2 pm.min_spare_servers=1 pm.max_spare_servers=3", values, " ") + for (i in values) image[values[i]] = 1 + } + { sub(/;.*/, ""); gsub(/[ \t\r"]/, "") } + /^pm(\.max_children|\.start_servers|\.min_spare_servers|\.max_spare_servers)?=/ && !($0 in image) { other = 1 } + END { exit !other } + ' "${files[@]}" +} + +# The value set for pm.<$1>: FPM_PM_<$2>, which is written after fpm.pool.pm.<$1> and wins, else fpm.pool.pm.<$1>. +fpmPoolSetValue() { + local variable="FPM_PM_$2" + if [[ -n "${!variable+x}" ]]; then + echo "${!variable}" + else + envGetValue "fpm.pool.pm.$1" + fi +} + +# Sizes the process manager to the CPUs and the memory of the container, see docs/environment-variables.md#php-fpm: +# pm.max_children is the smaller of the workers of FPM_AUTO_CHILD_MEMORY that fit into the memory beside OPcache and +# 256 MB for the other processes, and of FPM_AUTO_CHILDREN_PER_CPU per CPU, at least 2; the spare servers follow the +# CPUs. A value set with FPM_PM_* or fpm.pool.pm.* stays, and the others are computed to fit it. +fpmPoolSize() { + local cpus memory quota="" period="" limit="" cpuSource="no limit" memorySource="no limit" + local childMemory perCpu opcacheMemory value + local setChildren setStart setMin setMax children start min max + local positive='^[1-9][0-9]*$' + + # the CPU limit of the container, rounded up, at most the CPUs it may run on + cpus=$(nproc) + if [[ -r /sys/fs/cgroup/cpu.max ]]; then + read -r quota period < /sys/fs/cgroup/cpu.max || true + limit="cgroup v2 limit" + elif [[ -r /sys/fs/cgroup/cpu/cpu.cfs_quota_us && -r /sys/fs/cgroup/cpu/cpu.cfs_period_us ]]; then + quota=$(cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us) + period=$(cat /sys/fs/cgroup/cpu/cpu.cfs_period_us) + limit="cgroup v1 limit" + fi + if [[ "$quota" =~ $positive && "$period" =~ $positive ]] && (( (quota + period - 1) / period < cpus )); then + cpus=$(( (quota + period - 1) / period )) + cpuSource=$limit + fi + + # the memory limit of the container; without one, or a larger one, the memory of the host + memory=$(awk '/^MemTotal:/ { print int($2 / 1024) }' /proc/meminfo) + limit="" + if [[ -r /sys/fs/cgroup/memory.max ]]; then + limit=$(cat /sys/fs/cgroup/memory.max) + memorySource="cgroup v2 limit" + elif [[ -r /sys/fs/cgroup/memory/memory.limit_in_bytes ]]; then + limit=$(cat /sys/fs/cgroup/memory/memory.limit_in_bytes) + memorySource="cgroup v1 limit" + fi + if [[ "$limit" =~ $positive ]] && (( limit / 1048576 < memory )); then + memory=$(( limit / 1048576 )) + else + memorySource="no limit" + fi + + if [[ "${FPM_AUTO_CHILD_MEMORY:-128M}" =~ ^([1-9][0-9]*)([MmGg]?)$ ]]; then + childMemory=${BASH_REMATCH[1]} + if [[ "${BASH_REMATCH[2]}" == [Gg] ]]; then + childMemory=$(( childMemory * 1024 )) + fi + else + echo "PHP-FPM: FPM_AUTO_CHILD_MEMORY=${FPM_AUTO_CHILD_MEMORY} is not a size such as 128M, using 128M" >&2 + childMemory=128 + fi + + perCpu=${FPM_AUTO_CHILDREN_PER_CPU:-4} + if [[ ! "$perCpu" =~ $positive ]]; then + echo "PHP-FPM: FPM_AUTO_CHILDREN_PER_CPU=${perCpu} is not a positive number, using 4" >&2 + perCpu=4 + fi + + # PHP_OPCACHE_MEMORY_CONSUMPTION is written after php.opcache.memory_consumption and wins + opcacheMemory=${PHP_OPCACHE_MEMORY_CONSUMPTION-$(envGetValue php.opcache.memory_consumption)} + if [[ ! "$opcacheMemory" =~ $positive ]]; then + opcacheMemory=256 + fi + + setChildren=$(fpmPoolSetValue max_children MAX_CHILDREN) + setStart=$(fpmPoolSetValue start_servers START_SERVERS) + setMin=$(fpmPoolSetValue min_spare_servers MIN_SPARE_SERVERS) + setMax=$(fpmPoolSetValue max_spare_servers MAX_SPARE_SERVERS) + + children=$(( (memory - opcacheMemory - 256) / childMemory )) + if (( cpus * perCpu < children )); then + children=$(( cpus * perCpu )) + fi + for value in 2 "$setStart" "$setMin" "$setMax"; do + if [[ "$value" =~ $positive ]] && (( value > children )); then + children=$value + fi + done + if [[ "$setChildren" =~ $positive ]]; then + children=$setChildren + fi + + min=$(( cpus / 2 )) + for value in "$children" "$setMax" "$setStart"; do + if [[ "$value" =~ $positive ]] && (( value < min )); then + min=$value + fi + done + if (( min < 1 )); then + min=1 + fi + if [[ "$setMin" =~ $positive ]]; then + min=$setMin + fi + + max=$(( cpus * 2 )) + if (( children < max )); then + max=$children + fi + for value in "$min" "$setStart"; do + if [[ "$value" =~ $positive ]] && (( value > max )); then + max=$value + fi + done + if [[ "$setMax" =~ $positive ]]; then + max=$setMax + fi + + # the default of PHP-FPM + start=$(( min + (max - min) / 2 )) + if [[ "$setStart" =~ $positive ]]; then + start=$setStart + fi + + # only the values that are not set; zz-environment.conf gets the ones that are set below + if [[ -z "$setChildren" ]]; then echo "pm.max_children = ${children}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setStart" ]]; then echo "pm.start_servers = ${start}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setMin" ]]; then echo "pm.min_spare_servers = ${min}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setMax" ]]; then echo "pm.max_spare_servers = ${max}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + + echo "PHP-FPM: ${cpus} CPUs (${cpuSource}), ${memory} MB memory (${memorySource}) ->" \ + "pm.max_children=${children}${setChildren:+ (set)}, pm.start_servers=${start}${setStart:+ (set)}," \ + "pm.min_spare_servers=${min}${setMin:+ (set)}, pm.max_spare_servers=${max}${setMax:+ (set)}" +} + +if [[ "${FPM_AUTO_SIZE:-1}" != "0" ]]; then + if fpmPoolFilesSetTheProcessManager; then + echo "PHP-FPM: a pool file sets pm.* to other values than the image, so the pool is not sized to the container" + else + fpmPoolSize + fi +fi +unset -f fpmPoolFilesSetTheProcessManager fpmPoolSetValue fpmPoolSize + # General fpm pool setting for ENV_VAR in $(envListVars "fpm\.pool\."); do env_key=${ENV_VAR#fpm.pool.} diff --git a/docker/php/8.4-alpine/conf/etc/php/php.webdevops.ini b/docker/php/8.4-alpine/conf/etc/php/php.webdevops.ini index 829c21e23..8b134c192 100644 --- a/docker/php/8.4-alpine/conf/etc/php/php.webdevops.ini +++ b/docker/php/8.4-alpine/conf/etc/php/php.webdevops.ini @@ -33,4 +33,4 @@ mysqli.default_host = mysql opcache.memory_consumption = 256 opcache.interned_strings_buffer = 16 -opcache.max_accelerated_files = 7963 +opcache.max_accelerated_files = 32531 diff --git a/docker/php/8.4-alpine/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh b/docker/php/8.4-alpine/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh index e04580266..de4f1cc83 100644 --- a/docker/php/8.4-alpine/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh +++ b/docker/php/8.4-alpine/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh @@ -47,3 +47,9 @@ go-replace --mode=line --regex \ go-replace --mode=lineinfile --regex \ -s '^[\s;]*clear_env[\s]*=' -r 'clear_env = no' \ -- /opt/docker/etc/php/fpm/pool.d/application.conf + +# A worker ends after 1000 requests and a new one takes its place, so that memory that leaks in a PHP extension or in +# the application does not add up +go-replace --mode=lineinfile --regex \ + -s '^[\s;]*pm.max_requests[\s]*=' -r 'pm.max_requests = 1000' \ + -- /opt/docker/etc/php/fpm/pool.d/application.conf diff --git a/docker/php/8.4-alpine/conf/provision/entrypoint.d/20-php-fpm.sh b/docker/php/8.4-alpine/conf/provision/entrypoint.d/20-php-fpm.sh index dfbc12179..9563b51df 100644 --- a/docker/php/8.4-alpine/conf/provision/entrypoint.d/20-php-fpm.sh +++ b/docker/php/8.4-alpine/conf/provision/entrypoint.d/20-php-fpm.sh @@ -31,6 +31,172 @@ FPM_POOL_ENVIRONMENT_CONF=/opt/docker/etc/php/fpm/pool.d/zz-environment.conf echo '; container env settings, written at every start' > "$FPM_POOL_ENVIRONMENT_CONF" echo '[www]' >> "$FPM_POOL_ENVIRONMENT_CONF" +# Whether a pool file sets the process manager to other values than application.conf of the image, which has those of +# www.conf of the official PHP image: e.g. an application.conf or a www.conf of your own. zz-environment.conf does not +# count, it is being written. +fpmPoolFilesSetTheProcessManager() { + local file + local files=(/dev/null) + for file in /opt/docker/etc/php/fpm/pool.d/*.conf; do + if [[ "$file" != "$FPM_POOL_ENVIRONMENT_CONF" ]]; then + files+=("$file") + fi + done + + awk ' + BEGIN { + split("pm=dynamic pm.max_children=5 pm.start_servers=2 pm.min_spare_servers=1 pm.max_spare_servers=3", values, " ") + for (i in values) image[values[i]] = 1 + } + { sub(/;.*/, ""); gsub(/[ \t\r"]/, "") } + /^pm(\.max_children|\.start_servers|\.min_spare_servers|\.max_spare_servers)?=/ && !($0 in image) { other = 1 } + END { exit !other } + ' "${files[@]}" +} + +# The value set for pm.<$1>: FPM_PM_<$2>, which is written after fpm.pool.pm.<$1> and wins, else fpm.pool.pm.<$1>. +fpmPoolSetValue() { + local variable="FPM_PM_$2" + if [[ -n "${!variable+x}" ]]; then + echo "${!variable}" + else + envGetValue "fpm.pool.pm.$1" + fi +} + +# Sizes the process manager to the CPUs and the memory of the container, see docs/environment-variables.md#php-fpm: +# pm.max_children is the smaller of the workers of FPM_AUTO_CHILD_MEMORY that fit into the memory beside OPcache and +# 256 MB for the other processes, and of FPM_AUTO_CHILDREN_PER_CPU per CPU, at least 2; the spare servers follow the +# CPUs. A value set with FPM_PM_* or fpm.pool.pm.* stays, and the others are computed to fit it. +fpmPoolSize() { + local cpus memory quota="" period="" limit="" cpuSource="no limit" memorySource="no limit" + local childMemory perCpu opcacheMemory value + local setChildren setStart setMin setMax children start min max + local positive='^[1-9][0-9]*$' + + # the CPU limit of the container, rounded up, at most the CPUs it may run on + cpus=$(nproc) + if [[ -r /sys/fs/cgroup/cpu.max ]]; then + read -r quota period < /sys/fs/cgroup/cpu.max || true + limit="cgroup v2 limit" + elif [[ -r /sys/fs/cgroup/cpu/cpu.cfs_quota_us && -r /sys/fs/cgroup/cpu/cpu.cfs_period_us ]]; then + quota=$(cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us) + period=$(cat /sys/fs/cgroup/cpu/cpu.cfs_period_us) + limit="cgroup v1 limit" + fi + if [[ "$quota" =~ $positive && "$period" =~ $positive ]] && (( (quota + period - 1) / period < cpus )); then + cpus=$(( (quota + period - 1) / period )) + cpuSource=$limit + fi + + # the memory limit of the container; without one, or a larger one, the memory of the host + memory=$(awk '/^MemTotal:/ { print int($2 / 1024) }' /proc/meminfo) + limit="" + if [[ -r /sys/fs/cgroup/memory.max ]]; then + limit=$(cat /sys/fs/cgroup/memory.max) + memorySource="cgroup v2 limit" + elif [[ -r /sys/fs/cgroup/memory/memory.limit_in_bytes ]]; then + limit=$(cat /sys/fs/cgroup/memory/memory.limit_in_bytes) + memorySource="cgroup v1 limit" + fi + if [[ "$limit" =~ $positive ]] && (( limit / 1048576 < memory )); then + memory=$(( limit / 1048576 )) + else + memorySource="no limit" + fi + + if [[ "${FPM_AUTO_CHILD_MEMORY:-128M}" =~ ^([1-9][0-9]*)([MmGg]?)$ ]]; then + childMemory=${BASH_REMATCH[1]} + if [[ "${BASH_REMATCH[2]}" == [Gg] ]]; then + childMemory=$(( childMemory * 1024 )) + fi + else + echo "PHP-FPM: FPM_AUTO_CHILD_MEMORY=${FPM_AUTO_CHILD_MEMORY} is not a size such as 128M, using 128M" >&2 + childMemory=128 + fi + + perCpu=${FPM_AUTO_CHILDREN_PER_CPU:-4} + if [[ ! "$perCpu" =~ $positive ]]; then + echo "PHP-FPM: FPM_AUTO_CHILDREN_PER_CPU=${perCpu} is not a positive number, using 4" >&2 + perCpu=4 + fi + + # PHP_OPCACHE_MEMORY_CONSUMPTION is written after php.opcache.memory_consumption and wins + opcacheMemory=${PHP_OPCACHE_MEMORY_CONSUMPTION-$(envGetValue php.opcache.memory_consumption)} + if [[ ! "$opcacheMemory" =~ $positive ]]; then + opcacheMemory=256 + fi + + setChildren=$(fpmPoolSetValue max_children MAX_CHILDREN) + setStart=$(fpmPoolSetValue start_servers START_SERVERS) + setMin=$(fpmPoolSetValue min_spare_servers MIN_SPARE_SERVERS) + setMax=$(fpmPoolSetValue max_spare_servers MAX_SPARE_SERVERS) + + children=$(( (memory - opcacheMemory - 256) / childMemory )) + if (( cpus * perCpu < children )); then + children=$(( cpus * perCpu )) + fi + for value in 2 "$setStart" "$setMin" "$setMax"; do + if [[ "$value" =~ $positive ]] && (( value > children )); then + children=$value + fi + done + if [[ "$setChildren" =~ $positive ]]; then + children=$setChildren + fi + + min=$(( cpus / 2 )) + for value in "$children" "$setMax" "$setStart"; do + if [[ "$value" =~ $positive ]] && (( value < min )); then + min=$value + fi + done + if (( min < 1 )); then + min=1 + fi + if [[ "$setMin" =~ $positive ]]; then + min=$setMin + fi + + max=$(( cpus * 2 )) + if (( children < max )); then + max=$children + fi + for value in "$min" "$setStart"; do + if [[ "$value" =~ $positive ]] && (( value > max )); then + max=$value + fi + done + if [[ "$setMax" =~ $positive ]]; then + max=$setMax + fi + + # the default of PHP-FPM + start=$(( min + (max - min) / 2 )) + if [[ "$setStart" =~ $positive ]]; then + start=$setStart + fi + + # only the values that are not set; zz-environment.conf gets the ones that are set below + if [[ -z "$setChildren" ]]; then echo "pm.max_children = ${children}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setStart" ]]; then echo "pm.start_servers = ${start}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setMin" ]]; then echo "pm.min_spare_servers = ${min}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setMax" ]]; then echo "pm.max_spare_servers = ${max}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + + echo "PHP-FPM: ${cpus} CPUs (${cpuSource}), ${memory} MB memory (${memorySource}) ->" \ + "pm.max_children=${children}${setChildren:+ (set)}, pm.start_servers=${start}${setStart:+ (set)}," \ + "pm.min_spare_servers=${min}${setMin:+ (set)}, pm.max_spare_servers=${max}${setMax:+ (set)}" +} + +if [[ "${FPM_AUTO_SIZE:-1}" != "0" ]]; then + if fpmPoolFilesSetTheProcessManager; then + echo "PHP-FPM: a pool file sets pm.* to other values than the image, so the pool is not sized to the container" + else + fpmPoolSize + fi +fi +unset -f fpmPoolFilesSetTheProcessManager fpmPoolSetValue fpmPoolSize + # General fpm pool setting for ENV_VAR in $(envListVars "fpm\.pool\."); do env_key=${ENV_VAR#fpm.pool.} diff --git a/docker/php/8.4/conf/etc/php/php.webdevops.ini b/docker/php/8.4/conf/etc/php/php.webdevops.ini index 829c21e23..8b134c192 100644 --- a/docker/php/8.4/conf/etc/php/php.webdevops.ini +++ b/docker/php/8.4/conf/etc/php/php.webdevops.ini @@ -33,4 +33,4 @@ mysqli.default_host = mysql opcache.memory_consumption = 256 opcache.interned_strings_buffer = 16 -opcache.max_accelerated_files = 7963 +opcache.max_accelerated_files = 32531 diff --git a/docker/php/8.4/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh b/docker/php/8.4/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh index e04580266..de4f1cc83 100644 --- a/docker/php/8.4/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh +++ b/docker/php/8.4/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh @@ -47,3 +47,9 @@ go-replace --mode=line --regex \ go-replace --mode=lineinfile --regex \ -s '^[\s;]*clear_env[\s]*=' -r 'clear_env = no' \ -- /opt/docker/etc/php/fpm/pool.d/application.conf + +# A worker ends after 1000 requests and a new one takes its place, so that memory that leaks in a PHP extension or in +# the application does not add up +go-replace --mode=lineinfile --regex \ + -s '^[\s;]*pm.max_requests[\s]*=' -r 'pm.max_requests = 1000' \ + -- /opt/docker/etc/php/fpm/pool.d/application.conf diff --git a/docker/php/8.4/conf/provision/entrypoint.d/20-php-fpm.sh b/docker/php/8.4/conf/provision/entrypoint.d/20-php-fpm.sh index dfbc12179..9563b51df 100644 --- a/docker/php/8.4/conf/provision/entrypoint.d/20-php-fpm.sh +++ b/docker/php/8.4/conf/provision/entrypoint.d/20-php-fpm.sh @@ -31,6 +31,172 @@ FPM_POOL_ENVIRONMENT_CONF=/opt/docker/etc/php/fpm/pool.d/zz-environment.conf echo '; container env settings, written at every start' > "$FPM_POOL_ENVIRONMENT_CONF" echo '[www]' >> "$FPM_POOL_ENVIRONMENT_CONF" +# Whether a pool file sets the process manager to other values than application.conf of the image, which has those of +# www.conf of the official PHP image: e.g. an application.conf or a www.conf of your own. zz-environment.conf does not +# count, it is being written. +fpmPoolFilesSetTheProcessManager() { + local file + local files=(/dev/null) + for file in /opt/docker/etc/php/fpm/pool.d/*.conf; do + if [[ "$file" != "$FPM_POOL_ENVIRONMENT_CONF" ]]; then + files+=("$file") + fi + done + + awk ' + BEGIN { + split("pm=dynamic pm.max_children=5 pm.start_servers=2 pm.min_spare_servers=1 pm.max_spare_servers=3", values, " ") + for (i in values) image[values[i]] = 1 + } + { sub(/;.*/, ""); gsub(/[ \t\r"]/, "") } + /^pm(\.max_children|\.start_servers|\.min_spare_servers|\.max_spare_servers)?=/ && !($0 in image) { other = 1 } + END { exit !other } + ' "${files[@]}" +} + +# The value set for pm.<$1>: FPM_PM_<$2>, which is written after fpm.pool.pm.<$1> and wins, else fpm.pool.pm.<$1>. +fpmPoolSetValue() { + local variable="FPM_PM_$2" + if [[ -n "${!variable+x}" ]]; then + echo "${!variable}" + else + envGetValue "fpm.pool.pm.$1" + fi +} + +# Sizes the process manager to the CPUs and the memory of the container, see docs/environment-variables.md#php-fpm: +# pm.max_children is the smaller of the workers of FPM_AUTO_CHILD_MEMORY that fit into the memory beside OPcache and +# 256 MB for the other processes, and of FPM_AUTO_CHILDREN_PER_CPU per CPU, at least 2; the spare servers follow the +# CPUs. A value set with FPM_PM_* or fpm.pool.pm.* stays, and the others are computed to fit it. +fpmPoolSize() { + local cpus memory quota="" period="" limit="" cpuSource="no limit" memorySource="no limit" + local childMemory perCpu opcacheMemory value + local setChildren setStart setMin setMax children start min max + local positive='^[1-9][0-9]*$' + + # the CPU limit of the container, rounded up, at most the CPUs it may run on + cpus=$(nproc) + if [[ -r /sys/fs/cgroup/cpu.max ]]; then + read -r quota period < /sys/fs/cgroup/cpu.max || true + limit="cgroup v2 limit" + elif [[ -r /sys/fs/cgroup/cpu/cpu.cfs_quota_us && -r /sys/fs/cgroup/cpu/cpu.cfs_period_us ]]; then + quota=$(cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us) + period=$(cat /sys/fs/cgroup/cpu/cpu.cfs_period_us) + limit="cgroup v1 limit" + fi + if [[ "$quota" =~ $positive && "$period" =~ $positive ]] && (( (quota + period - 1) / period < cpus )); then + cpus=$(( (quota + period - 1) / period )) + cpuSource=$limit + fi + + # the memory limit of the container; without one, or a larger one, the memory of the host + memory=$(awk '/^MemTotal:/ { print int($2 / 1024) }' /proc/meminfo) + limit="" + if [[ -r /sys/fs/cgroup/memory.max ]]; then + limit=$(cat /sys/fs/cgroup/memory.max) + memorySource="cgroup v2 limit" + elif [[ -r /sys/fs/cgroup/memory/memory.limit_in_bytes ]]; then + limit=$(cat /sys/fs/cgroup/memory/memory.limit_in_bytes) + memorySource="cgroup v1 limit" + fi + if [[ "$limit" =~ $positive ]] && (( limit / 1048576 < memory )); then + memory=$(( limit / 1048576 )) + else + memorySource="no limit" + fi + + if [[ "${FPM_AUTO_CHILD_MEMORY:-128M}" =~ ^([1-9][0-9]*)([MmGg]?)$ ]]; then + childMemory=${BASH_REMATCH[1]} + if [[ "${BASH_REMATCH[2]}" == [Gg] ]]; then + childMemory=$(( childMemory * 1024 )) + fi + else + echo "PHP-FPM: FPM_AUTO_CHILD_MEMORY=${FPM_AUTO_CHILD_MEMORY} is not a size such as 128M, using 128M" >&2 + childMemory=128 + fi + + perCpu=${FPM_AUTO_CHILDREN_PER_CPU:-4} + if [[ ! "$perCpu" =~ $positive ]]; then + echo "PHP-FPM: FPM_AUTO_CHILDREN_PER_CPU=${perCpu} is not a positive number, using 4" >&2 + perCpu=4 + fi + + # PHP_OPCACHE_MEMORY_CONSUMPTION is written after php.opcache.memory_consumption and wins + opcacheMemory=${PHP_OPCACHE_MEMORY_CONSUMPTION-$(envGetValue php.opcache.memory_consumption)} + if [[ ! "$opcacheMemory" =~ $positive ]]; then + opcacheMemory=256 + fi + + setChildren=$(fpmPoolSetValue max_children MAX_CHILDREN) + setStart=$(fpmPoolSetValue start_servers START_SERVERS) + setMin=$(fpmPoolSetValue min_spare_servers MIN_SPARE_SERVERS) + setMax=$(fpmPoolSetValue max_spare_servers MAX_SPARE_SERVERS) + + children=$(( (memory - opcacheMemory - 256) / childMemory )) + if (( cpus * perCpu < children )); then + children=$(( cpus * perCpu )) + fi + for value in 2 "$setStart" "$setMin" "$setMax"; do + if [[ "$value" =~ $positive ]] && (( value > children )); then + children=$value + fi + done + if [[ "$setChildren" =~ $positive ]]; then + children=$setChildren + fi + + min=$(( cpus / 2 )) + for value in "$children" "$setMax" "$setStart"; do + if [[ "$value" =~ $positive ]] && (( value < min )); then + min=$value + fi + done + if (( min < 1 )); then + min=1 + fi + if [[ "$setMin" =~ $positive ]]; then + min=$setMin + fi + + max=$(( cpus * 2 )) + if (( children < max )); then + max=$children + fi + for value in "$min" "$setStart"; do + if [[ "$value" =~ $positive ]] && (( value > max )); then + max=$value + fi + done + if [[ "$setMax" =~ $positive ]]; then + max=$setMax + fi + + # the default of PHP-FPM + start=$(( min + (max - min) / 2 )) + if [[ "$setStart" =~ $positive ]]; then + start=$setStart + fi + + # only the values that are not set; zz-environment.conf gets the ones that are set below + if [[ -z "$setChildren" ]]; then echo "pm.max_children = ${children}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setStart" ]]; then echo "pm.start_servers = ${start}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setMin" ]]; then echo "pm.min_spare_servers = ${min}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setMax" ]]; then echo "pm.max_spare_servers = ${max}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + + echo "PHP-FPM: ${cpus} CPUs (${cpuSource}), ${memory} MB memory (${memorySource}) ->" \ + "pm.max_children=${children}${setChildren:+ (set)}, pm.start_servers=${start}${setStart:+ (set)}," \ + "pm.min_spare_servers=${min}${setMin:+ (set)}, pm.max_spare_servers=${max}${setMax:+ (set)}" +} + +if [[ "${FPM_AUTO_SIZE:-1}" != "0" ]]; then + if fpmPoolFilesSetTheProcessManager; then + echo "PHP-FPM: a pool file sets pm.* to other values than the image, so the pool is not sized to the container" + else + fpmPoolSize + fi +fi +unset -f fpmPoolFilesSetTheProcessManager fpmPoolSetValue fpmPoolSize + # General fpm pool setting for ENV_VAR in $(envListVars "fpm\.pool\."); do env_key=${ENV_VAR#fpm.pool.} diff --git a/docker/php/8.5-alpine/conf/etc/php/php.webdevops.ini b/docker/php/8.5-alpine/conf/etc/php/php.webdevops.ini index 829c21e23..8b134c192 100644 --- a/docker/php/8.5-alpine/conf/etc/php/php.webdevops.ini +++ b/docker/php/8.5-alpine/conf/etc/php/php.webdevops.ini @@ -33,4 +33,4 @@ mysqli.default_host = mysql opcache.memory_consumption = 256 opcache.interned_strings_buffer = 16 -opcache.max_accelerated_files = 7963 +opcache.max_accelerated_files = 32531 diff --git a/docker/php/8.5-alpine/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh b/docker/php/8.5-alpine/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh index e04580266..de4f1cc83 100644 --- a/docker/php/8.5-alpine/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh +++ b/docker/php/8.5-alpine/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh @@ -47,3 +47,9 @@ go-replace --mode=line --regex \ go-replace --mode=lineinfile --regex \ -s '^[\s;]*clear_env[\s]*=' -r 'clear_env = no' \ -- /opt/docker/etc/php/fpm/pool.d/application.conf + +# A worker ends after 1000 requests and a new one takes its place, so that memory that leaks in a PHP extension or in +# the application does not add up +go-replace --mode=lineinfile --regex \ + -s '^[\s;]*pm.max_requests[\s]*=' -r 'pm.max_requests = 1000' \ + -- /opt/docker/etc/php/fpm/pool.d/application.conf diff --git a/docker/php/8.5-alpine/conf/provision/entrypoint.d/20-php-fpm.sh b/docker/php/8.5-alpine/conf/provision/entrypoint.d/20-php-fpm.sh index dfbc12179..9563b51df 100644 --- a/docker/php/8.5-alpine/conf/provision/entrypoint.d/20-php-fpm.sh +++ b/docker/php/8.5-alpine/conf/provision/entrypoint.d/20-php-fpm.sh @@ -31,6 +31,172 @@ FPM_POOL_ENVIRONMENT_CONF=/opt/docker/etc/php/fpm/pool.d/zz-environment.conf echo '; container env settings, written at every start' > "$FPM_POOL_ENVIRONMENT_CONF" echo '[www]' >> "$FPM_POOL_ENVIRONMENT_CONF" +# Whether a pool file sets the process manager to other values than application.conf of the image, which has those of +# www.conf of the official PHP image: e.g. an application.conf or a www.conf of your own. zz-environment.conf does not +# count, it is being written. +fpmPoolFilesSetTheProcessManager() { + local file + local files=(/dev/null) + for file in /opt/docker/etc/php/fpm/pool.d/*.conf; do + if [[ "$file" != "$FPM_POOL_ENVIRONMENT_CONF" ]]; then + files+=("$file") + fi + done + + awk ' + BEGIN { + split("pm=dynamic pm.max_children=5 pm.start_servers=2 pm.min_spare_servers=1 pm.max_spare_servers=3", values, " ") + for (i in values) image[values[i]] = 1 + } + { sub(/;.*/, ""); gsub(/[ \t\r"]/, "") } + /^pm(\.max_children|\.start_servers|\.min_spare_servers|\.max_spare_servers)?=/ && !($0 in image) { other = 1 } + END { exit !other } + ' "${files[@]}" +} + +# The value set for pm.<$1>: FPM_PM_<$2>, which is written after fpm.pool.pm.<$1> and wins, else fpm.pool.pm.<$1>. +fpmPoolSetValue() { + local variable="FPM_PM_$2" + if [[ -n "${!variable+x}" ]]; then + echo "${!variable}" + else + envGetValue "fpm.pool.pm.$1" + fi +} + +# Sizes the process manager to the CPUs and the memory of the container, see docs/environment-variables.md#php-fpm: +# pm.max_children is the smaller of the workers of FPM_AUTO_CHILD_MEMORY that fit into the memory beside OPcache and +# 256 MB for the other processes, and of FPM_AUTO_CHILDREN_PER_CPU per CPU, at least 2; the spare servers follow the +# CPUs. A value set with FPM_PM_* or fpm.pool.pm.* stays, and the others are computed to fit it. +fpmPoolSize() { + local cpus memory quota="" period="" limit="" cpuSource="no limit" memorySource="no limit" + local childMemory perCpu opcacheMemory value + local setChildren setStart setMin setMax children start min max + local positive='^[1-9][0-9]*$' + + # the CPU limit of the container, rounded up, at most the CPUs it may run on + cpus=$(nproc) + if [[ -r /sys/fs/cgroup/cpu.max ]]; then + read -r quota period < /sys/fs/cgroup/cpu.max || true + limit="cgroup v2 limit" + elif [[ -r /sys/fs/cgroup/cpu/cpu.cfs_quota_us && -r /sys/fs/cgroup/cpu/cpu.cfs_period_us ]]; then + quota=$(cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us) + period=$(cat /sys/fs/cgroup/cpu/cpu.cfs_period_us) + limit="cgroup v1 limit" + fi + if [[ "$quota" =~ $positive && "$period" =~ $positive ]] && (( (quota + period - 1) / period < cpus )); then + cpus=$(( (quota + period - 1) / period )) + cpuSource=$limit + fi + + # the memory limit of the container; without one, or a larger one, the memory of the host + memory=$(awk '/^MemTotal:/ { print int($2 / 1024) }' /proc/meminfo) + limit="" + if [[ -r /sys/fs/cgroup/memory.max ]]; then + limit=$(cat /sys/fs/cgroup/memory.max) + memorySource="cgroup v2 limit" + elif [[ -r /sys/fs/cgroup/memory/memory.limit_in_bytes ]]; then + limit=$(cat /sys/fs/cgroup/memory/memory.limit_in_bytes) + memorySource="cgroup v1 limit" + fi + if [[ "$limit" =~ $positive ]] && (( limit / 1048576 < memory )); then + memory=$(( limit / 1048576 )) + else + memorySource="no limit" + fi + + if [[ "${FPM_AUTO_CHILD_MEMORY:-128M}" =~ ^([1-9][0-9]*)([MmGg]?)$ ]]; then + childMemory=${BASH_REMATCH[1]} + if [[ "${BASH_REMATCH[2]}" == [Gg] ]]; then + childMemory=$(( childMemory * 1024 )) + fi + else + echo "PHP-FPM: FPM_AUTO_CHILD_MEMORY=${FPM_AUTO_CHILD_MEMORY} is not a size such as 128M, using 128M" >&2 + childMemory=128 + fi + + perCpu=${FPM_AUTO_CHILDREN_PER_CPU:-4} + if [[ ! "$perCpu" =~ $positive ]]; then + echo "PHP-FPM: FPM_AUTO_CHILDREN_PER_CPU=${perCpu} is not a positive number, using 4" >&2 + perCpu=4 + fi + + # PHP_OPCACHE_MEMORY_CONSUMPTION is written after php.opcache.memory_consumption and wins + opcacheMemory=${PHP_OPCACHE_MEMORY_CONSUMPTION-$(envGetValue php.opcache.memory_consumption)} + if [[ ! "$opcacheMemory" =~ $positive ]]; then + opcacheMemory=256 + fi + + setChildren=$(fpmPoolSetValue max_children MAX_CHILDREN) + setStart=$(fpmPoolSetValue start_servers START_SERVERS) + setMin=$(fpmPoolSetValue min_spare_servers MIN_SPARE_SERVERS) + setMax=$(fpmPoolSetValue max_spare_servers MAX_SPARE_SERVERS) + + children=$(( (memory - opcacheMemory - 256) / childMemory )) + if (( cpus * perCpu < children )); then + children=$(( cpus * perCpu )) + fi + for value in 2 "$setStart" "$setMin" "$setMax"; do + if [[ "$value" =~ $positive ]] && (( value > children )); then + children=$value + fi + done + if [[ "$setChildren" =~ $positive ]]; then + children=$setChildren + fi + + min=$(( cpus / 2 )) + for value in "$children" "$setMax" "$setStart"; do + if [[ "$value" =~ $positive ]] && (( value < min )); then + min=$value + fi + done + if (( min < 1 )); then + min=1 + fi + if [[ "$setMin" =~ $positive ]]; then + min=$setMin + fi + + max=$(( cpus * 2 )) + if (( children < max )); then + max=$children + fi + for value in "$min" "$setStart"; do + if [[ "$value" =~ $positive ]] && (( value > max )); then + max=$value + fi + done + if [[ "$setMax" =~ $positive ]]; then + max=$setMax + fi + + # the default of PHP-FPM + start=$(( min + (max - min) / 2 )) + if [[ "$setStart" =~ $positive ]]; then + start=$setStart + fi + + # only the values that are not set; zz-environment.conf gets the ones that are set below + if [[ -z "$setChildren" ]]; then echo "pm.max_children = ${children}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setStart" ]]; then echo "pm.start_servers = ${start}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setMin" ]]; then echo "pm.min_spare_servers = ${min}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setMax" ]]; then echo "pm.max_spare_servers = ${max}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + + echo "PHP-FPM: ${cpus} CPUs (${cpuSource}), ${memory} MB memory (${memorySource}) ->" \ + "pm.max_children=${children}${setChildren:+ (set)}, pm.start_servers=${start}${setStart:+ (set)}," \ + "pm.min_spare_servers=${min}${setMin:+ (set)}, pm.max_spare_servers=${max}${setMax:+ (set)}" +} + +if [[ "${FPM_AUTO_SIZE:-1}" != "0" ]]; then + if fpmPoolFilesSetTheProcessManager; then + echo "PHP-FPM: a pool file sets pm.* to other values than the image, so the pool is not sized to the container" + else + fpmPoolSize + fi +fi +unset -f fpmPoolFilesSetTheProcessManager fpmPoolSetValue fpmPoolSize + # General fpm pool setting for ENV_VAR in $(envListVars "fpm\.pool\."); do env_key=${ENV_VAR#fpm.pool.} diff --git a/docker/php/8.5/conf/etc/php/php.webdevops.ini b/docker/php/8.5/conf/etc/php/php.webdevops.ini index 829c21e23..8b134c192 100644 --- a/docker/php/8.5/conf/etc/php/php.webdevops.ini +++ b/docker/php/8.5/conf/etc/php/php.webdevops.ini @@ -33,4 +33,4 @@ mysqli.default_host = mysql opcache.memory_consumption = 256 opcache.interned_strings_buffer = 16 -opcache.max_accelerated_files = 7963 +opcache.max_accelerated_files = 32531 diff --git a/docker/php/8.5/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh b/docker/php/8.5/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh index e04580266..de4f1cc83 100644 --- a/docker/php/8.5/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh +++ b/docker/php/8.5/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh @@ -47,3 +47,9 @@ go-replace --mode=line --regex \ go-replace --mode=lineinfile --regex \ -s '^[\s;]*clear_env[\s]*=' -r 'clear_env = no' \ -- /opt/docker/etc/php/fpm/pool.d/application.conf + +# A worker ends after 1000 requests and a new one takes its place, so that memory that leaks in a PHP extension or in +# the application does not add up +go-replace --mode=lineinfile --regex \ + -s '^[\s;]*pm.max_requests[\s]*=' -r 'pm.max_requests = 1000' \ + -- /opt/docker/etc/php/fpm/pool.d/application.conf diff --git a/docker/php/8.5/conf/provision/entrypoint.d/20-php-fpm.sh b/docker/php/8.5/conf/provision/entrypoint.d/20-php-fpm.sh index dfbc12179..9563b51df 100644 --- a/docker/php/8.5/conf/provision/entrypoint.d/20-php-fpm.sh +++ b/docker/php/8.5/conf/provision/entrypoint.d/20-php-fpm.sh @@ -31,6 +31,172 @@ FPM_POOL_ENVIRONMENT_CONF=/opt/docker/etc/php/fpm/pool.d/zz-environment.conf echo '; container env settings, written at every start' > "$FPM_POOL_ENVIRONMENT_CONF" echo '[www]' >> "$FPM_POOL_ENVIRONMENT_CONF" +# Whether a pool file sets the process manager to other values than application.conf of the image, which has those of +# www.conf of the official PHP image: e.g. an application.conf or a www.conf of your own. zz-environment.conf does not +# count, it is being written. +fpmPoolFilesSetTheProcessManager() { + local file + local files=(/dev/null) + for file in /opt/docker/etc/php/fpm/pool.d/*.conf; do + if [[ "$file" != "$FPM_POOL_ENVIRONMENT_CONF" ]]; then + files+=("$file") + fi + done + + awk ' + BEGIN { + split("pm=dynamic pm.max_children=5 pm.start_servers=2 pm.min_spare_servers=1 pm.max_spare_servers=3", values, " ") + for (i in values) image[values[i]] = 1 + } + { sub(/;.*/, ""); gsub(/[ \t\r"]/, "") } + /^pm(\.max_children|\.start_servers|\.min_spare_servers|\.max_spare_servers)?=/ && !($0 in image) { other = 1 } + END { exit !other } + ' "${files[@]}" +} + +# The value set for pm.<$1>: FPM_PM_<$2>, which is written after fpm.pool.pm.<$1> and wins, else fpm.pool.pm.<$1>. +fpmPoolSetValue() { + local variable="FPM_PM_$2" + if [[ -n "${!variable+x}" ]]; then + echo "${!variable}" + else + envGetValue "fpm.pool.pm.$1" + fi +} + +# Sizes the process manager to the CPUs and the memory of the container, see docs/environment-variables.md#php-fpm: +# pm.max_children is the smaller of the workers of FPM_AUTO_CHILD_MEMORY that fit into the memory beside OPcache and +# 256 MB for the other processes, and of FPM_AUTO_CHILDREN_PER_CPU per CPU, at least 2; the spare servers follow the +# CPUs. A value set with FPM_PM_* or fpm.pool.pm.* stays, and the others are computed to fit it. +fpmPoolSize() { + local cpus memory quota="" period="" limit="" cpuSource="no limit" memorySource="no limit" + local childMemory perCpu opcacheMemory value + local setChildren setStart setMin setMax children start min max + local positive='^[1-9][0-9]*$' + + # the CPU limit of the container, rounded up, at most the CPUs it may run on + cpus=$(nproc) + if [[ -r /sys/fs/cgroup/cpu.max ]]; then + read -r quota period < /sys/fs/cgroup/cpu.max || true + limit="cgroup v2 limit" + elif [[ -r /sys/fs/cgroup/cpu/cpu.cfs_quota_us && -r /sys/fs/cgroup/cpu/cpu.cfs_period_us ]]; then + quota=$(cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us) + period=$(cat /sys/fs/cgroup/cpu/cpu.cfs_period_us) + limit="cgroup v1 limit" + fi + if [[ "$quota" =~ $positive && "$period" =~ $positive ]] && (( (quota + period - 1) / period < cpus )); then + cpus=$(( (quota + period - 1) / period )) + cpuSource=$limit + fi + + # the memory limit of the container; without one, or a larger one, the memory of the host + memory=$(awk '/^MemTotal:/ { print int($2 / 1024) }' /proc/meminfo) + limit="" + if [[ -r /sys/fs/cgroup/memory.max ]]; then + limit=$(cat /sys/fs/cgroup/memory.max) + memorySource="cgroup v2 limit" + elif [[ -r /sys/fs/cgroup/memory/memory.limit_in_bytes ]]; then + limit=$(cat /sys/fs/cgroup/memory/memory.limit_in_bytes) + memorySource="cgroup v1 limit" + fi + if [[ "$limit" =~ $positive ]] && (( limit / 1048576 < memory )); then + memory=$(( limit / 1048576 )) + else + memorySource="no limit" + fi + + if [[ "${FPM_AUTO_CHILD_MEMORY:-128M}" =~ ^([1-9][0-9]*)([MmGg]?)$ ]]; then + childMemory=${BASH_REMATCH[1]} + if [[ "${BASH_REMATCH[2]}" == [Gg] ]]; then + childMemory=$(( childMemory * 1024 )) + fi + else + echo "PHP-FPM: FPM_AUTO_CHILD_MEMORY=${FPM_AUTO_CHILD_MEMORY} is not a size such as 128M, using 128M" >&2 + childMemory=128 + fi + + perCpu=${FPM_AUTO_CHILDREN_PER_CPU:-4} + if [[ ! "$perCpu" =~ $positive ]]; then + echo "PHP-FPM: FPM_AUTO_CHILDREN_PER_CPU=${perCpu} is not a positive number, using 4" >&2 + perCpu=4 + fi + + # PHP_OPCACHE_MEMORY_CONSUMPTION is written after php.opcache.memory_consumption and wins + opcacheMemory=${PHP_OPCACHE_MEMORY_CONSUMPTION-$(envGetValue php.opcache.memory_consumption)} + if [[ ! "$opcacheMemory" =~ $positive ]]; then + opcacheMemory=256 + fi + + setChildren=$(fpmPoolSetValue max_children MAX_CHILDREN) + setStart=$(fpmPoolSetValue start_servers START_SERVERS) + setMin=$(fpmPoolSetValue min_spare_servers MIN_SPARE_SERVERS) + setMax=$(fpmPoolSetValue max_spare_servers MAX_SPARE_SERVERS) + + children=$(( (memory - opcacheMemory - 256) / childMemory )) + if (( cpus * perCpu < children )); then + children=$(( cpus * perCpu )) + fi + for value in 2 "$setStart" "$setMin" "$setMax"; do + if [[ "$value" =~ $positive ]] && (( value > children )); then + children=$value + fi + done + if [[ "$setChildren" =~ $positive ]]; then + children=$setChildren + fi + + min=$(( cpus / 2 )) + for value in "$children" "$setMax" "$setStart"; do + if [[ "$value" =~ $positive ]] && (( value < min )); then + min=$value + fi + done + if (( min < 1 )); then + min=1 + fi + if [[ "$setMin" =~ $positive ]]; then + min=$setMin + fi + + max=$(( cpus * 2 )) + if (( children < max )); then + max=$children + fi + for value in "$min" "$setStart"; do + if [[ "$value" =~ $positive ]] && (( value > max )); then + max=$value + fi + done + if [[ "$setMax" =~ $positive ]]; then + max=$setMax + fi + + # the default of PHP-FPM + start=$(( min + (max - min) / 2 )) + if [[ "$setStart" =~ $positive ]]; then + start=$setStart + fi + + # only the values that are not set; zz-environment.conf gets the ones that are set below + if [[ -z "$setChildren" ]]; then echo "pm.max_children = ${children}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setStart" ]]; then echo "pm.start_servers = ${start}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setMin" ]]; then echo "pm.min_spare_servers = ${min}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setMax" ]]; then echo "pm.max_spare_servers = ${max}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + + echo "PHP-FPM: ${cpus} CPUs (${cpuSource}), ${memory} MB memory (${memorySource}) ->" \ + "pm.max_children=${children}${setChildren:+ (set)}, pm.start_servers=${start}${setStart:+ (set)}," \ + "pm.min_spare_servers=${min}${setMin:+ (set)}, pm.max_spare_servers=${max}${setMax:+ (set)}" +} + +if [[ "${FPM_AUTO_SIZE:-1}" != "0" ]]; then + if fpmPoolFilesSetTheProcessManager; then + echo "PHP-FPM: a pool file sets pm.* to other values than the image, so the pool is not sized to the container" + else + fpmPoolSize + fi +fi +unset -f fpmPoolFilesSetTheProcessManager fpmPoolSetValue fpmPoolSize + # General fpm pool setting for ENV_VAR in $(envListVars "fpm\.pool\."); do env_key=${ENV_VAR#fpm.pool.} diff --git a/docker/php/8.6-alpine/conf/etc/php/php.webdevops.ini b/docker/php/8.6-alpine/conf/etc/php/php.webdevops.ini index 829c21e23..8b134c192 100644 --- a/docker/php/8.6-alpine/conf/etc/php/php.webdevops.ini +++ b/docker/php/8.6-alpine/conf/etc/php/php.webdevops.ini @@ -33,4 +33,4 @@ mysqli.default_host = mysql opcache.memory_consumption = 256 opcache.interned_strings_buffer = 16 -opcache.max_accelerated_files = 7963 +opcache.max_accelerated_files = 32531 diff --git a/docker/php/8.6-alpine/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh b/docker/php/8.6-alpine/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh index e04580266..de4f1cc83 100644 --- a/docker/php/8.6-alpine/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh +++ b/docker/php/8.6-alpine/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh @@ -47,3 +47,9 @@ go-replace --mode=line --regex \ go-replace --mode=lineinfile --regex \ -s '^[\s;]*clear_env[\s]*=' -r 'clear_env = no' \ -- /opt/docker/etc/php/fpm/pool.d/application.conf + +# A worker ends after 1000 requests and a new one takes its place, so that memory that leaks in a PHP extension or in +# the application does not add up +go-replace --mode=lineinfile --regex \ + -s '^[\s;]*pm.max_requests[\s]*=' -r 'pm.max_requests = 1000' \ + -- /opt/docker/etc/php/fpm/pool.d/application.conf diff --git a/docker/php/8.6-alpine/conf/provision/entrypoint.d/20-php-fpm.sh b/docker/php/8.6-alpine/conf/provision/entrypoint.d/20-php-fpm.sh index dfbc12179..9563b51df 100644 --- a/docker/php/8.6-alpine/conf/provision/entrypoint.d/20-php-fpm.sh +++ b/docker/php/8.6-alpine/conf/provision/entrypoint.d/20-php-fpm.sh @@ -31,6 +31,172 @@ FPM_POOL_ENVIRONMENT_CONF=/opt/docker/etc/php/fpm/pool.d/zz-environment.conf echo '; container env settings, written at every start' > "$FPM_POOL_ENVIRONMENT_CONF" echo '[www]' >> "$FPM_POOL_ENVIRONMENT_CONF" +# Whether a pool file sets the process manager to other values than application.conf of the image, which has those of +# www.conf of the official PHP image: e.g. an application.conf or a www.conf of your own. zz-environment.conf does not +# count, it is being written. +fpmPoolFilesSetTheProcessManager() { + local file + local files=(/dev/null) + for file in /opt/docker/etc/php/fpm/pool.d/*.conf; do + if [[ "$file" != "$FPM_POOL_ENVIRONMENT_CONF" ]]; then + files+=("$file") + fi + done + + awk ' + BEGIN { + split("pm=dynamic pm.max_children=5 pm.start_servers=2 pm.min_spare_servers=1 pm.max_spare_servers=3", values, " ") + for (i in values) image[values[i]] = 1 + } + { sub(/;.*/, ""); gsub(/[ \t\r"]/, "") } + /^pm(\.max_children|\.start_servers|\.min_spare_servers|\.max_spare_servers)?=/ && !($0 in image) { other = 1 } + END { exit !other } + ' "${files[@]}" +} + +# The value set for pm.<$1>: FPM_PM_<$2>, which is written after fpm.pool.pm.<$1> and wins, else fpm.pool.pm.<$1>. +fpmPoolSetValue() { + local variable="FPM_PM_$2" + if [[ -n "${!variable+x}" ]]; then + echo "${!variable}" + else + envGetValue "fpm.pool.pm.$1" + fi +} + +# Sizes the process manager to the CPUs and the memory of the container, see docs/environment-variables.md#php-fpm: +# pm.max_children is the smaller of the workers of FPM_AUTO_CHILD_MEMORY that fit into the memory beside OPcache and +# 256 MB for the other processes, and of FPM_AUTO_CHILDREN_PER_CPU per CPU, at least 2; the spare servers follow the +# CPUs. A value set with FPM_PM_* or fpm.pool.pm.* stays, and the others are computed to fit it. +fpmPoolSize() { + local cpus memory quota="" period="" limit="" cpuSource="no limit" memorySource="no limit" + local childMemory perCpu opcacheMemory value + local setChildren setStart setMin setMax children start min max + local positive='^[1-9][0-9]*$' + + # the CPU limit of the container, rounded up, at most the CPUs it may run on + cpus=$(nproc) + if [[ -r /sys/fs/cgroup/cpu.max ]]; then + read -r quota period < /sys/fs/cgroup/cpu.max || true + limit="cgroup v2 limit" + elif [[ -r /sys/fs/cgroup/cpu/cpu.cfs_quota_us && -r /sys/fs/cgroup/cpu/cpu.cfs_period_us ]]; then + quota=$(cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us) + period=$(cat /sys/fs/cgroup/cpu/cpu.cfs_period_us) + limit="cgroup v1 limit" + fi + if [[ "$quota" =~ $positive && "$period" =~ $positive ]] && (( (quota + period - 1) / period < cpus )); then + cpus=$(( (quota + period - 1) / period )) + cpuSource=$limit + fi + + # the memory limit of the container; without one, or a larger one, the memory of the host + memory=$(awk '/^MemTotal:/ { print int($2 / 1024) }' /proc/meminfo) + limit="" + if [[ -r /sys/fs/cgroup/memory.max ]]; then + limit=$(cat /sys/fs/cgroup/memory.max) + memorySource="cgroup v2 limit" + elif [[ -r /sys/fs/cgroup/memory/memory.limit_in_bytes ]]; then + limit=$(cat /sys/fs/cgroup/memory/memory.limit_in_bytes) + memorySource="cgroup v1 limit" + fi + if [[ "$limit" =~ $positive ]] && (( limit / 1048576 < memory )); then + memory=$(( limit / 1048576 )) + else + memorySource="no limit" + fi + + if [[ "${FPM_AUTO_CHILD_MEMORY:-128M}" =~ ^([1-9][0-9]*)([MmGg]?)$ ]]; then + childMemory=${BASH_REMATCH[1]} + if [[ "${BASH_REMATCH[2]}" == [Gg] ]]; then + childMemory=$(( childMemory * 1024 )) + fi + else + echo "PHP-FPM: FPM_AUTO_CHILD_MEMORY=${FPM_AUTO_CHILD_MEMORY} is not a size such as 128M, using 128M" >&2 + childMemory=128 + fi + + perCpu=${FPM_AUTO_CHILDREN_PER_CPU:-4} + if [[ ! "$perCpu" =~ $positive ]]; then + echo "PHP-FPM: FPM_AUTO_CHILDREN_PER_CPU=${perCpu} is not a positive number, using 4" >&2 + perCpu=4 + fi + + # PHP_OPCACHE_MEMORY_CONSUMPTION is written after php.opcache.memory_consumption and wins + opcacheMemory=${PHP_OPCACHE_MEMORY_CONSUMPTION-$(envGetValue php.opcache.memory_consumption)} + if [[ ! "$opcacheMemory" =~ $positive ]]; then + opcacheMemory=256 + fi + + setChildren=$(fpmPoolSetValue max_children MAX_CHILDREN) + setStart=$(fpmPoolSetValue start_servers START_SERVERS) + setMin=$(fpmPoolSetValue min_spare_servers MIN_SPARE_SERVERS) + setMax=$(fpmPoolSetValue max_spare_servers MAX_SPARE_SERVERS) + + children=$(( (memory - opcacheMemory - 256) / childMemory )) + if (( cpus * perCpu < children )); then + children=$(( cpus * perCpu )) + fi + for value in 2 "$setStart" "$setMin" "$setMax"; do + if [[ "$value" =~ $positive ]] && (( value > children )); then + children=$value + fi + done + if [[ "$setChildren" =~ $positive ]]; then + children=$setChildren + fi + + min=$(( cpus / 2 )) + for value in "$children" "$setMax" "$setStart"; do + if [[ "$value" =~ $positive ]] && (( value < min )); then + min=$value + fi + done + if (( min < 1 )); then + min=1 + fi + if [[ "$setMin" =~ $positive ]]; then + min=$setMin + fi + + max=$(( cpus * 2 )) + if (( children < max )); then + max=$children + fi + for value in "$min" "$setStart"; do + if [[ "$value" =~ $positive ]] && (( value > max )); then + max=$value + fi + done + if [[ "$setMax" =~ $positive ]]; then + max=$setMax + fi + + # the default of PHP-FPM + start=$(( min + (max - min) / 2 )) + if [[ "$setStart" =~ $positive ]]; then + start=$setStart + fi + + # only the values that are not set; zz-environment.conf gets the ones that are set below + if [[ -z "$setChildren" ]]; then echo "pm.max_children = ${children}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setStart" ]]; then echo "pm.start_servers = ${start}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setMin" ]]; then echo "pm.min_spare_servers = ${min}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setMax" ]]; then echo "pm.max_spare_servers = ${max}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + + echo "PHP-FPM: ${cpus} CPUs (${cpuSource}), ${memory} MB memory (${memorySource}) ->" \ + "pm.max_children=${children}${setChildren:+ (set)}, pm.start_servers=${start}${setStart:+ (set)}," \ + "pm.min_spare_servers=${min}${setMin:+ (set)}, pm.max_spare_servers=${max}${setMax:+ (set)}" +} + +if [[ "${FPM_AUTO_SIZE:-1}" != "0" ]]; then + if fpmPoolFilesSetTheProcessManager; then + echo "PHP-FPM: a pool file sets pm.* to other values than the image, so the pool is not sized to the container" + else + fpmPoolSize + fi +fi +unset -f fpmPoolFilesSetTheProcessManager fpmPoolSetValue fpmPoolSize + # General fpm pool setting for ENV_VAR in $(envListVars "fpm\.pool\."); do env_key=${ENV_VAR#fpm.pool.} diff --git a/docker/php/8.6/conf/etc/php/php.webdevops.ini b/docker/php/8.6/conf/etc/php/php.webdevops.ini index 829c21e23..8b134c192 100644 --- a/docker/php/8.6/conf/etc/php/php.webdevops.ini +++ b/docker/php/8.6/conf/etc/php/php.webdevops.ini @@ -33,4 +33,4 @@ mysqli.default_host = mysql opcache.memory_consumption = 256 opcache.interned_strings_buffer = 16 -opcache.max_accelerated_files = 7963 +opcache.max_accelerated_files = 32531 diff --git a/docker/php/8.6/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh b/docker/php/8.6/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh index e04580266..de4f1cc83 100644 --- a/docker/php/8.6/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh +++ b/docker/php/8.6/conf/provision/bootstrap.d/20-setup-php-fpm-pool.sh @@ -47,3 +47,9 @@ go-replace --mode=line --regex \ go-replace --mode=lineinfile --regex \ -s '^[\s;]*clear_env[\s]*=' -r 'clear_env = no' \ -- /opt/docker/etc/php/fpm/pool.d/application.conf + +# A worker ends after 1000 requests and a new one takes its place, so that memory that leaks in a PHP extension or in +# the application does not add up +go-replace --mode=lineinfile --regex \ + -s '^[\s;]*pm.max_requests[\s]*=' -r 'pm.max_requests = 1000' \ + -- /opt/docker/etc/php/fpm/pool.d/application.conf diff --git a/docker/php/8.6/conf/provision/entrypoint.d/20-php-fpm.sh b/docker/php/8.6/conf/provision/entrypoint.d/20-php-fpm.sh index dfbc12179..9563b51df 100644 --- a/docker/php/8.6/conf/provision/entrypoint.d/20-php-fpm.sh +++ b/docker/php/8.6/conf/provision/entrypoint.d/20-php-fpm.sh @@ -31,6 +31,172 @@ FPM_POOL_ENVIRONMENT_CONF=/opt/docker/etc/php/fpm/pool.d/zz-environment.conf echo '; container env settings, written at every start' > "$FPM_POOL_ENVIRONMENT_CONF" echo '[www]' >> "$FPM_POOL_ENVIRONMENT_CONF" +# Whether a pool file sets the process manager to other values than application.conf of the image, which has those of +# www.conf of the official PHP image: e.g. an application.conf or a www.conf of your own. zz-environment.conf does not +# count, it is being written. +fpmPoolFilesSetTheProcessManager() { + local file + local files=(/dev/null) + for file in /opt/docker/etc/php/fpm/pool.d/*.conf; do + if [[ "$file" != "$FPM_POOL_ENVIRONMENT_CONF" ]]; then + files+=("$file") + fi + done + + awk ' + BEGIN { + split("pm=dynamic pm.max_children=5 pm.start_servers=2 pm.min_spare_servers=1 pm.max_spare_servers=3", values, " ") + for (i in values) image[values[i]] = 1 + } + { sub(/;.*/, ""); gsub(/[ \t\r"]/, "") } + /^pm(\.max_children|\.start_servers|\.min_spare_servers|\.max_spare_servers)?=/ && !($0 in image) { other = 1 } + END { exit !other } + ' "${files[@]}" +} + +# The value set for pm.<$1>: FPM_PM_<$2>, which is written after fpm.pool.pm.<$1> and wins, else fpm.pool.pm.<$1>. +fpmPoolSetValue() { + local variable="FPM_PM_$2" + if [[ -n "${!variable+x}" ]]; then + echo "${!variable}" + else + envGetValue "fpm.pool.pm.$1" + fi +} + +# Sizes the process manager to the CPUs and the memory of the container, see docs/environment-variables.md#php-fpm: +# pm.max_children is the smaller of the workers of FPM_AUTO_CHILD_MEMORY that fit into the memory beside OPcache and +# 256 MB for the other processes, and of FPM_AUTO_CHILDREN_PER_CPU per CPU, at least 2; the spare servers follow the +# CPUs. A value set with FPM_PM_* or fpm.pool.pm.* stays, and the others are computed to fit it. +fpmPoolSize() { + local cpus memory quota="" period="" limit="" cpuSource="no limit" memorySource="no limit" + local childMemory perCpu opcacheMemory value + local setChildren setStart setMin setMax children start min max + local positive='^[1-9][0-9]*$' + + # the CPU limit of the container, rounded up, at most the CPUs it may run on + cpus=$(nproc) + if [[ -r /sys/fs/cgroup/cpu.max ]]; then + read -r quota period < /sys/fs/cgroup/cpu.max || true + limit="cgroup v2 limit" + elif [[ -r /sys/fs/cgroup/cpu/cpu.cfs_quota_us && -r /sys/fs/cgroup/cpu/cpu.cfs_period_us ]]; then + quota=$(cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us) + period=$(cat /sys/fs/cgroup/cpu/cpu.cfs_period_us) + limit="cgroup v1 limit" + fi + if [[ "$quota" =~ $positive && "$period" =~ $positive ]] && (( (quota + period - 1) / period < cpus )); then + cpus=$(( (quota + period - 1) / period )) + cpuSource=$limit + fi + + # the memory limit of the container; without one, or a larger one, the memory of the host + memory=$(awk '/^MemTotal:/ { print int($2 / 1024) }' /proc/meminfo) + limit="" + if [[ -r /sys/fs/cgroup/memory.max ]]; then + limit=$(cat /sys/fs/cgroup/memory.max) + memorySource="cgroup v2 limit" + elif [[ -r /sys/fs/cgroup/memory/memory.limit_in_bytes ]]; then + limit=$(cat /sys/fs/cgroup/memory/memory.limit_in_bytes) + memorySource="cgroup v1 limit" + fi + if [[ "$limit" =~ $positive ]] && (( limit / 1048576 < memory )); then + memory=$(( limit / 1048576 )) + else + memorySource="no limit" + fi + + if [[ "${FPM_AUTO_CHILD_MEMORY:-128M}" =~ ^([1-9][0-9]*)([MmGg]?)$ ]]; then + childMemory=${BASH_REMATCH[1]} + if [[ "${BASH_REMATCH[2]}" == [Gg] ]]; then + childMemory=$(( childMemory * 1024 )) + fi + else + echo "PHP-FPM: FPM_AUTO_CHILD_MEMORY=${FPM_AUTO_CHILD_MEMORY} is not a size such as 128M, using 128M" >&2 + childMemory=128 + fi + + perCpu=${FPM_AUTO_CHILDREN_PER_CPU:-4} + if [[ ! "$perCpu" =~ $positive ]]; then + echo "PHP-FPM: FPM_AUTO_CHILDREN_PER_CPU=${perCpu} is not a positive number, using 4" >&2 + perCpu=4 + fi + + # PHP_OPCACHE_MEMORY_CONSUMPTION is written after php.opcache.memory_consumption and wins + opcacheMemory=${PHP_OPCACHE_MEMORY_CONSUMPTION-$(envGetValue php.opcache.memory_consumption)} + if [[ ! "$opcacheMemory" =~ $positive ]]; then + opcacheMemory=256 + fi + + setChildren=$(fpmPoolSetValue max_children MAX_CHILDREN) + setStart=$(fpmPoolSetValue start_servers START_SERVERS) + setMin=$(fpmPoolSetValue min_spare_servers MIN_SPARE_SERVERS) + setMax=$(fpmPoolSetValue max_spare_servers MAX_SPARE_SERVERS) + + children=$(( (memory - opcacheMemory - 256) / childMemory )) + if (( cpus * perCpu < children )); then + children=$(( cpus * perCpu )) + fi + for value in 2 "$setStart" "$setMin" "$setMax"; do + if [[ "$value" =~ $positive ]] && (( value > children )); then + children=$value + fi + done + if [[ "$setChildren" =~ $positive ]]; then + children=$setChildren + fi + + min=$(( cpus / 2 )) + for value in "$children" "$setMax" "$setStart"; do + if [[ "$value" =~ $positive ]] && (( value < min )); then + min=$value + fi + done + if (( min < 1 )); then + min=1 + fi + if [[ "$setMin" =~ $positive ]]; then + min=$setMin + fi + + max=$(( cpus * 2 )) + if (( children < max )); then + max=$children + fi + for value in "$min" "$setStart"; do + if [[ "$value" =~ $positive ]] && (( value > max )); then + max=$value + fi + done + if [[ "$setMax" =~ $positive ]]; then + max=$setMax + fi + + # the default of PHP-FPM + start=$(( min + (max - min) / 2 )) + if [[ "$setStart" =~ $positive ]]; then + start=$setStart + fi + + # only the values that are not set; zz-environment.conf gets the ones that are set below + if [[ -z "$setChildren" ]]; then echo "pm.max_children = ${children}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setStart" ]]; then echo "pm.start_servers = ${start}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setMin" ]]; then echo "pm.min_spare_servers = ${min}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setMax" ]]; then echo "pm.max_spare_servers = ${max}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + + echo "PHP-FPM: ${cpus} CPUs (${cpuSource}), ${memory} MB memory (${memorySource}) ->" \ + "pm.max_children=${children}${setChildren:+ (set)}, pm.start_servers=${start}${setStart:+ (set)}," \ + "pm.min_spare_servers=${min}${setMin:+ (set)}, pm.max_spare_servers=${max}${setMax:+ (set)}" +} + +if [[ "${FPM_AUTO_SIZE:-1}" != "0" ]]; then + if fpmPoolFilesSetTheProcessManager; then + echo "PHP-FPM: a pool file sets pm.* to other values than the image, so the pool is not sized to the container" + else + fpmPoolSize + fi +fi +unset -f fpmPoolFilesSetTheProcessManager fpmPoolSetValue fpmPoolSize + # General fpm pool setting for ENV_VAR in $(envListVars "fpm\.pool\."); do env_key=${ENV_VAR#fpm.pool.} diff --git a/docs/customization.md b/docs/customization.md index 34a00dfec..6ac1db625 100644 --- a/docs/customization.md +++ b/docs/customization.md @@ -322,11 +322,11 @@ Images: `php-*`. Files in `/opt/docker/etc/php/`: | `php.ini` | For your settings, empty by default; [`docker-php-setting`](#tools-for-your-dockerfile) appends to it. Loaded as `99-docker.ini`, after `98-webdevops.ini`, so its values win over the environment variables. A `:ro` mount works. | | `fpm/php-fpm.conf` | PHP-FPM main configuration; `FPM_PROCESS_MAX` and `fpm.global.*` are appended at every start. | | `fpm/pool.d/application.conf` | The PHP-FPM pool `[www]`. You can replace it, also with a `:ro` mount, unless `php.error_log` is set: its value is written into this file at start, and with `:ro` the container exits at start. | -| `fpm/pool.d/zz-environment.conf` | Written at every start: the `fpm.pool.*` values, the `FPM_*` values except `FPM_PROCESS_MAX`, `PHP_SENDMAIL_PATH` and the user of the pool. Do not replace or edit it. | +| `fpm/pool.d/zz-environment.conf` | Written at every start: the size of the pool computed from the CPUs and the memory of the container, the `fpm.pool.*` values, the `FPM_*` values except `FPM_PROCESS_MAX`, `PHP_SENDMAIL_PATH` and the user of the pool. Do not replace or edit it. | `PHP_SENDMAIL_PATH` is the exception to the order above. It is also written into the pool as `php_admin_value[sendmail_path]`, and it is always set (default `/usr/sbin/sendmail -t -i`). So for requests handled by PHP-FPM, `sendmail_path` in your `php.ini` has no effect; only the PHP CLI uses it. Set `PHP_SENDMAIL_PATH` instead. -PHP-FPM reads the `*.conf` files in `fpm/pool.d/` in the order of their names: `application.conf`, then `docker.conf` of the official PHP image, then `zz-environment.conf`. For a directive set in more than one of them, the last one wins. So the variables win over `application.conf`, over `docker.conf` and over pool files of your own whose names sort before `zz-environment.conf`, such as `www.conf`. Name your pool file `zzz-pool.conf`, for example, if its directives should win over the variables. For `php_value[...]` and `php_admin_value[...]` it is the other way round: the first one wins. +PHP-FPM reads the `*.conf` files in `fpm/pool.d/` in the order of their names: `application.conf`, then `docker.conf` of the official PHP image, then `zz-environment.conf`. For a directive set in more than one of them, the last one wins. So the variables win over `application.conf`, over `docker.conf` and over pool files of your own whose names sort before `zz-environment.conf`, such as `www.conf`. Name your pool file `zzz-pool.conf`, for example, if its directives should win over the variables. For `php_value[...]` and `php_admin_value[...]` it is the other way round: the first one wins. The computed size of the pool goes to `zz-environment.conf` only while no pool file sets `pm` or one of the numbers of workers to another value than the image, see [Size of the PHP-FPM pool](environment-variables.md#size-of-the-php-fpm-pool): `pm.max_children` and the spare servers in a pool file of your own apply, wherever it sorts. ### supervisord diff --git a/docs/dockerhub/php-apache.md b/docs/dockerhub/php-apache.md index 3de84efad..80a16d4b0 100644 --- a/docs/dockerhub/php-apache.md +++ b/docs/dockerhub/php-apache.md @@ -1,6 +1,6 @@ # webdevops/php-apache -[`webdevops/php`](https://hub.docker.com/r/webdevops/php) plus Apache. Apache serves `/app` and passes PHP requests to PHP-FPM in the same container. Ports: `80` (HTTP) and `443` (HTTPS with a self-signed certificate). +[`webdevops/php`](https://hub.docker.com/r/webdevops/php) plus Apache. Apache serves `/app` and passes PHP requests to PHP-FPM in the same container. Ports: `80` (HTTP) and `443` (HTTPS with a self-signed certificate). At every start, PHP-FPM sizes its pool of workers to the CPU and memory limits of the container, or to the host without limits, see [Size of the PHP-FPM pool](https://github.com/webdevops/Dockerfile/blob/master/docs/environment-variables.md#size-of-the-php-fpm-pool). Always use one of the tags below: an image without a tag (`latest`) and the `alpine` tag are old builds from 2019 and 2020 that get no updates, see [Known issues](https://github.com/webdevops/Dockerfile/blob/master/docs/known-issues.md#latest-and-alpine-tags-of-the-php-images). diff --git a/docs/dockerhub/php-nginx.md b/docs/dockerhub/php-nginx.md index 78ba3abf5..11b060360 100644 --- a/docs/dockerhub/php-nginx.md +++ b/docs/dockerhub/php-nginx.md @@ -1,6 +1,6 @@ # webdevops/php-nginx -[`webdevops/php`](https://hub.docker.com/r/webdevops/php) plus Nginx. Nginx serves `/app` and passes PHP requests to PHP-FPM in the same container. Ports: `80` (HTTP) and `443` (HTTPS with a self-signed certificate). +[`webdevops/php`](https://hub.docker.com/r/webdevops/php) plus Nginx. Nginx serves `/app` and passes PHP requests to PHP-FPM in the same container. Ports: `80` (HTTP) and `443` (HTTPS with a self-signed certificate). At every start, PHP-FPM sizes its pool of workers to the CPU and memory limits of the container, or to the host without limits, see [Size of the PHP-FPM pool](https://github.com/webdevops/Dockerfile/blob/master/docs/environment-variables.md#size-of-the-php-fpm-pool). Always use one of the tags below: an image without a tag (`latest`) and the `alpine` tag are old builds from 2019 and 2020 that get no updates, see [Known issues](https://github.com/webdevops/Dockerfile/blob/master/docs/known-issues.md#latest-and-alpine-tags-of-the-php-images). diff --git a/docs/dockerhub/php.md b/docs/dockerhub/php.md index 7fcce1180..4f03b0653 100644 --- a/docs/dockerhub/php.md +++ b/docs/dockerhub/php.md @@ -1,6 +1,6 @@ # webdevops/php -PHP-FPM on top of the official PHP image, with the webdevops entrypoint, `supervisord`, cron, syslog-ng and the `application` user. PHP-FPM listens on port `9000`. For a web server in the same container, use [`webdevops/php-nginx`](https://hub.docker.com/r/webdevops/php-nginx) or [`webdevops/php-apache`](https://hub.docker.com/r/webdevops/php-apache). +PHP-FPM on top of the official PHP image, with the webdevops entrypoint, `supervisord`, cron, syslog-ng and the `application` user. PHP-FPM listens on port `9000`. At every start, PHP-FPM sizes its pool of workers to the CPU and memory limits of the container, or to the host without limits, see [Size of the PHP-FPM pool](https://github.com/webdevops/Dockerfile/blob/master/docs/environment-variables.md#size-of-the-php-fpm-pool). For a web server in the same container, use [`webdevops/php-nginx`](https://hub.docker.com/r/webdevops/php-nginx) or [`webdevops/php-apache`](https://hub.docker.com/r/webdevops/php-apache). Always use one of the tags below: an image without a tag (`latest`) and the `alpine` tag are old builds from 2019 and 2020 that get no updates, see [Known issues](https://github.com/webdevops/Dockerfile/blob/master/docs/known-issues.md#latest-and-alpine-tags-of-the-php-images). diff --git a/docs/environment-variables.md b/docs/environment-variables.md index 78a1f8c3e..7fb6bd2cb 100644 --- a/docs/environment-variables.md +++ b/docs/environment-variables.md @@ -72,7 +72,7 @@ The following shortcuts set a single directive: | `PHP_POST_MAX_SIZE` | `post_max_size` | `50M` | | `PHP_UPLOAD_MAX_FILESIZE` | `upload_max_filesize` | `50M` | | `PHP_OPCACHE_MEMORY_CONSUMPTION` | `opcache.memory_consumption` | `256` | -| `PHP_OPCACHE_MAX_ACCELERATED_FILES` | `opcache.max_accelerated_files` | `7963` | +| `PHP_OPCACHE_MAX_ACCELERATED_FILES` | `opcache.max_accelerated_files` | `32531` | | `PHP_OPCACHE_VALIDATE_TIMESTAMPS` | `opcache.validate_timestamps` | `1` | | `PHP_OPCACHE_REVALIDATE_FREQ` | `opcache.revalidate_freq` | `2` (`0` in development images) | | `PHP_OPCACHE_INTERNED_STRINGS_BUFFER` | `opcache.interned_strings_buffer` | `16` | @@ -96,18 +96,52 @@ The following shortcuts set a single directive: | Variable | PHP-FPM directive | Default | | --- | --- | --- | | `FPM_PROCESS_MAX` | `process.max` | `0` | -| `FPM_PM_MAX_CHILDREN` | `pm.max_children` | `5` | -| `FPM_PM_START_SERVERS` | `pm.start_servers` | `2` | -| `FPM_PM_MIN_SPARE_SERVERS` | `pm.min_spare_servers` | `1` | -| `FPM_PM_MAX_SPARE_SERVERS` | `pm.max_spare_servers` | `3` | +| `FPM_PM_MAX_CHILDREN` | `pm.max_children` | computed, see [Size of the PHP-FPM pool](#size-of-the-php-fpm-pool) | +| `FPM_PM_START_SERVERS` | `pm.start_servers` | computed | +| `FPM_PM_MIN_SPARE_SERVERS` | `pm.min_spare_servers` | computed | +| `FPM_PM_MAX_SPARE_SERVERS` | `pm.max_spare_servers` | computed | | `FPM_PROCESS_IDLE_TIMEOUT` | `pm.process_idle_timeout` | `10s` | -| `FPM_MAX_REQUESTS` | `pm.max_requests` | `0` | +| `FPM_MAX_REQUESTS` | `pm.max_requests` | `1000`: a worker ends after 1000 requests and a new one takes its place, so that memory that leaks in an extension or the application does not add up. `0` keeps the workers. | | `FPM_REQUEST_TERMINATE_TIMEOUT` | `request_terminate_timeout` | `0` | | `FPM_RLIMIT_FILES` | `rlimit_files` | `0` | | `FPM_RLIMIT_CORE` | `rlimit_core` | `0` | The pool uses `pm = dynamic` and runs as `application`. `FPM_PROCESS_IDLE_TIMEOUT` applies only with `fpm.pool.pm: ondemand`; with `dynamic`, PHP-FPM keeps between `FPM_PM_MIN_SPARE_SERVERS` and `FPM_PM_MAX_SPARE_SERVERS` idle workers instead. +### Size of the PHP-FPM pool + +At every start, the entrypoint sizes the pool to the CPUs and the memory of the container: + +| Directive | Value | +| --- | --- | +| `pm.max_children` | The smaller of two numbers, at least `2`: the workers of `FPM_AUTO_CHILD_MEMORY` that fit into the memory, less `PHP_OPCACHE_MEMORY_CONSUMPTION` for OPcache and 256 MB for the web server, cron and the other processes; and `FPM_AUTO_CHILDREN_PER_CPU` workers per CPU. | +| `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 the two, as PHP-FPM computes it by default | + +The CPUs are the CPU limit of the container (`cpus`), rounded up, and at most the CPUs it may run on (`cpuset`); without a limit, the CPUs of the host. The memory is the memory limit of the container (`mem_limit`); without one, the memory of the host. Both cgroup v2 and cgroup v1 limits count. For example: + +| Container | `pm.max_children` | `pm.start_servers` | `pm.min_spare_servers` | `pm.max_spare_servers` | +| --- | --- | --- | --- | --- | +| `cpus: 1`, `mem_limit: 512m` | `2` | `1` | `1` | `2` | +| `cpus: 2`, `mem_limit: 1g` | `4` | `2` | `1` | `4` | +| `cpus: 2`, `mem_limit: 2g` | `8` | `2` | `1` | `4` | +| no limits, on a host with 4 CPUs and 16 GB | `16` | `5` | `2` | `8` | + +The container log shows the result at start, for example `PHP-FPM: 2 CPUs (cgroup v2 limit), 1024 MB memory (cgroup v2 limit) -> pm.max_children=4, pm.start_servers=2, pm.min_spare_servers=1, pm.max_spare_servers=4`. + +| Variable | Default | Effect | +| --- | --- | --- | +| `FPM_AUTO_SIZE` | `1` | `0` switches the sizing off. The pool then keeps the values of the official PHP image: `pm.max_children` `5`, `pm.start_servers` `2`, `pm.min_spare_servers` `1`, `pm.max_spare_servers` `3`. | +| `FPM_AUTO_CHILD_MEMORY` | `128M` | Memory of one worker, in megabytes (`M`) or gigabytes (`G`). OPcache is shared by all workers and counted once, see above. `memory_limit` (`512M`) is the most a single request may use, not what a worker usually needs. | +| `FPM_AUTO_CHILDREN_PER_CPU` | `4` | Workers per CPU. A PHP request usually spends most of its time waiting for the database or other services; lower it for requests that mostly compute. | + +A value set with `FPM_PM_*` or `fpm.pool.pm.*` stays as set, and the others are computed to fit it: with `cpus: 2` and `mem_limit: 1g`, `FPM_PM_MAX_CHILDREN: "7"` keeps `pm.max_spare_servers` at `4`, `FPM_PM_MAX_CHILDREN: "3"` lowers it to `3`. If one of your pool files sets `pm`, `pm.max_children`, `pm.start_servers`, `pm.min_spare_servers` or `pm.max_spare_servers` to another value than the image, for example an `application.conf` or a `www.conf` of your own, nothing is computed and the values of your pool files apply; the container log then says `PHP-FPM: a pool file sets pm.* to other values than the image, so the pool is not sized to the container`. + +The sizing does not change `pm`. With `fpm.pool.pm: static`, PHP-FPM starts all `pm.max_children` workers at once. + +Without limits, every container sizes itself to the whole host. With `pm = dynamic`, PHP-FPM starts more workers only under load, but several busy containers on one host can then use more memory than the host has. In production, set `cpus` and `mem_limit` for each container, see [Production](images/php.md#production). + ## Uploads and long requests Images: `php-*`; the web server limits apply to `php-nginx` and `php-apache` and their `-dev` variants. diff --git a/docs/images/php.md b/docs/images/php.md index 98522f5b4..8604003d0 100644 --- a/docs/images/php.md +++ b/docs/images/php.md @@ -43,7 +43,8 @@ Started by default: `php-fpm`, `cron`, `syslog`. The `-dev` variant also starts | `date.timezone` | `UTC` | | `display_errors` | `0` | | `session.upload_progress.enabled` | `0` | -| OPcache | enabled, `opcache.memory_consumption` `256`, `opcache.revalidate_freq` `2` | +| OPcache | enabled, `opcache.memory_consumption` `256`, `opcache.max_accelerated_files` `32531`, `opcache.revalidate_freq` `2` | +| PHP-FPM workers | sized to the CPUs and the memory of the container at every start, see [Size of the PHP-FPM pool](../environment-variables.md#size-of-the-php-fpm-pool); a worker ends after `1000` requests | All of these can be changed with environment variables, see [PHP](../environment-variables.md#php). For larger uploads and long requests, see [Uploads and long requests](../environment-variables.md#uploads-and-long-requests); for production, see [Production](#production). @@ -163,7 +164,8 @@ Use the images without `-dev` in production. The `-dev` images load Xdebug and s Check these defaults for your application: -- **PHP-FPM processes:** `pm.max_children` is `5` (`FPM_PM_MAX_CHILDREN`). PHP-FPM handles at most five requests at the same time; further requests wait. When that happens, PHP-FPM logs `server reached pm.max_children setting (5), consider raising it` to the container log, or to the file of [`LOG_STDERR`](../environment-variables.md#container). Raise the value if the container has memory to spare; each process can use up to `memory_limit` (`512M`). If the log shows `seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers)`, PHP-FPM starts processes too slowly for the load; raise `FPM_PM_START_SERVERS` and the spare server settings, see [PHP-FPM](../environment-variables.md#php-fpm). +- **CPU and memory limits:** set `cpus` and `mem_limit` for the container. PHP-FPM sizes its pool to them at every start: with `cpus: 2` and `mem_limit: 2g`, it handles up to eight requests at the same time, see [Size of the PHP-FPM pool](../environment-variables.md#size-of-the-php-fpm-pool). Without limits, every container sizes itself to the whole host, and several busy containers on one host can use more memory than it has. +- **PHP-FPM processes:** the container log shows the size of the pool at start. When all workers are busy, further requests wait, and PHP-FPM logs `server reached pm.max_children setting (8), consider raising it` to the container log, or to the file of [`LOG_STDERR`](../environment-variables.md#container). Give the container more CPUs or memory, or raise `FPM_AUTO_CHILDREN_PER_CPU` or `FPM_PM_MAX_CHILDREN` if it has memory to spare; each process can use up to `memory_limit` (`512M`). If the log shows `seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers)`, PHP-FPM starts processes too slowly for the load; raise `FPM_PM_START_SERVERS` and the spare server settings, see [PHP-FPM](../environment-variables.md#php-fpm). - **Code in the image:** if your Dockerfile copies the application code into the image, set `PHP_OPCACHE_VALIDATE_TIMESTAMPS: "0"`. PHP then no longer checks the files for changes; a changed file takes effect only after PHP-FPM restarts. Do not use it with code mounted from the host. - **Errors:** `display_errors` is `0` (`PHP_DISPLAY_ERRORS`), so error messages are not shown to visitors. Keep it that way. - **Uploads and long requests:** the limits are in [Uploads and long requests](../environment-variables.md#uploads-and-long-requests). @@ -174,8 +176,9 @@ services: build: . ports: - "8080:80" + cpus: 2 + mem_limit: 2g environment: - FPM_PM_MAX_CHILDREN: "20" PHP_OPCACHE_VALIDATE_TIMESTAMPS: "0" ``` diff --git a/provisioning/php-dev/general/provision/bootstrap.d/20-php-module-opcache.sh b/provisioning/php-dev/general/provision/bootstrap.d/20-php-module-opcache.sh index 0bbb940b4..92158dfe0 100644 --- a/provisioning/php-dev/general/provision/bootstrap.d/20-php-module-opcache.sh +++ b/provisioning/php-dev/general/provision/bootstrap.d/20-php-module-opcache.sh @@ -6,5 +6,5 @@ go-replace --mode=lineinfile --regex \ -s '^[\s;]*opcache.validate_timestamps[\s]*=' -r 'opcache.validate_timestamps = 1' \ -s '^[\s;]*opcache.revalidate_freq[\s]*=' -r 'opcache.revalidate_freq = 0' \ -s '^[\s;]*opcache.interned_strings_buffer[\s]*=' -r 'opcache.interned_strings_buffer = 16' \ - -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 7963' \ + -s '^[\s;]*opcache.max_accelerated_files[\s]*=' -r 'opcache.max_accelerated_files = 32531' \ -- /opt/docker/etc/php/php.webdevops.ini diff --git a/provisioning/php/general/etc/php/php.webdevops.ini b/provisioning/php/general/etc/php/php.webdevops.ini index 829c21e23..8b134c192 100644 --- a/provisioning/php/general/etc/php/php.webdevops.ini +++ b/provisioning/php/general/etc/php/php.webdevops.ini @@ -33,4 +33,4 @@ mysqli.default_host = mysql opcache.memory_consumption = 256 opcache.interned_strings_buffer = 16 -opcache.max_accelerated_files = 7963 +opcache.max_accelerated_files = 32531 diff --git a/provisioning/php/general/provision/bootstrap.d/20-setup-php-fpm-pool.sh b/provisioning/php/general/provision/bootstrap.d/20-setup-php-fpm-pool.sh index e04580266..de4f1cc83 100644 --- a/provisioning/php/general/provision/bootstrap.d/20-setup-php-fpm-pool.sh +++ b/provisioning/php/general/provision/bootstrap.d/20-setup-php-fpm-pool.sh @@ -47,3 +47,9 @@ go-replace --mode=line --regex \ go-replace --mode=lineinfile --regex \ -s '^[\s;]*clear_env[\s]*=' -r 'clear_env = no' \ -- /opt/docker/etc/php/fpm/pool.d/application.conf + +# A worker ends after 1000 requests and a new one takes its place, so that memory that leaks in a PHP extension or in +# the application does not add up +go-replace --mode=lineinfile --regex \ + -s '^[\s;]*pm.max_requests[\s]*=' -r 'pm.max_requests = 1000' \ + -- /opt/docker/etc/php/fpm/pool.d/application.conf diff --git a/provisioning/php/general/provision/entrypoint.d/20-php-fpm.sh b/provisioning/php/general/provision/entrypoint.d/20-php-fpm.sh index dfbc12179..9563b51df 100644 --- a/provisioning/php/general/provision/entrypoint.d/20-php-fpm.sh +++ b/provisioning/php/general/provision/entrypoint.d/20-php-fpm.sh @@ -31,6 +31,172 @@ FPM_POOL_ENVIRONMENT_CONF=/opt/docker/etc/php/fpm/pool.d/zz-environment.conf echo '; container env settings, written at every start' > "$FPM_POOL_ENVIRONMENT_CONF" echo '[www]' >> "$FPM_POOL_ENVIRONMENT_CONF" +# Whether a pool file sets the process manager to other values than application.conf of the image, which has those of +# www.conf of the official PHP image: e.g. an application.conf or a www.conf of your own. zz-environment.conf does not +# count, it is being written. +fpmPoolFilesSetTheProcessManager() { + local file + local files=(/dev/null) + for file in /opt/docker/etc/php/fpm/pool.d/*.conf; do + if [[ "$file" != "$FPM_POOL_ENVIRONMENT_CONF" ]]; then + files+=("$file") + fi + done + + awk ' + BEGIN { + split("pm=dynamic pm.max_children=5 pm.start_servers=2 pm.min_spare_servers=1 pm.max_spare_servers=3", values, " ") + for (i in values) image[values[i]] = 1 + } + { sub(/;.*/, ""); gsub(/[ \t\r"]/, "") } + /^pm(\.max_children|\.start_servers|\.min_spare_servers|\.max_spare_servers)?=/ && !($0 in image) { other = 1 } + END { exit !other } + ' "${files[@]}" +} + +# The value set for pm.<$1>: FPM_PM_<$2>, which is written after fpm.pool.pm.<$1> and wins, else fpm.pool.pm.<$1>. +fpmPoolSetValue() { + local variable="FPM_PM_$2" + if [[ -n "${!variable+x}" ]]; then + echo "${!variable}" + else + envGetValue "fpm.pool.pm.$1" + fi +} + +# Sizes the process manager to the CPUs and the memory of the container, see docs/environment-variables.md#php-fpm: +# pm.max_children is the smaller of the workers of FPM_AUTO_CHILD_MEMORY that fit into the memory beside OPcache and +# 256 MB for the other processes, and of FPM_AUTO_CHILDREN_PER_CPU per CPU, at least 2; the spare servers follow the +# CPUs. A value set with FPM_PM_* or fpm.pool.pm.* stays, and the others are computed to fit it. +fpmPoolSize() { + local cpus memory quota="" period="" limit="" cpuSource="no limit" memorySource="no limit" + local childMemory perCpu opcacheMemory value + local setChildren setStart setMin setMax children start min max + local positive='^[1-9][0-9]*$' + + # the CPU limit of the container, rounded up, at most the CPUs it may run on + cpus=$(nproc) + if [[ -r /sys/fs/cgroup/cpu.max ]]; then + read -r quota period < /sys/fs/cgroup/cpu.max || true + limit="cgroup v2 limit" + elif [[ -r /sys/fs/cgroup/cpu/cpu.cfs_quota_us && -r /sys/fs/cgroup/cpu/cpu.cfs_period_us ]]; then + quota=$(cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us) + period=$(cat /sys/fs/cgroup/cpu/cpu.cfs_period_us) + limit="cgroup v1 limit" + fi + if [[ "$quota" =~ $positive && "$period" =~ $positive ]] && (( (quota + period - 1) / period < cpus )); then + cpus=$(( (quota + period - 1) / period )) + cpuSource=$limit + fi + + # the memory limit of the container; without one, or a larger one, the memory of the host + memory=$(awk '/^MemTotal:/ { print int($2 / 1024) }' /proc/meminfo) + limit="" + if [[ -r /sys/fs/cgroup/memory.max ]]; then + limit=$(cat /sys/fs/cgroup/memory.max) + memorySource="cgroup v2 limit" + elif [[ -r /sys/fs/cgroup/memory/memory.limit_in_bytes ]]; then + limit=$(cat /sys/fs/cgroup/memory/memory.limit_in_bytes) + memorySource="cgroup v1 limit" + fi + if [[ "$limit" =~ $positive ]] && (( limit / 1048576 < memory )); then + memory=$(( limit / 1048576 )) + else + memorySource="no limit" + fi + + if [[ "${FPM_AUTO_CHILD_MEMORY:-128M}" =~ ^([1-9][0-9]*)([MmGg]?)$ ]]; then + childMemory=${BASH_REMATCH[1]} + if [[ "${BASH_REMATCH[2]}" == [Gg] ]]; then + childMemory=$(( childMemory * 1024 )) + fi + else + echo "PHP-FPM: FPM_AUTO_CHILD_MEMORY=${FPM_AUTO_CHILD_MEMORY} is not a size such as 128M, using 128M" >&2 + childMemory=128 + fi + + perCpu=${FPM_AUTO_CHILDREN_PER_CPU:-4} + if [[ ! "$perCpu" =~ $positive ]]; then + echo "PHP-FPM: FPM_AUTO_CHILDREN_PER_CPU=${perCpu} is not a positive number, using 4" >&2 + perCpu=4 + fi + + # PHP_OPCACHE_MEMORY_CONSUMPTION is written after php.opcache.memory_consumption and wins + opcacheMemory=${PHP_OPCACHE_MEMORY_CONSUMPTION-$(envGetValue php.opcache.memory_consumption)} + if [[ ! "$opcacheMemory" =~ $positive ]]; then + opcacheMemory=256 + fi + + setChildren=$(fpmPoolSetValue max_children MAX_CHILDREN) + setStart=$(fpmPoolSetValue start_servers START_SERVERS) + setMin=$(fpmPoolSetValue min_spare_servers MIN_SPARE_SERVERS) + setMax=$(fpmPoolSetValue max_spare_servers MAX_SPARE_SERVERS) + + children=$(( (memory - opcacheMemory - 256) / childMemory )) + if (( cpus * perCpu < children )); then + children=$(( cpus * perCpu )) + fi + for value in 2 "$setStart" "$setMin" "$setMax"; do + if [[ "$value" =~ $positive ]] && (( value > children )); then + children=$value + fi + done + if [[ "$setChildren" =~ $positive ]]; then + children=$setChildren + fi + + min=$(( cpus / 2 )) + for value in "$children" "$setMax" "$setStart"; do + if [[ "$value" =~ $positive ]] && (( value < min )); then + min=$value + fi + done + if (( min < 1 )); then + min=1 + fi + if [[ "$setMin" =~ $positive ]]; then + min=$setMin + fi + + max=$(( cpus * 2 )) + if (( children < max )); then + max=$children + fi + for value in "$min" "$setStart"; do + if [[ "$value" =~ $positive ]] && (( value > max )); then + max=$value + fi + done + if [[ "$setMax" =~ $positive ]]; then + max=$setMax + fi + + # the default of PHP-FPM + start=$(( min + (max - min) / 2 )) + if [[ "$setStart" =~ $positive ]]; then + start=$setStart + fi + + # only the values that are not set; zz-environment.conf gets the ones that are set below + if [[ -z "$setChildren" ]]; then echo "pm.max_children = ${children}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setStart" ]]; then echo "pm.start_servers = ${start}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setMin" ]]; then echo "pm.min_spare_servers = ${min}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + if [[ -z "$setMax" ]]; then echo "pm.max_spare_servers = ${max}" >> "$FPM_POOL_ENVIRONMENT_CONF"; fi + + echo "PHP-FPM: ${cpus} CPUs (${cpuSource}), ${memory} MB memory (${memorySource}) ->" \ + "pm.max_children=${children}${setChildren:+ (set)}, pm.start_servers=${start}${setStart:+ (set)}," \ + "pm.min_spare_servers=${min}${setMin:+ (set)}, pm.max_spare_servers=${max}${setMax:+ (set)}" +} + +if [[ "${FPM_AUTO_SIZE:-1}" != "0" ]]; then + if fpmPoolFilesSetTheProcessManager; then + echo "PHP-FPM: a pool file sets pm.* to other values than the image, so the pool is not sized to the container" + else + fpmPoolSize + fi +fi +unset -f fpmPoolFilesSetTheProcessManager fpmPoolSetValue fpmPoolSize + # General fpm pool setting for ENV_VAR in $(envListVars "fpm\.pool\."); do env_key=${ENV_VAR#fpm.pool.}