【问题标题】:PHP's configuration setting 'error_log' is not workingPHP 配置设置“error_log”不起作用
【发布时间】:2010-12-13 06:37:34
【问题描述】:

我在这里做错了什么?

我在 .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.

这个问题已经被问了 1000 次了,我在发布之前浏览了不同的帖子,但我没有找到答案。只要我一直在使用 PHP 编程,这一直是工作的噩梦。

【问题讨论】:

标签: php error-logging error-log selinux


【解决方案1】:

检查 PHP-FPM 没有明确设置error_log:

确保文件/etc/php-fpm.d/www.conf 不包含error_logphp_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

检查 Apache .htaccess 文件未使用设置 error_log

php_admin_value Apache 配置文件中的设置不能被覆盖,因此请确保您没有任何 php_admin_value 用于 Apache 配置文件中的 error_log 设置。还要检查php_value 设置以防万一。

PHP Website - How to change configuration settings

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 嗨混乱,谢谢。我已经将其设置为开启。我还有什么遗漏吗?
    • 重启服务器即可。
    • @aagjalpankaj 你能解释一下为什么它有帮助以及为什么它被破坏了吗?
    • 文件php.ini在哪里? In /etc/php/7.4/apache2/php.ini?
    • 好的,一种方法是phpinfo() 的输出中带有“加载的配置文件”的行。从命令行:php -r "phpinfo();" | grep "Loaded Configuration File"(示例输出:Loaded Configuration File => /etc/php/7.4/cli/php.ini)。在网络环境中:如果测试页面文件 test.php 存在,它包含对phpinfo() 的调用 - 例如,URL http://localhost/test.php(示例输出:/etc/php/7.4/apache2 /php.ini).
    【解决方案3】:

    Flimm 的故障排除指南

    这是我对error_log() 呼叫不起作用的故障排除指南。

    1. 查看您的服务器配置,找出默认错误日志文件的位置。 这取决于您使用的服务器。为了让你开始,有 如果您使用的是 Apache,请查看 Apache 的 ErrorLog option 或 Nginx 的error_log option 如果您使用的是 Nginx。确保它 设置为文件。如果您使用像 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_flag (docs) 完成的。例如,您可能会在您的.htaccess 文件中找到这一行:php_admin_flag[log_errors] = off。请记住在配置更改后重新启动服务器。此时,您应该可以创建一个包含<?php error_log("test"); 内容的测试文件test.php,重新启动服务器,然后在浏览器中打开URL,您应该可以在错误日志中看到test(服务器或error_log = 指定的服务器)。但请继续阅读。

    5. 检查 PHP 的设置没有在运行时更改log_errors 选项可以在运行时通过运行ini_set('log_errors', 1); 进行更改,其他配置选项error_reportingerror_log 也可以更改。另请注意,有一个特殊的error_reporting() PHP function 会在运行时更改配置。在您的代码库中搜索ini_seterror_reporting 的任何调用。例如,WordPress 会根据 WP_DEBUG 的值运行这些。

    其他需要注意的事项:您可能在 SELinux 中遇到权限问题(请参阅 this answer)。

    【讨论】:

    • 还有一件事:如果您访问的是 https: 网站,那么您需要查看 ssl_error_log 文件,而不是 error_log 文件。跨度>
    • @charlesdeb 你说的是 nginx 吗?
    • 我的经验是使用 apache。我花了很长时间试图弄清楚为什么没有任何内容被记录到 error_log 文件,然后意识到这一切都将发送到 ssl_error_log(尽管有些安装将所有内容发送到 error_log,无论它是否是 SSL。
    • 在我的例子中,应用程序本身有它自己的日志文件。
    【解决方案4】:

    如果其他人无法让他们的本地开发环境记录错误,以下是为我解决的问题:

    在 Windows 上,error_log 必须设置为日志的完整路径,error_log() 才能工作 (error_log = c:\apache\php_errors.log)。但是,如果error_log = php_errors.log 没有路径,php 仍然可以记录启动错误,例如

    PHP 启动:无法加载动态库 'ext\php_mysqli.dll' - 找不到指定的模块

    【讨论】:

    • 我指定了完整路径,并没有提前创建文件。当我重新启动 WAMP 时,会创建日志文件,但 WAMP 会显示一个黄色图标,并且不会加载 localhost。任何想法可能是什么?当我注释掉 error_log 时,一切都很好,但 PHP 错误出现在 Apache 日志中。
    【解决方案5】:

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

    chmod 777 watermellon-app-errors.log
    

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

    【讨论】:

    • 是的,你是对的。当然,如果文件的所有者是其他人,644 可能还不够。您可以使用 777 对其进行测试,然后降低权限以查看有效的方法。
    • 就我而言,我需要 646.. 仅供参考,以防其他人遇到像我这样的问题
    • 在 Mac OS 上,我不得不使用 666,因为普通文件设置为 root:wheel 我可能本来可以保留 646,因为我认为 apache 默认以“_www”运行。跨度>
    • chmod 777 = "设置权限,(U)ser / owner 可以读,可以写,可以执行。(G)roup 可以读,可以写,可以执行。(O)thers能读、能写、能执行。”。这有多安全?
    【解决方案6】:

    如果设置了error_log 指令,该文件将用于记录PHP 错误。如果未设置,错误将记录到 Apache 日志中。看看error_log

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

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

    【讨论】:

      【解决方案7】:

      在我的情况下,在CentOS 开发服务器上,在完整的yum update 之后,/var/log/http 上的权限更改为700,用户为“root” ',因此用户 'apache' 无法输入或写入。

      它仍然能够写入现有文件 /var/log/httpd/error.log,但它无法创建新文件,因为我使用的是日期后缀日志文件。

      发出命令

      chown apache /var/log/httpd
      

      解决了问题。

      【讨论】:

      • chmod 700 = "设置权限,以便 (U)ser / owner 可以读、可以写和可以执行。(G)roup 不能读、不能写和无法执行。(O)其他人无法读取,无法写入,无法执行。”
      【解决方案8】:

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

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

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

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

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

      完成此操作后,您需要重新启动系统才能使更改生效。

      【讨论】:

      • 您给出的建议实际上有一个网站可以阻止给出该建议。 stopdisablingselinux.com
      • 我知道,因此我的写作“理想”。我还不相信在本地工作的家庭用户需要开启 SELinux。
      • 如果 SElinux 的人们停止破坏人们可能会费心使用它的一切。甚至一个 SElinux 日志说,“嘿,我也刚刚阻止了它。如果你愿意,这就是你可以启用它的方法。”真的很有帮助。因为在您说之前无法知道它为什么会损坏,因此可能是 SElinux,因为它们会损坏所有东西。基本上任何损坏的东西,我都会寻找 SElinux,直到我最终放弃,因为它花费了我数小时的时间来诊断它是 SElinux 或搜索晦涩的命令或设置来让这个实例通过。
      【解决方案9】:

      我不明白为什么,但错误日志现在可以正常工作。这就是我所做的。

      我放弃并注释掉了 error_log 指令并关闭了 ini 文件。我运行带有解析错误的脚本以查看 Apache 是否仍会崩溃,并且在日志文件中出现 PHP 错误。这很奇怪,因为 ini 文件不再启用 error_log 并且我的脚本没有使用 ini_set()

      【讨论】:

      【解决方案10】:

      作为bradym said,检查您是否对Apache 用户的PHP 错误日志所在目录具有写入权限。如果您创建了具有写入权限的日志文件,这还不够;目录也应该有它们。

      【讨论】:

        【解决方案11】:

        我遇到了同样的问题并通过这样做解决了它:

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

        我认为 PHP 以用户 www-data 运行,而对 /var/log/apache2 的访问权限仅限于 root。所以,这解决了它。

        【讨论】:

          【解决方案12】:

          error_log = "C:\php\Log\error.log" 行也不适合我。我的解决方案是您不应该自己创建 error.log,因为 PHP 会为您完成。 See the PHP message board。我在 Windows Server 2008 服务器上使用 PHP 5.2。

          【讨论】:

            【解决方案13】:

            对我来说,手动创建文件 /var/log/php_error.log 解决了这个问题:

            1. 在文件php.ini中,配置日志到文件

              log_errors = On
              error_log = /var/log/php_error.log
              
            2. 重启 Apache

              sudo systemctl restart apache2
              
            3. 创建日志文件,例如:

              sudo touch /var/log/php_error.log
              
            4. 赋予正确的权限(所有者必须是 Apache)

              sudo chown www-data:root /var/log/php_error.log
              

              (如果尚未设置)

              sudo chmod 0644 /var/log/php_error.log
              
            5. 这样测试,例如:

              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>
              
            6. 检查错误是否写入

              sudo less /var/log/php_error.log
              

            【讨论】:

            • 感谢@Peter Mortensen 让我的答案更清晰、格式正确!
            【解决方案14】:

            进一步调试: phpinfo();

            它显示当前的 error_log 设置,在 phpinfo() 中的 error_log 值旁边,并且在 vhost 没有记录时很有用。

            在该设置被文件 php.ini 和虚拟主机覆盖后,我来到了这里。我最终登录到 syslog。

            【讨论】:

            • 为@PeterMortensen 的格式和拼写更新欢呼。我已对其进行了更新,以指定在 error_log 下找到 error_log 值。我不确定由此获得了多少价值,但感谢格式和拼写更新。
            【解决方案15】:

            这对我有用。

            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,但这对我不起作用。

            【讨论】:

            • 但是chmod a+wchmod 777不一样(?)。
            猜你喜欢
            • 2011-03-28
            • 2021-11-08
            • 2019-08-16
            • 2017-03-29
            • 2017-08-31
            • 2018-06-25
            • 1970-01-01
            • 2018-06-16
            • 2012-03-05
            相关资源
            最近更新 更多