简体   繁体   English

PHP-FPM 不写入错误日志

[英]PHP-FPM doesn't write to error log

I've just installed a nginx+php-fpm server.我刚刚安装了一个 nginx+php-fpm 服务器。 Everything seems fine except that PHP-FPM never writes error to its log.除了 PHP-FPM 从不将错误写入其日志外,一切似乎都很好。

fpm.conf配置文件

[default]
listen = /var/run/php-fpm/default.sock
listen.allowed_clients = 127.0.0.1
listen.owner = webusr
listen.group = webusr
listen.mode = 0666
user = webusr
group = webusr
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.status_path = /php/fpm/status
ping.path = /php/fpm/ping
request_terminate_timeout = 30s
request_slowlog_timeout = 10s
slowlog = /var/log/php-fpm/default/slow.log
chroot = /var/www/sites/webusr
catch_workers_output = yes
env[HOSTNAME] = mapsvr.mapking.com
php_flag[display_errors] = on
php_admin_value[error_log] = /var/log/php-fpm/default/error.log
php_admin_flag[log_errors] = on

nginx.conf nginx.conf

server
{
  listen        80 default_server;
  server_name   _;

  charset       utf-8;
  access_log    /var/log/nginx/access.log rest;

  include       conf.d/drops.conf.inc;

  location      /
  {
    root        /var/www/sites/webusr/htdocs;
    index       index.html index.htm index.php;
  }

  # pass the PHP scripts to FastCGI server listening on socket
  #
  location      ~ \.php$
  {
    root           /var/www/sites/webusr/htdocs;
    include        /etc/nginx/fastcgi_params;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME /htdocs/$fastcgi_script_name;
    if (-f $request_filename)
    {
      fastcgi_pass   unix:/var/run/php-fpm/default.sock;
    }
  }

  location      = /php/fpm/status
  {
    include        /etc/nginx/fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass   unix:/var/run/php-fpm/default.sock;
  }

  location      = /php/fpm/ping
  {
    include        /etc/nginx/fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass   unix:/var/run/php-fpm/default.sock;
  }

  # redirect server error pages to the static page /50x.html
  #
  error_page    500 502 503 504  /50x.html;
  location      = /50x.html
  {
    root        /usr/share/nginx/html;
  }
}

