php error_log无效

时间:2009-11-05 02:48:13

标签: php error-logging error-log selinux

这已被问过1000次,我在浏览之前浏览了不同的帖子,但还没有找到答案。只要我用PHP编写程序,这一直是工作的噩梦。有人可以告诉我这里我做错了吗?

我在ini文件中设置了error_log以及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.

15 个答案:

答案 0 :(得分:9)

您还需要在php.ini中设置log_errors = On

答案 1 :(得分:8)

如果其他人在使用本地开发环境记录错误时遇到问题,请按以下步骤解决问题:

在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

答案 2 :(得分:7)

我遇到的问题是我指定的错误日志是写保护的。我的所有.htaccess设置都是正确的,PHP无法写入错误日志,因为它没有权限。这为我解决了这个问题:

chmod 777 watermellon-app-errors.log

显然,您将要将.log更改为您用于日志的任何文件。

答案 3 :(得分:4)

如果设置了error_log指令,该文件将用于记录php错误,当没有设置时,错误将记录到Apache日志中。看看http://us3.php.net/manual/en/errorfunc.configuration.php#ini.error-log

error_log文件及其所在的目录必须是运行Apache的用户可写的。如果没有创建文件,则可能是由于权限问题。

我不确定为什么Apache会崩溃你,但我猜它是某种权限问题。

答案 4 :(得分:1)

我不明白为什么,但错误日志现在正在运行。这就是我做的。我放弃并注释掉了error_log指令并关闭了ini文件。我用解析错误运行脚本,看看Apache仍会崩溃,我在日志文件中得到了PHP错误。这很怪异,因为ini文件不再启用error_log而我的脚本没有使用ini_set()。

有没有人对这种疯狂有解释?此外,Apache不再崩溃。

答案 5 :(得分:1)

如果您正在使用Fedora,SELinux(默认启用)将阻止apache / httpd将错误附加到您的日志文件,即使您的文件在php.ini中指定并且其包含的目录具有所有权限。

您可以通过查看 / var / log / messages

中的系统日志文件来查看是否发生了这种情况。

enter image description here

理想的解决方案是配置SELinux以允许访问日志文件。

更快的解决方案是通过将SELINUX设置为禁用来禁用 / etc / selinux / config 中的SELinux。

执行此操作后,您需要重启系统才能使更改生效。

答案 6 :(得分:1)

就我而言,在CentOS开发服务器上,在 / var / log / http 的完整yum update权限更改为700并且用户为'root'后,用户'apache'无法输入或写入。它仍然可以写入现有文件 /var/log/httpd/error.log ,但它无法创建新文件,因为我使用以日期为后缀的日志文件。 发出命令

chown apache /var/log/httpd

解决了这个问题。

答案 7 :(得分:0)

正如bradym所说,检查你是否对apache用户的php错误日志所在的目录有写权限。如果你创建了一个具有书面权限的日志文件,那还不够,dir也应该有它们。

答案 8 :(得分:0)

error_log =“C:\ php \ Log \ error.log”对我来说也不起作用。我的解决方案是你不应该自己创建error.log,因为PHP会为你做。 See the PHP message board.我在Windows 2008服务器上使用PHP 5.2

答案 9 :(得分:0)

它在我的ubuntu(Apache 2.4.7,PHP 5.5.9)上的工作方式如下:

命令行脚本:

  • 如果error_log将日志写入log_errors = On指向的路径。 /etc/php5/cli/php.ini;
  • 中的设置

通过apache发送网页请求:

  • 如果log_errors = On/etc/php5/apache2/php.ini),则错误会附加到虚拟主机中ErrorLog apache指令指向的路径中。如果该指令不存在,则使用php.ini error_log路径;
  • 如果log_errors = Off没有在任何地方写入日志;

据我记忆,它在大多数linux堆栈中几乎都是这样的

答案 10 :(得分:0)

这对我有用。

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,但这对我没用。

希望这可以帮助某人。

答案 11 :(得分:0)

这是我针对error_log()的电话无法正常工作的故障排除指南。

  1. 查看您的服务器配置,以查找默认错误日志文件的位置。

    这取决于您使用的服务器。首先,如果您使用的是Apache,请看看Apache的ErrorLog option;如果您使用的是Nginx,请看看Nginx的error_log option。确保将其设置为文件。如果您使用的是Valet之类的工具,请注意,它在后台使用Nginx之类的服务器软件。

  2. 检查服务器错误日志文件的权限

    在类似Unix的系统上,正确的用户和组应该可以写它,并且父目录及其所有祖先的权限也必须正确。使用chmodchown

  3. 检查.ini文件中PHP的配置

    具体来说,检查log_errors = Onerror_reporting = E_ALL | E_STRICTerror_log = /tmp/example/php_errors.log(请参阅log_errorserror_reportingerror_log配置设置的文档)。要查找.ini文件,请查看phpinfo();的输出。如果未设置error_log,则默认情况下它将转到服务器的错误日志,如先前步骤所述。如果将error_log设置为文件,则该文件应该已经存在并且可写,就像前面的步骤一样。记住在配置更改后重新启动服务器。

  4. 检查 PHP的设置没有被服务器配置更改

    您的服务器配置(甚至是.htaccess)都可以更改PHP配置设置。在Apache中,这是使用php_admin_valuephp_admin_flagdocs)完成的。例如,您可能在.htaccess文件中找到以下行:php_admin_flag[log_errors] = off。记住在配置更改后重新启动服务器。

    此时,您应该能够创建包含内容test.php的测试文件<?php error_log("test");,重新启动服务器,并在浏览器中打开URL,并且您应该能够看到{错误日志中的{1}}(服务器的日志或test指定的日志)。但请继续阅读。

  5. 检查 PHP的设置在运行时未更改

    可以在运行时通过运行error_log =来更改log_errors选项,其他配置选项ini_set('log_errors', 1);error_reporting也可以更改。还要注意,有一个特殊的error_reporting() PHP function可以在运行时更改配置。在代码库中搜索error_logini_set的所有调用。例如WordPress会根据error_reporting的值来运行它们。

要注意的其他事项:SELinux中可能存在权限问题(请参见this answer)。

答案 12 :(得分:0)

进一步调试: phpinfo();

显示当前的error_log设置在不记录虚拟主机时很有用。

在php.ini和vhost中覆盖了该设置后,结束了此处。最终记录到syslog。

答案 13 :(得分:0)

有同样的问题。通过执行此操作修复了该问题:

$ chmod 0777 -R /var/log/apache2
$ apachectl restart

我认为PHP以用户www-data的身份运行,而对/var/log/apache2的访问仅限于root。因此,可以解决此问题。

答案 14 :(得分:-3)

确保并设置

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);
}

希望这会有所帮助。

**我没有正确阅读,你想记录错误而不是显示,在这种情况下,Chaos的回答是你正在寻找的。

相关问题