diff --git a/ci/image-tests/Checks/PhpEnvironmentChecks.php b/ci/image-tests/Checks/PhpEnvironmentChecks.php index a310374f9..6bd338666 100644 --- a/ci/image-tests/Checks/PhpEnvironmentChecks.php +++ b/ci/image-tests/Checks/PhpEnvironmentChecks.php @@ -177,6 +177,43 @@ public function testPhpSendmailPathFromTheEnvironment(): void } } + /** + * php.error_log, in a container of its own: PHP-FPM and the PHP CLI write the messages of error_log() to that file. + * The file is in /app, not in /tmp: a worker of PHP-FPM creates it as application, and with fs.protected_regular, + * as on the runners of GitHub Actions, even root may not open the file of another user in a sticky directory. + */ + public function testPhpErrorLogFromTheEnvironment(): void + { + $container = $this->startContainer('php-error-log', ['php.error_log' => '/app/image-test-php-error.log']); + self::assertSame('logged', $this->phpFpmBody($container, 'test=error_log')); + $this->assertCommandSucceeds("php -r 'error_log(\"image-test-from-the-cli\");'", $container); + $log = $this->stdout('cat /app/image-test-php-error.log', $container); + self::assertStringContainsString('image-test-from-php-fpm', $log); + self::assertStringContainsString('image-test-from-the-cli', $log); + } + + /** + * Without php.error_log, PHP-FPM logs the errors of PHP scripts to /docker.stderr, which the container of the class, + * without LOG_STDERR, links to the stderr of the container: the message of error_log() reaches the container log. + * With LOG_STDERR, it reaches that file where a worker of PHP-FPM may write to it, as in webdevops/php; the setting + * is checked too, because the container log alone would not tell /docker.stderr from the stderr of the worker. + * The PHP CLI has no error log of its own: its messages go to the stderr of the command, e.g. of docker compose + * exec, not to the container log. + */ + public function testPhpErrorsReachTheContainerLogAndTheStderrOfTheCliByDefault(): void + { + $container = self::container(); + self::assertSame('/docker.stderr', $this->phpFpmBody($container, 'test=error_log_setting')); + self::assertSame('logged', $this->phpFpmBody($container, 'test=error_log')); + $this->eventually(function () use ($container): void { + self::assertStringContainsString('PHP message: image-test-from-php-fpm', $container->logs()); + }); + + $cli = $this->assertCommandSucceeds("php -r 'error_log(\"image-test-from-the-cli\");'", $container); + self::assertStringContainsString('image-test-from-the-cli', $cli->stderr); + self::assertStringNotContainsString('image-test-from-the-cli', $container->logs()); + } + public function testPhpDismodDisablesExtensionsOfTheCliAndPhpFpm(): void { $container = $this->environmentContainer(); diff --git a/ci/image-tests/Fixtures/app/php-test.php b/ci/image-tests/Fixtures/app/php-test.php index b343ccfa4..0f545e304 100644 --- a/ci/image-tests/Fixtures/app/php-test.php +++ b/ci/image-tests/Fixtures/app/php-test.php @@ -74,6 +74,16 @@ echo 'finished'; break; + case 'error_log': + // a fixed message: one from the request would let the request write into the log + error_log('image-test-from-php-fpm'); + echo 'logged'; + break; + + case 'error_log_setting': + echo ini_get('error_log'); + break; + case 'warning': echo $undefinedImageTestVariable; echo 'done'; diff --git a/docker/php-official/8.2-alpine/conf/provision/entrypoint.d/20-php-fpm.sh b/docker/php-official/8.2-alpine/conf/provision/entrypoint.d/20-php-fpm.sh index 5ab722cf7..0bcd0b8d1 100644 --- a/docker/php-official/8.2-alpine/conf/provision/entrypoint.d/20-php-fpm.sh +++ b/docker/php-official/8.2-alpine/conf/provision/entrypoint.d/20-php-fpm.sh @@ -74,6 +74,15 @@ if [[ -n "${FPM_RLIMIT_CORE+x}" ]]; then echo "rlimit_core = ${FPM_RLIMIT_CORE}" >> /opt/docker/etc/php/fpm/pool.d/application.conf fi +# The pool logs the errors of PHP scripts to /docker.stderr with php_admin_value, which a php.ini value cannot override, +# so php.error_log replaces it here. PHP-FPM takes the first of two php_admin_value lines, so a line added below would +# not take effect. +if [[ -n "$(envListVars 'php\.error_log=')" ]]; then + go-replace --mode=lineinfile --regex \ + -s '^[\s;]*php_admin_value\[error_log\][\s]*=' -r "php_admin_value[error_log] = $(envGetValue php.error_log)" \ + -- /opt/docker/etc/php/fpm/pool.d/application.conf +fi + # Workaround for official PHP images if [[ -n "${PHP_SENDMAIL_PATH+x}" ]]; then echo "php_admin_value[sendmail_path] = ${PHP_SENDMAIL_PATH}" >> /opt/docker/etc/php/fpm/pool.d/application.conf diff --git a/docker/php-official/8.2/conf/provision/entrypoint.d/20-php-fpm.sh b/docker/php-official/8.2/conf/provision/entrypoint.d/20-php-fpm.sh index 5ab722cf7..0bcd0b8d1 100644 --- a/docker/php-official/8.2/conf/provision/entrypoint.d/20-php-fpm.sh +++ b/docker/php-official/8.2/conf/provision/entrypoint.d/20-php-fpm.sh @@ -74,6 +74,15 @@ if [[ -n "${FPM_RLIMIT_CORE+x}" ]]; then echo "rlimit_core = ${FPM_RLIMIT_CORE}" >> /opt/docker/etc/php/fpm/pool.d/application.conf fi +# The pool logs the errors of PHP scripts to /docker.stderr with php_admin_value, which a php.ini value cannot override, +# so php.error_log replaces it here. PHP-FPM takes the first of two php_admin_value lines, so a line added below would +# not take effect. +if [[ -n "$(envListVars 'php\.error_log=')" ]]; then + go-replace --mode=lineinfile --regex \ + -s '^[\s;]*php_admin_value\[error_log\][\s]*=' -r "php_admin_value[error_log] = $(envGetValue php.error_log)" \ + -- /opt/docker/etc/php/fpm/pool.d/application.conf +fi + # Workaround for official PHP images if [[ -n "${PHP_SENDMAIL_PATH+x}" ]]; then echo "php_admin_value[sendmail_path] = ${PHP_SENDMAIL_PATH}" >> /opt/docker/etc/php/fpm/pool.d/application.conf diff --git a/docker/php-official/8.3-alpine/conf/provision/entrypoint.d/20-php-fpm.sh b/docker/php-official/8.3-alpine/conf/provision/entrypoint.d/20-php-fpm.sh index 5ab722cf7..0bcd0b8d1 100644 --- a/docker/php-official/8.3-alpine/conf/provision/entrypoint.d/20-php-fpm.sh +++ b/docker/php-official/8.3-alpine/conf/provision/entrypoint.d/20-php-fpm.sh @@ -74,6 +74,15 @@ if [[ -n "${FPM_RLIMIT_CORE+x}" ]]; then echo "rlimit_core = ${FPM_RLIMIT_CORE}" >> /opt/docker/etc/php/fpm/pool.d/application.conf fi +# The pool logs the errors of PHP scripts to /docker.stderr with php_admin_value, which a php.ini value cannot override, +# so php.error_log replaces it here. PHP-FPM takes the first of two php_admin_value lines, so a line added below would +# not take effect. +if [[ -n "$(envListVars 'php\.error_log=')" ]]; then + go-replace --mode=lineinfile --regex \ + -s '^[\s;]*php_admin_value\[error_log\][\s]*=' -r "php_admin_value[error_log] = $(envGetValue php.error_log)" \ + -- /opt/docker/etc/php/fpm/pool.d/application.conf +fi + # Workaround for official PHP images if [[ -n "${PHP_SENDMAIL_PATH+x}" ]]; then echo "php_admin_value[sendmail_path] = ${PHP_SENDMAIL_PATH}" >> /opt/docker/etc/php/fpm/pool.d/application.conf diff --git a/docker/php-official/8.3/conf/provision/entrypoint.d/20-php-fpm.sh b/docker/php-official/8.3/conf/provision/entrypoint.d/20-php-fpm.sh index 5ab722cf7..0bcd0b8d1 100644 --- a/docker/php-official/8.3/conf/provision/entrypoint.d/20-php-fpm.sh +++ b/docker/php-official/8.3/conf/provision/entrypoint.d/20-php-fpm.sh @@ -74,6 +74,15 @@ if [[ -n "${FPM_RLIMIT_CORE+x}" ]]; then echo "rlimit_core = ${FPM_RLIMIT_CORE}" >> /opt/docker/etc/php/fpm/pool.d/application.conf fi +# The pool logs the errors of PHP scripts to /docker.stderr with php_admin_value, which a php.ini value cannot override, +# so php.error_log replaces it here. PHP-FPM takes the first of two php_admin_value lines, so a line added below would +# not take effect. +if [[ -n "$(envListVars 'php\.error_log=')" ]]; then + go-replace --mode=lineinfile --regex \ + -s '^[\s;]*php_admin_value\[error_log\][\s]*=' -r "php_admin_value[error_log] = $(envGetValue php.error_log)" \ + -- /opt/docker/etc/php/fpm/pool.d/application.conf +fi + # Workaround for official PHP images if [[ -n "${PHP_SENDMAIL_PATH+x}" ]]; then echo "php_admin_value[sendmail_path] = ${PHP_SENDMAIL_PATH}" >> /opt/docker/etc/php/fpm/pool.d/application.conf diff --git a/docker/php-official/8.4-alpine/conf/provision/entrypoint.d/20-php-fpm.sh b/docker/php-official/8.4-alpine/conf/provision/entrypoint.d/20-php-fpm.sh index 5ab722cf7..0bcd0b8d1 100644 --- a/docker/php-official/8.4-alpine/conf/provision/entrypoint.d/20-php-fpm.sh +++ b/docker/php-official/8.4-alpine/conf/provision/entrypoint.d/20-php-fpm.sh @@ -74,6 +74,15 @@ if [[ -n "${FPM_RLIMIT_CORE+x}" ]]; then echo "rlimit_core = ${FPM_RLIMIT_CORE}" >> /opt/docker/etc/php/fpm/pool.d/application.conf fi +# The pool logs the errors of PHP scripts to /docker.stderr with php_admin_value, which a php.ini value cannot override, +# so php.error_log replaces it here. PHP-FPM takes the first of two php_admin_value lines, so a line added below would +# not take effect. +if [[ -n "$(envListVars 'php\.error_log=')" ]]; then + go-replace --mode=lineinfile --regex \ + -s '^[\s;]*php_admin_value\[error_log\][\s]*=' -r "php_admin_value[error_log] = $(envGetValue php.error_log)" \ + -- /opt/docker/etc/php/fpm/pool.d/application.conf +fi + # Workaround for official PHP images if [[ -n "${PHP_SENDMAIL_PATH+x}" ]]; then echo "php_admin_value[sendmail_path] = ${PHP_SENDMAIL_PATH}" >> /opt/docker/etc/php/fpm/pool.d/application.conf diff --git a/docker/php-official/8.4/conf/provision/entrypoint.d/20-php-fpm.sh b/docker/php-official/8.4/conf/provision/entrypoint.d/20-php-fpm.sh index 5ab722cf7..0bcd0b8d1 100644 --- a/docker/php-official/8.4/conf/provision/entrypoint.d/20-php-fpm.sh +++ b/docker/php-official/8.4/conf/provision/entrypoint.d/20-php-fpm.sh @@ -74,6 +74,15 @@ if [[ -n "${FPM_RLIMIT_CORE+x}" ]]; then echo "rlimit_core = ${FPM_RLIMIT_CORE}" >> /opt/docker/etc/php/fpm/pool.d/application.conf fi +# The pool logs the errors of PHP scripts to /docker.stderr with php_admin_value, which a php.ini value cannot override, +# so php.error_log replaces it here. PHP-FPM takes the first of two php_admin_value lines, so a line added below would +# not take effect. +if [[ -n "$(envListVars 'php\.error_log=')" ]]; then + go-replace --mode=lineinfile --regex \ + -s '^[\s;]*php_admin_value\[error_log\][\s]*=' -r "php_admin_value[error_log] = $(envGetValue php.error_log)" \ + -- /opt/docker/etc/php/fpm/pool.d/application.conf +fi + # Workaround for official PHP images if [[ -n "${PHP_SENDMAIL_PATH+x}" ]]; then echo "php_admin_value[sendmail_path] = ${PHP_SENDMAIL_PATH}" >> /opt/docker/etc/php/fpm/pool.d/application.conf diff --git a/docker/php-official/8.5-alpine/conf/provision/entrypoint.d/20-php-fpm.sh b/docker/php-official/8.5-alpine/conf/provision/entrypoint.d/20-php-fpm.sh index 5ab722cf7..0bcd0b8d1 100644 --- a/docker/php-official/8.5-alpine/conf/provision/entrypoint.d/20-php-fpm.sh +++ b/docker/php-official/8.5-alpine/conf/provision/entrypoint.d/20-php-fpm.sh @@ -74,6 +74,15 @@ if [[ -n "${FPM_RLIMIT_CORE+x}" ]]; then echo "rlimit_core = ${FPM_RLIMIT_CORE}" >> /opt/docker/etc/php/fpm/pool.d/application.conf fi +# The pool logs the errors of PHP scripts to /docker.stderr with php_admin_value, which a php.ini value cannot override, +# so php.error_log replaces it here. PHP-FPM takes the first of two php_admin_value lines, so a line added below would +# not take effect. +if [[ -n "$(envListVars 'php\.error_log=')" ]]; then + go-replace --mode=lineinfile --regex \ + -s '^[\s;]*php_admin_value\[error_log\][\s]*=' -r "php_admin_value[error_log] = $(envGetValue php.error_log)" \ + -- /opt/docker/etc/php/fpm/pool.d/application.conf +fi + # Workaround for official PHP images if [[ -n "${PHP_SENDMAIL_PATH+x}" ]]; then echo "php_admin_value[sendmail_path] = ${PHP_SENDMAIL_PATH}" >> /opt/docker/etc/php/fpm/pool.d/application.conf diff --git a/docker/php-official/8.5/conf/provision/entrypoint.d/20-php-fpm.sh b/docker/php-official/8.5/conf/provision/entrypoint.d/20-php-fpm.sh index 5ab722cf7..0bcd0b8d1 100644 --- a/docker/php-official/8.5/conf/provision/entrypoint.d/20-php-fpm.sh +++ b/docker/php-official/8.5/conf/provision/entrypoint.d/20-php-fpm.sh @@ -74,6 +74,15 @@ if [[ -n "${FPM_RLIMIT_CORE+x}" ]]; then echo "rlimit_core = ${FPM_RLIMIT_CORE}" >> /opt/docker/etc/php/fpm/pool.d/application.conf fi +# The pool logs the errors of PHP scripts to /docker.stderr with php_admin_value, which a php.ini value cannot override, +# so php.error_log replaces it here. PHP-FPM takes the first of two php_admin_value lines, so a line added below would +# not take effect. +if [[ -n "$(envListVars 'php\.error_log=')" ]]; then + go-replace --mode=lineinfile --regex \ + -s '^[\s;]*php_admin_value\[error_log\][\s]*=' -r "php_admin_value[error_log] = $(envGetValue php.error_log)" \ + -- /opt/docker/etc/php/fpm/pool.d/application.conf +fi + # Workaround for official PHP images if [[ -n "${PHP_SENDMAIL_PATH+x}" ]]; then echo "php_admin_value[sendmail_path] = ${PHP_SENDMAIL_PATH}" >> /opt/docker/etc/php/fpm/pool.d/application.conf diff --git a/docs/environment-variables.md b/docs/environment-variables.md index d8cf559c1..b3f33de05 100644 --- a/docs/environment-variables.md +++ b/docs/environment-variables.md @@ -36,7 +36,7 @@ Images: `base`, `base-app`, `apache`, `nginx`, `php-*`. | --- | --- | --- | | `CLI_SCRIPT` | not set | Command the `cli` entrypoint runs as `APPLICATION_USER`. Works in `base-app` and `php-*` only; in the other images `cli` fails because `APPLICATION_USER` is not set. See [Customization](customization.md#commands). | | `LOG_STDOUT` | container stdout | File that `/docker.stdout` points to. Nginx and Apache write their access logs there, syslog-ng the messages of the services, for example of cron and Postfix. PHP-FPM does not, see [Known issues](known-issues.md#log_stdout-and-log_stderr-for-php-fpm). | -| `LOG_STDERR` | container stderr | File that `/docker.stderr` points to. Nginx and Apache write their error logs there. | +| `LOG_STDERR` | container stderr | File that `/docker.stderr` points to. Nginx and Apache write their error logs there. The errors of PHP scripts end up there only in `php` and `php-dev`, see [Known issues](known-issues.md#log_stdout-and-log_stderr-for-php-fpm). | | `TZ` | not set (UTC) | Time zone of the container, for example `Europe/Berlin`. It applies to `date`, cron and the time stamps supervisord writes to the container log. PHP uses `PHP_DATE_TIMEZONE` instead, see [Time zone](customization.md#time-zone). | ## Web server diff --git a/docs/known-issues.md b/docs/known-issues.md index b7997aafa..879fba885 100644 --- a/docs/known-issues.md +++ b/docs/known-issues.md @@ -18,9 +18,9 @@ These options and tools are part of the images but do not work, or work only in ### `LOG_STDOUT` and `LOG_STDERR` for PHP-FPM -- **Effect:** PHP-FPM writes its access log, its own messages and the errors of PHP scripts to the stderr of the container, not to `/docker.stdout` and `/docker.stderr`, so `LOG_STDOUT` and `LOG_STDERR` do not apply to it. The `docker.conf` of the official PHP image sets `error_log` and `access.log` to `/proc/self/fd/2` after the configuration of the image. -- **Images:** `php-*`, Debian and Alpine. -- **Instead:** Read them with `docker compose logs`. To have PHP-FPM's messages and the PHP errors in the file of `LOG_STDERR`, also set `fpm.global.error_log: /docker.stderr`. The access log needs a pool file that sorts after `docker.conf`, see [`fpm.pool.`](#fpmpoolkey-for-the-directives-of-dockerconf). +- **Effect:** PHP-FPM writes its access log and its own messages to the stderr of the container, not to `/docker.stdout` and `/docker.stderr`, so `LOG_STDOUT` and `LOG_STDERR` do not apply to them. The `docker.conf` of the official PHP image sets `error_log` and `access.log` to `/proc/self/fd/2` after the configuration of the image. The errors of PHP scripts go to `/docker.stderr`, but the workers of PHP-FPM run as `application` and can only write to a file that they may open. In `php` and `php-dev`, they create the file of `LOG_STDERR` and write the errors there. In the images with Nginx or Apache, the web server creates that file as root first, so the errors go to the stderr of the container, as they do in every image without `LOG_STDERR`. +- **Images:** `php`, `php-dev`, `php-nginx`, `php-nginx-dev`, `php-apache` and `php-apache-dev`, Debian and Alpine. +- **Instead:** Read them with `docker compose logs`. To have PHP-FPM's messages and the PHP errors in the file of `LOG_STDERR` in every image, also set `fpm.global.error_log: /docker.stderr`. `php.error_log` gives the PHP errors of PHP-FPM and the CLI a file of their own, in a directory that `application` may write to, for example `/app`. The access log needs a pool file that sorts after `docker.conf`, see [`fpm.pool.`](#fpmpoolkey-for-the-directives-of-dockerconf). ### No `/app` in `nginx` and `apache` 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 5ab722cf7..0bcd0b8d1 100644 --- a/provisioning/php/general/provision/entrypoint.d/20-php-fpm.sh +++ b/provisioning/php/general/provision/entrypoint.d/20-php-fpm.sh @@ -74,6 +74,15 @@ if [[ -n "${FPM_RLIMIT_CORE+x}" ]]; then echo "rlimit_core = ${FPM_RLIMIT_CORE}" >> /opt/docker/etc/php/fpm/pool.d/application.conf fi +# The pool logs the errors of PHP scripts to /docker.stderr with php_admin_value, which a php.ini value cannot override, +# so php.error_log replaces it here. PHP-FPM takes the first of two php_admin_value lines, so a line added below would +# not take effect. +if [[ -n "$(envListVars 'php\.error_log=')" ]]; then + go-replace --mode=lineinfile --regex \ + -s '^[\s;]*php_admin_value\[error_log\][\s]*=' -r "php_admin_value[error_log] = $(envGetValue php.error_log)" \ + -- /opt/docker/etc/php/fpm/pool.d/application.conf +fi + # Workaround for official PHP images if [[ -n "${PHP_SENDMAIL_PATH+x}" ]]; then echo "php_admin_value[sendmail_path] = ${PHP_SENDMAIL_PATH}" >> /opt/docker/etc/php/fpm/pool.d/application.conf