We use a typical nginx and php-fpm setup for a WordPress site running on Amazon Linux 2.
We recently noticed that:
- PHP error log output was being sent to
/var/log/nginx/error_log
even though/etc/php-fpm.d/www.conf
containsphp_admin_value[error_log] = /var/log/php-fpm/www-error.log
andphp_admin_flag[log_errors] = on
- It turned out that
/var/log/php-fpm
was not writable becauseuser = nginx
andgroup = nginx
in/etc/php-fpm.d/www.conf
and/var/log/php-fpm
is owned byapache:root
- After we changed the permission, PHP error log is written to
/var/log/php-fpm/www-error.log
Now we have 2 questions.
- Is this expected behavior? When the php-fpm log file/directory is not writable, it sends logs to nginx log? We searched the documentation that explains how that is possible but couldn’t find any.
- While the permission was misconfigured, some page of the WordPress didn’t work correctly. Can the error log settings affect PHP’s behavior?
Unfortunately, the WordPress problem was occurring only on the production website, so we can’t investigate the problem by trial and error. We need to make it clear theoretically.
2
Answers
In Nginx by default when a fastcgi application sends an error message, nginx sends that error message to its own error log. I guess PHP has a fallback behavior where if it cannot write to the error log file, it complains to the fastcgi server (in this case, nginx) instead of the log. Sounds reasonable to me
It shouldn’t.. I can imagine code written like:
though, but I’ve never seen that in the wild (and i’ve been doing PHP since 2006)
No, it is not the expected behavior for PHP-FPM to send its error logs to the Nginx log file when the specified log file or directory is not writable. By default, PHP-FPM should log its errors to the location specified in the
php_admin_value[error_log]
directive in thewww.conf
file (which in your case has been set to/var/log/php-fpm/www-error.log
).However, the behavior you observed could be a result of misconfiguration or a fallback mechanism in the particular setup you have. If PHP-FPM encounters an issue with writing to the specified log file or directory, it may fall back to sending the error logs to the Nginx error log instead.
Not at all! Logging has nothing to with page rendering process (except, of course, when incorrect logging settings cause the content of the error message to be displayed in the output). But if your pages had some template rendering issues, that has nothing to do with logging misconfiguration.
In cases like this, you may also consider switching the PHP handler and/or PHP version to see if the problem resolves.