I've made an erroneous php script and run, and see error output on the web browser.我制作了一个错误的 php 脚本并运行,在 web 浏览器上看到错误 output。 Also nginx error log states stderr output from fpm with the same message.另外 nginx 错误日志指出来自 fpm 的 stderr output 具有相同的消息。 I've check that the user have write (I've even tried 777) permission to the appointed log folder.我检查过用户是否对指定的日志文件夹具有写入权限(我什至尝试过 777)。 Even the appointed error.log file has be created successfully by php-fpm.连指定的error.log文件也被php-fpm成功创建了。 However, the log file is always empty, no matter what outrageous error has been made from php script.但是,无论 php 脚本中出现了多么离谱的错误,日志文件始终为空。

What's going on?这是怎么回事?

[Found the reason quite a while later] 【好久才找到原因】

It was permission.这是许可。 Changed the owner to the sites's users solved the problem.将所有者更改为网站的用户解决了问题。

This worked for me:这对我有用:

; Redirect worker stdout and stderr into main error log. If not set, stdout and
; stderr will be redirected to /dev/null according to FastCGI specs.
; Default Value: no
catch_workers_output = yes

Edit:编辑:

The file to edit is the file that configure your desired pool.要编辑的文件是配置所需池的文件。 By default its: /etc/php-fpm.d/www.conf默认情况下:/etc/php-fpm.d/www.conf

I struggled with this for a long time before finding my php-fpm logs were being written to /var/log/upstart/php5-fpm.log .在发现我的 php-fpm 日志被写入/var/log/upstart/php5-fpm.log之前,我为此苦苦挣扎了很长时间。 It appears to be a bug between how upstart and php-fpm interact.这似乎是 upstart 和 php-fpm 如何交互之间的错误。 See more here: https://bugs.launchpad.net/ubuntu/+source/php5/+bug/1319595在此处查看更多信息: https : //bugs.launchpad.net/ubuntu/+source/php5/+bug/1319595

I had a similar issue and had to do the following to the pool.d/www.conf file我遇到了类似的问题,必须对pool.d/www.conf文件执行以下pool.d/www.conf

php_admin_value[error_log] = /var/log/fpm-php.www.log
php_admin_flag[log_errors] = on

It still wasn't writing the log file so I actually had to create it by touch /var/log/fpm-php.www.log then setting the correct owner sudo chown www-data:www-data /var/log/fpm-php.www.log .它仍然没有写入日志文件,所以我实际上必须通过touch /var/log/fpm-php.www.log创建它,然后设置正确的所有者sudo chown www-data:www-data /var/log/fpm-php.www.log

Once this was done, and php5-fpm restarted, logging was resumed.完成后,php5-fpm 重新启动,日志记录恢复。

There are multiple php config files, but THIS is the one you need to edit:有多个 php 配置文件,但这是您需要编辑的一个:

/etc/php(version)?/fpm/pool.d/www.conf

uncomment the line that says:取消注释以下行:

catch_workers_output

That will allow PHPs stderr to go to php-fpm's error log instead of /dev/null.这将允许 PHP 的 stderr 转到 php-fpm 的错误日志而不是 /dev/null。

I gathered insights from a bunch of answers here and I present a comprehensive solution:我从这里的一堆答案中收集了见解,并提出了一个全面的解决方案:

So, if you setup nginx with php5-fpm and log a message using error_log() you can see it in /var/log/nginx/error.log by default.因此,如果您使用 php5-fpm 设置 nginx 并使用error_log()记录消息,则默认情况下您可以在/var/log/nginx/error.log看到它。

A problem can arise if you want to log a lot of data (say an array) using error_log(print_r($myArr, true));如果您想使用error_log(print_r($myArr, true));记录大量数据(例如数组),则会出现问题error_log(print_r($myArr, true)); . . If an array is large enough, it seems that nginx will truncate your log entry.如果数组足够大, nginx似乎会截断您的日志条目。

To get around this you can configure fpm ( php.net fpm config ) to manage logs.为了解决这个问题,您可以配置fpm ( php.net fpm config ) 来管理日志。 Here are the steps to do so.以下是执行此操作的步骤。

  1. Open /etc/php5/fpm/pool.d/www.conf :打开/etc/php5/fpm/pool.d/www.conf

    $ sudo nano /etc/php5/fpm/pool.d/www.conf

  2. Uncomment the following two lines by removing ;通过删除取消注释以下两行; at the beginning of the line: (error_log is defined here: php.net )在行的开头:(error_log 在这里定义: php.net

    ;php_admin_value[error_log] = /var/log/fpm-php.www.log ;php_admin_flag[log_errors] = on

  3. Create /var/log/fpm-php.www.log :创建/var/log/fpm-php.www.log

    $ sudo touch /var/log/fpm-php.www.log;

  4. Change ownership of /var/log/fpm-php.www.log so that php5-fpm can edit it:更改/var/log/fpm-php.www.log所有权,以便 php5-fpm 可以编辑它:

    $ sudo chown vagrant /var/log/fpm-php.www.log

    Note: vagrant is the user that I need to give ownership to.注意: vagrant是我需要授予所有权的用户。 You can see what user this should be for you by running $ ps aux | grep php.*www您可以通过运行$ ps aux | grep php.*www来查看这应该是哪个用户$ ps aux | grep php.*www $ ps aux | grep php.*www and looking at first column. $ ps aux | grep php.*www并查看第一列。

  5. Restart php5-fpm:重启 php5-fpm:

    $ sudo service php5-fpm restart

Now your logs will be in /var/log/fpm-php.www.log .现在您的日志将在/var/log/fpm-php.www.log

There is a bug https://bugs.php.net/bug.php?id=61045 in php-fpm from v5.3.9 and till now (5.3.14 and 5.4.4).从 v5.3.9 到现在(5.3.14 和 5.4.4),php-fpm 中有一个错误https://bugs.php.net/bug.php?id=61045 Developer promised fix will go live in next release.开发人员承诺的修复将在下一个版本中上线。 If you don't want to wait - use patch on that page and re-build or rollback to 5.3.8.如果您不想等待 - 在该页面上使用补丁并重新构建或回滚到 5.3.8。

In your fpm.conf file you haven't set 2 variable which are only for error logging.在您的 fpm.conf 文件中,您没有设置 2 个仅用于错误记录的变量。

The variables are error_log (file path of your error log file) and log_level (error logging level).变量是error_log (错误日志文件的文件路径)和log_level (错误日志级别)。

; Error log file
; Note: the default prefix is /usr/local/php/var
; Default Value: log/php-fpm.log

error_log = log/php-fpm.log

; Log level
; Possible Values: alert, error, warning, notice, debug
; Default Value: notice

log_level = notice

I'd like to add another tip to the existing answers because they did not solve my problem.我想在现有答案中添加另一个提示,因为它们没有解决我的问题。

Watch out for the following nginx directive in your php location block:注意 php 位置块中的以下 nginx 指令:

fastcgi_intercept_errors on;

Removing this line has brought an end to many hours of struggling and pulling hair.去除这条线结束了许多小时的挣扎和拉头发。

It could be hidden in some included conf directory like /etc/nginx/default.d/php.conf in my fedora.它可能隐藏在一些包含的 conf 目录中,例如我的 Fedora 中的/etc/nginx/default.d/php.conf

in my case I show that the error log was going to /var/log/php-fpm/www-error.log .就我而言,我显示错误日志将转到/var/log/php-fpm/www-error.log so I commented this line in /etc/php-fpm.d/www.conf所以我在 /etc/php-fpm.d/www.conf 中评论了这一行

php_flag[display_errors]   is commented
php_flag[display_errors] = on  log will be at /var/log/php-fpm/www-error.log

and as said above I also uncommented this line如上所述,我也取消了这一行的注释

catch_workers_output = yes

Now I can see logs in the file specified by nginx.现在我可以在nginx指定的文件中看到日志了。

On alpine 3.15 with php8 i found on /var/log/php8/error.log在带有 php8 的 alpine 3.15 上,我在 /var/log/php8/error.log 上找到了

/var/log/php8 # cat error.log
 16:10:52] NOTICE: fpm is running, pid 14
 16:10:52] NOTICE: ready to handle connections

i also have this:我也有这个:

catch_workers_output = yes

In my case php-fpm outputs 500 error without any logging because of missing php-mysql module.在我的例子中,由于缺少 php-mysql 模块,php-fpm 输出 500 错误而没有任何日志记录。 I moved joomla installation to another server and forgot about it.我将 joomla 安装移动到另一台服务器并忘记了它。 So apt-get install php-mysql and service restart solved it.所以apt-get install php-mysql和 service restart 解决了它。

I started with trying to fix broken logging without success.我开始尝试修复损坏的日志记录,但没有成功。 Finally with strace i found fail message after db-related system calls.最后使用strace我在与数据库相关的系统调用之后发现了失败消息。 Though my case is not directly related to op's question, I hope it could be useful.虽然我的案例与 op 的问题没有直接关系,但我希望它可能有用。

Check the Owner directory of "PHP-FPM"检查“PHP-FPM”的所有者目录

You can do:你可以做:

ls -lah /var/log/php-fpm/
chown -R webusr:webusr /var/log/php-fpm/
chmod -R 777 /var/log/php-fpm/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM