PHP-FPM 不写入错误日志

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8677493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:14:55  来源:igfitidea点击:

PHP-FPM doesn't write to error log

php

提问by eidng8

I've just installed a nginx+php-fpm server. Everything seems fine except that PHP-FPM never writes error to its log.

我刚刚安装了一个 nginx+php-fpm 服务器。一切似乎都很好,除了 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

配置文件

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. Also nginx error log states stderr output from fpm with the same message. I've check that the user have write (I've even tried 777) permission to the appointed log folder. Even the appointed error.log file has be created successfully by php-fpm. However, the log file is always empty, no matter what outrageous error has been made from php script.

我编写了一个错误的 php 脚本并运行,并在 Web 浏览器上看到错误输出。nginx 错误日志还使用相同的消息声明 fpm 的 stderr 输出。我已经检查过用户是否对指定的日志文件夹具有写入(我什至尝试过 777)权限。甚至指定的error.log 文件也被php-fpm 创建成功。但是,日志文件始终为空,无论 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.

这是许可。将所有者更改为站点的用户解决了问题。

回答by michaelbn

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

回答by Code Commander

I struggled with this for a long time before finding my php-fpm logs were being written to /var/log/upstart/php5-fpm.log. It appears to be a bug between how upstart and php-fpm interact. See more here: https://bugs.launchpad.net/ubuntu/+source/php5/+bug/1319595

在发现我的 php-fpm 日志被写入/var/log/upstart/php5-fpm.log. 这似乎是 upstart 和 php-fpm 如何交互之间的错误。在此处查看更多信息:https: //bugs.launchpad.net/ubuntu/+source/php5/+bug/1319595

回答by adnans

I had a similar issue and had to do the following to the pool.d/www.conffile

我遇到了类似的问题,必须对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.logthen setting the correct owner sudo chown www-data:www-data /var/log/fpm-php.www.log.

它仍然没有写入日志文件,所以我实际上必须通过touch /var/log/fpm-php.www.log设置正确的 owner来创建它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 重新启动,日志记录恢复。

回答by vector

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。

回答by Gezim

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.logby 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));. If an array is large enough, it seems that nginxwill truncate your log entry.

如果您想使用 .log 记录大量数据(例如数组),则可能会出现问题error_log(print_r($myArr, true));。如果数组足够大,似乎nginx会截断您的日志条目。

To get around this you can configure fpm(php.net fpm config) to manage logs. Here are the steps to do so.

为了解决这个问题,您可以配置fpmphp.net fpm config)来管理日志。以下是执行此操作的步骤。

  1. Open /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)

    ;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:

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

  4. Change ownership of /var/log/fpm-php.www.logso that php5-fpm can edit it:

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

    Note: vagrantis the user that I need to give ownership to. You can see what user this should be for you by running $ ps aux | grep php.*wwwand looking at first column.

  5. Restart php5-fpm:

    $ sudo service php5-fpm restart

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

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

  2. 通过删除行首来取消注释以下两;行:(error_log 在此处定义:php.net

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

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

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

  4. 更改所有权/var/log/fpm-php.www.log以便 php5-fpm 可以编辑它:

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

    注意:vagrant是我需要授予所有权的用户。您可以通过运行$ ps aux | grep php.*www和查看第一列来了解这应该是什么用户。

  5. 重启 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.

回答by Drewx

There is a bug https://bugs.php.net/bug.php?id=61045in php-fpm from v5.3.9 and till now (5.3.14 and 5.4.4). 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.

从 v5.3.9 到现在(5.3.14 和 5.4.4),php-fpm 中有一个错误https://bugs.php.net/bug.php?id=61045。开发人员承诺的修复将在下一个版本中上线。如果您不想等待 - 在该页面上使用补丁并重新构建或回滚到 5.3.8。

回答by khizar ansari

in your fpm.conf file you haven't set 2 variable which is only for error logging

在您的 fpm.conf 文件中,您没有设置 2 个仅用于错误记录的变量

this variables are error_log (file path of your error log file) 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

; 错误日志文件;注意:默认前缀是 /usr/local/php/var ;默认值: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

回答by enRaiser

in my case I show that the error log was going to /var/log/php-fpm/www-error.log. so I commented this line in /etc/php-fpm.d/www.conf

在我的情况下,我显示错误日志将转到/var/log/php-fpm/www-error.log。所以我在 /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指定的文件中看到日志了。

回答by Arsylum

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.confin my fedora.

它可能隐藏在某些包含的 conf 目录中,例如/etc/nginx/default.d/php.conf在我的 Fedora 中。

回答by user3132194

In my case php-fpm outputs 500 error without any logging because of missing php-mysql module. I moved joomla installation to another server and forgot about it. So apt-get install php-mysqland service restart solved it.

在我的例子中,由于缺少 php-mysql 模块,php-fpm 输出 500 错误而没有任何日志记录。我将 joomla 安装移动到另一台服务器并忘记了它。因此apt-get install php-mysql,服务重启解决了它。

I started with trying to fix broken logging without success. Finally with stracei found fail message after db-related system calls. Though my case is not directly related to op's question, I hope it could be useful.

我开始尝试修复损坏的日志记录,但没有成功。最后,strace我在与数据库相关的系统调用后发现了失败消息。虽然我的案例与 op 的问题没有直接关系,但我希望它可能有用。