PHP Logo

PHP FPM: Log errors to stderr (for Docker)

Posted by

When running php-fpm in a Docker container, it is often desirable to log errors to stderr such that they can be picked up by your docker logging driver and sent to wherever your logs normally go. This requires a few different bits of configuration (config below is for php7.3 and above):

Under the [global] section of your php-fpm.conf you need to add the following:

error_log = /proc/self/fd/2
log_level = error
log_buffering = no

This sends logs to stderr (fd 2) and sets the log level to only log error and above. You can set log_level to another value as required.

Under the section for your pool (e.g. [www] in pool.d/www.conf) you need to add the following:

catch_workers_output = yes
decorate_workers_output = no
php_flag[display_errors] = off
php_admin_flag[log_errors] = on
php_admin_flag[fastcgi.logging] = off

catch_workers_output directs the stdout of your child worker processes back to the stdout of the parent process
decorate_workers_output causes errors to be logged as is and not wrapped in php-fpm process information
display_errors stops errors being displayed to your users
log_errors turns on logging of errors (to stdout)
fastcgi.logging stops php-fpm sending errors, back to Nginx, which cause Nginx to send 5xx responses to requests. This is especially important if you want to log info/warning as any logs will cause Nginx to return 5xx responses without this setting

Leave a Reply

Your email address will not be published. Required fields are marked *