[英]PHP's configuration setting 'error_log' is not working
我在这里做错了什么?
我在 .ini 文件中设置了 error_log 以及error_reporting = E_ALL | E_STRICT
error_reporting = E_ALL | E_STRICT
。
我还缺少什么? 这通常给了我。 我希望在 .ini 文件中设置此设置,而不是在我的脚本中设置。
正在发生的另一件有趣的事情是,当我故意尝试在我的一个脚本中抛出错误时,Apache 一遍又一遍地重新启动。
这是我在一次错误后的事件日志。 查看时间戳。
Wed Nov 04 19:34:23 2009] [notice] Apache/2.2.14 (Win32) PHP/5.3.0 configured -- resuming normal operations
[Wed Nov 04 19:34:23 2009] [notice] Server built: Sep 28 2009 22:41:08
[Wed Nov 04 19:34:23 2009] [notice] Parent: Created child process 1700
[Wed Nov 04 19:34:23 2009] [notice] Child 1700: Child process is running
[Wed Nov 04 19:34:23 2009] [notice] Child 3008: Released the start mutex
[Wed Nov 04 19:34:23 2009] [notice] Child 1700: Acquired the start mutex.
[Wed Nov 04 19:34:23 2009] [notice] Child 1700: Starting 64 worker threads.
[Wed Nov 04 19:34:23 2009] [notice] Child 1700: Starting thread to listen on port 80.
[Wed Nov 04 19:34:24 2009] [notice] Child 3008: All worker threads have exited.
[Wed Nov 04 19:34:24 2009] [notice] Child 3008: Child process is exiting
[Wed Nov 04 19:34:53 2009] [notice] Parent: child process exited with status 128 -- Restarting.
[Wed Nov 04 19:34:53 2009] [notice] Apache/2.2.14 (Win32) PHP/5.3.0 configured -- resuming normal operations
[Wed Nov 04 19:34:53 2009] [notice] Server built: Sep 28 2009 22:41:08
[Wed Nov 04 19:34:53 2009] [notice] Parent: Created child process 3656
[Wed Nov 04 19:34:53 2009] [notice] Child 3656: Child process is running
[Wed Nov 04 19:34:53 2009] [notice] Child 3656: Acquired the start mutex.
[Wed Nov 04 19:34:53 2009] [notice] Child 3656: Starting 64 worker threads.
[Wed Nov 04 19:34:53 2009] [notice] Child 3656: Starting thread to listen on port 80.
[Wed Nov 04 19:34:53 2009] [notice] Parent: child process exited with status 128 -- Restarting.
[Wed Nov 04 19:34:54 2009] [notice] Apache/2.2.14 (Win32) PHP/5.3.0 configured -- resuming normal operations
[Wed Nov 04 19:34:54 2009] [notice] Server built: Sep 28 2009 22:41:08
[Wed Nov 04 19:34:54 2009] [notice] Parent: Created child process 3980
[Wed Nov 04 19:34:54 2009] [notice] Child 3980: Child process is running
[Wed Nov 04 19:34:54 2009] [notice] Child 3980: Acquired the start mutex.
[Wed Nov 04 19:34:54 2009] [notice] Child 3980: Starting 64 worker threads.
[Wed Nov 04 19:34:54 2009] [notice] Child 3980: Starting thread to listen on port 80.
[Wed Nov 04 19:34:54 2009] [notice] Parent: child process exited with status 128 -- Restarting.
[Wed Nov 04 19:34:54 2009] [notice] Apache/2.2.14 (Win32) PHP/5.3.0 configured -- resuming normal operations
[Wed Nov 04 19:34:54 2009] [notice] Server built: Sep 28 2009 22:41:08
[Wed Nov 04 19:34:54 2009] [notice] Parent: Created child process 1600
[Wed Nov 04 19:34:54 2009] [notice] Child 1600: Child process is running
[Wed Nov 04 19:34:54 2009] [notice] Child 1600: Acquired the start mutex.
[Wed Nov 04 19:34:54 2009] [notice] Child 1600: Starting 64 worker threads.
[Wed Nov 04 19:34:54 2009] [notice] Child 1600: Starting thread to listen on port 80.
[Wed Nov 04 19:34:55 2009] [notice] Parent: child process exited with status 128 -- Restarting.
[Wed Nov 04 19:34:55 2009] [notice] Apache/2.2.14 (Win32) PHP/5.3.0 configured -- resuming normal operations
[Wed Nov 04 19:34:55 2009] [notice] Server built: Sep 28 2009 22:41:08
[Wed Nov 04 19:34:55 2009] [notice] Parent: Created child process 1068
[Wed Nov 04 19:34:55 2009] [notice] Child 1068: Child process is running
[Wed Nov 04 19:34:55 2009] [notice] Child 1068: Acquired the start mutex.
[Wed Nov 04 19:34:55 2009] [notice] Child 1068: Starting 64 worker threads.
[Wed Nov 04 19:34:55 2009] [notice] Child 1068: Starting thread to listen on port 80.
[Wed Nov 04 19:34:55 2009] [notice] Parent: child process exited with status 128 -- Restarting.
[Wed Nov 04 19:34:55 2009] [notice] Apache/2.2.14 (Win32) PHP/5.3.0 configured -- resuming normal operations
[Wed Nov 04 19:34:55 2009] [notice] Server built: Sep 28 2009 22:41:08
[Wed Nov 04 19:34:55 2009] [notice] Parent: Created child process 3220
[Wed Nov 04 19:34:56 2009] [notice] Child 3220: Child process is running
[Wed Nov 04 19:34:56 2009] [notice] Child 3220: Acquired the start mutex.
[Wed Nov 04 19:34:56 2009] [notice] Child 3220: Starting 64 worker threads.
[Wed Nov 04 19:34:56 2009] [notice] Child 3220: Starting thread to listen on port 80.
这已被问过 1000 次,我在发布之前浏览了不同的帖子,但我没有找到答案。 只要我一直在用 PHP 编程,这一直是开始工作的噩梦。
error_log
: 确保文件/etc/php-fpm.d/www.conf
不包含 error_log 的php_admin_value
设置。 搜索以下内容并使用分号将其注释掉:
; NOTE: If these are set, ini_set('error_log', 'path') will have no effect
; inside your php code, and this will be forced to be the value always.
; php_admin_value[error_log] = /var/log/php-fpm/www-error.log
; php_admin_flag[log_errors] = on
然后重启php-fpm:
systemctl restart php-fpm
error_log
值 apache 配置文件中的php_admin_value
设置不能被覆盖,因此请确保 Apache 配置文件中的error_log
设置没有任何 php_admin_value。 还要检查php_value
设置以防万一。
您还需要在 php.ini 中设置log_errors = On
。
如果其他人在让他们的本地开发环境记录错误时遇到问题,以下是为我解决的问题:
在 Windows 上, error_log
必须设置为日志的完整路径才能使error_log()
工作( error_log = c:\\apache\\php_errors.log
)。 但是,如果error_log = php_errors.log
没有路径,php 仍然可以记录启动错误,例如
PHP Startup: Unable to load dynamic library 'ext\php_mysqli.dll' - The specified module could not be found
我遇到的问题是我指定的错误日志是写保护的。 我所有的 .htaccess 设置都是正确的,PHP 只是无法写入错误日志,因为它没有权限。 这对我来说是正确的:
chmod 777 watermellon-app-errors.log
显然,您将希望将 .log 更改为您用于日志的任何文件。
这是我对error_log()
调用不起作用的故障排除指南。
查看服务器的配置以找出默认错误日志文件的位置。 这取决于您使用的服务器。 为了让您开始,如果您使用的是 Apache,请查看 Apache 的ErrorLog
选项,如果您使用的是 Nginx,请查看 Nginx 的error_log
选项。 确保将其设置为文件。 如果您使用的是 Valet 之类的工具,请注意它在幕后使用了 Nginx 之类的服务器软件。
检查服务器错误日志文件的权限。 在类 Unix 系统上,它应该可由正确的用户和组写入,并且父目录及其所有祖先的权限也需要正确。 使用chmod
和chown
。
检查.ini
文件中 PHP的配置。 具体来说,检查log_errors = On
和error_reporting = E_ALL | E_STRICT
error_reporting = E_ALL | E_STRICT
和error_log = /tmp/example/php_errors.log
(请参阅文档以了解log_errors
、 error_reporting
和error_log
配置设置)。 要找到.ini
文件,请查看phpinfo();
的输出phpinfo();
. 如果未设置error_log
,默认情况下它会转到服务器的错误日志,在前面的步骤中提到过。 如果error_log
被设置为一个文件,它应该已经存在并且是可写的,就像在前面的步骤中一样。 请记住在配置更改后重新启动服务器。
检查PHP 的设置没有被服务器配置更改。 您服务器的配置(甚至.htaccess
)可以更改 PHP 配置设置。 在 Apache 中,这是使用php_admin_value
和php_admin_flag
( docs ) 完成的。 例如,您可能会在.htaccess
文件中找到这一行: php_admin_flag[log_errors] = off
。 请记住在配置更改后重新启动服务器。 此时,您应该能够创建一个测试文件test.php
,其内容为<?php error_log("test");
,重新启动服务器,并在浏览器中打开 URL,您应该能够在错误日志中看到test
(服务器的,或由error_log =
指定的日志)。 但继续阅读。
检查PHP 的设置在运行时没有被更改。 可以在运行时通过运行ini_set('log_errors', 1);
更改log_errors
选项ini_set('log_errors', 1);
,其他配置选项error_reporting
和error_log
。 另请注意,有一个特殊的error_reporting()
PHP 函数可在运行时更改配置。 在您的代码库中搜索任何对ini_set
或error_reporting
调用。 例如,WordPress 确实根据WP_DEBUG
的值运行这些。
其他需要注意的事项:您可能在 SELinux 中遇到权限问题(请参阅此答案)。
如果设置了 error_log 指令,该文件将用于记录 php 错误,如果未设置,则错误将记录到 Apache 日志中。 看看http://us3.php.net/manual/en/errorfunc.configuration.php#ini.error-log 。
error_log 文件及其所在的目录必须可由运行 Apache 的用户写入。 如果未创建文件,则可能是由于权限问题。
我不确定为什么 Apache 会在你身上崩溃,但我猜这是某种权限问题。
如果您使用的是 Fedora,SELinux(默认启用)将阻止 apache/httpd 将错误附加到您的日志文件中,即使您的文件在 php.ini 中指定并且其包含的目录具有所有权限。
您可以通过查看/var/log/messages 中的系统日志文件来查看是否发生这种情况
理想的解决方案是配置 SELinux 以允许访问日志文件。
更快的解决方案是通过将 SELINUX 设置为禁用来禁用/etc/selinux/config 中的 SELinux。
执行此操作后,您需要重新启动系统以使更改生效。
我不明白为什么,但错误日志现在正在工作。 这是我所做的。 我放弃并注释掉 error_log 指令并关闭了 ini 文件。 我运行带有解析错误的脚本以查看 Apache 仍然会崩溃,并且我在日志文件中收到了 PHP 错误。 这很奇怪,因为 ini 文件不再启用 error_log 并且我的脚本没有使用 ini_set()。
有没有人对这种疯狂有任何解释? 此外,Apache 不再崩溃。
就我而言,在 CentOS 开发服务器上,在完整的yum update
后, /var/log/http上的权限更改为 700 并且用户更改为“root”,因此用户“apache”无法输入或写入进去。 它仍然能够写入现有文件/var/log/httpd/error.log但它无法创建新文件,因为我使用日期后缀的日志文件。 发出命令
chown apache /var/log/httpd
解决了这个问题。
对我来说,手动创建文件/var/log/php_error.log解决了这个问题:
在文件php.ini中,配置日志到文件
log_errors = On error_log = /var/log/php_error.log
重启 Apache
sudo systemctl restart apache2
创建日志文件,例如:
sudo touch /var/log/php_error.log
赋予正确的权限(所有者必须是 Apache)
sudo chown www-data:root /var/log/php_error.log
(如果尚未设置)
sudo chmod 0644 /var/log/php_error.log
以这种方式测试,例如:
sudo vim /var/www/.../index.php
<html> <body> PHP file: <?php echo "Hello, World;", trigger_error("User error in PHP"; E_USER_ERROR)? ?> </body> </html>
检查错误是否写入
sudo less /var/log/php_error.log
它在我的 ubuntu(Apache 2.4.7,PHP 5.5.9)上的工作方式如下:
命令行脚本:
log_errors = On
则将日志写入error_log
指向的路径。 /etc/php5/cli/php.ini
设置;通过 apache 的网络请求:
log_errors = On
( /etc/php5/apache2/php.ini
),则错误附加到虚拟主机中ErrorLog
apache 指令指向的路径中。 如果该指令不存在,则使用 php.ini error_log
路径;log_errors = Off
不将日志写入任何地方;据我所知,它在大多数 linux 堆栈中几乎都是这样工作的
正如 Bradym 所说,检查您是否对 apache 用户的 php 错误日志所在的目录具有写入权限。 如果您创建了一个具有写入权限的日志文件,这还不够,目录也应该有它们。
进一步调试: phpinfo();
显示当前的 error_log 设置在 vhost 未记录时很有用。
在该设置从 php.ini 和 vhost 被覆盖后结束。 最终登录到系统日志。
有同样的问题。 通过这样做修复它:
$ chmod 0777 -R /var/log/apache2
$ apachectl restart
我认为 PHP 以用户www-data
运行,而对/var/log/apache2
访问仅限于root
。 所以,这解决了它。
error_log = "C:\\php\\Log\\error.log" 对我也不起作用。 我的解决方案是你不应该自己创建 error.log,因为 PHP 会为你做。 请参阅 PHP 留言板。 我在 Windows 2008 服务器上使用 PHP 5.2
这对我有用。
setsebool -P httpd_unified 1
请注意,这将是以下首选方法:
semanage fcontext -a -t httpd_sys_rw_content_t 'errorLogNameHere.error.log
restorecon -v 'errorLogNameHere.error.log'
此答案来自执行此命令行的日志:
journalctl -xe
有关我运行的系统的更多信息:PHP 7.0 和 CentOS 7
不确定这是否不明显,但问题是 Apache 写入文件的配置。 我确实在日志目录上尝试了 chmod 777、chmod a+w,但这对我不起作用。
希望这可以帮助某人。
确保并设置
display_errors = On
并尝试
error_reporting(E_ALL);
在你的代码中。 很多时候,我包含一个运行时错误配置脚本,它在我开发时打开错误,并在我不开发时关闭它们。 它看起来大致是这样的:
if ($debugmode == 'on') {
error_reporting(E_ALL);
ini_set("display_errors", 1);
}else {
error_reporting(0);
ini_set("display_errors", 0);
}
希望这可以帮助。
** 我没有正确阅读,您想记录错误而不是显示,在这种情况下,混沌的答案就是您正在寻找的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.