php error_log 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1678009/
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-25 03:28:54  来源:igfitidea点击:

php error_log not working

phperror-loggingerror-logselinux

提问by Jim

This has been asked 1000 times and I have browsed through the different posts before posting this but have not found an answer. As long as I have been prgramming with PHP, this have always been a nightmare to get working. Can someone please tell me what I am doing wrong here?

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

I have error_log set in the ini file along with error_reporting = E_ALL | E_STRICT

我在 ini 文件中设置了 error_log 以及 error_reporting = E_ALL | E_STRICT

What else am I missing? This usually gave it to me. I want this set in the ini file and not in my scripts.

我还缺少什么?这通常给我。我希望在 ini 文件中而不是在我的脚本中设置。

Another interesting thing that is happening is that when I purposfully try and throw an error in one of my scripts, Apache restarts over and over again.

发生的另一件有趣的事情是,当我有目的地尝试在我的一个脚本中抛出错误时,Apache 一遍又一遍地重新启动。



This is my event log after one error. Loot at the timestamp

这是一个错误后我的事件日志。掠夺时间戳

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.

回答by chaos

You also need to set log_errors = Onin php.ini.

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

回答by Basil Musa

Check PHP-FPM is not explicitly setting error_log:

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

Make sure the file /etc/php-fpm.d/www.confdoes not contain php_admin_valuesettings for error_log. Search for the following and comment them out using a semi-colon:

确保该文件/etc/php-fpm.d/www.conf不包含php_admin_valueerror_log 的设置。搜索以下内容并使用分号将其注释掉:

; 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

Then restart php-fpm:

然后重启php-fpm:

systemctl restart php-fpm

Check Apache .htaccess files are not setting the error_logvalue using

检查 Apache .htaccess 文件没有error_log使用设置值

php_admin_valuesettings in apache configuration files cannot be overridden, so make sure you dont have any php_admin_value for the error_logsetting in the Apache configuration files. Also check for php_valuesettings just in case.

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

PHP Website - How to change configuration settings

PHP 网站 - 如何更改配置设置

回答by Mel Reams

In case anyone else is having trouble getting their local development environment to log errors, here's what fixed it for me:

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

On windows, error_logmust be set to the complete path to the log for error_log()to work (error_log = c:\apache\php_errors.log). However, if error_log = php_errors.logwith no path, php will still be able to log startup errors such as

在 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

回答by Thomas Keene

The problem I ran into was that the error log I had designated was write protected. All my .htaccess settings were correct, PHP just couldn't write to the error log because it had no permissions. This fixed it right up for me:

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

chmod 777 watermellon-app-errors.log

Obviously, you're going to want to change the .log to whatever file you're using for a log.

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

回答by bradym

If the error_log directive is set, the file will be used for recording php errors, when it is not set errors will be logged to the Apache log. Take a look at http://us3.php.net/manual/en/errorfunc.configuration.php#ini.error-log.

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

The error_log file and the directory it's in must be writable by the user that Apache is running under. If the file isn't being created, it's probably due to a permissions issue.

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

I don't know for sure why Apache would be crashing on you, but I'm guessing it's a permissions issue of some sort.

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

回答by Flimm

Here is my troubleshooting guide to error_log()calls not working.

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

  1. Look at your server's configurationto find out where the default error log file is.

    This depends on which server you're using. To get you started, have a look at Apache's ErrorLogoptionif you're using Apache or Nginx's error_logoptionif you're using Nginx. Make sure it is set to a file. If you're using a tool like Valet, note that it's using server software like Nginx behind the scenes.

  2. Check the permissions of your server's error log file.

    On Unix-like systems, it should be writeable by the correct user and group, and the permissions of the parent directory and all its ancestors need to be correct as well. Use chmodand chown.

  3. Check the configuration of PHP in the .inifiles.

    Specifically, check for log_errors = Onand error_reporting = E_ALL | E_STRICTand error_log = /tmp/example/php_errors.log(see docs for log_errors, error_reportingand error_logconfiguration settings). To find the .inifile, look at the output of phpinfo();. If error_logis not set, by default it goes to the error log for the server, mentioned in the previous steps. If error_logis set to a file, it should already exist and be writeable, just like in previous steps. Remember to restart the server after configuration changes.

  4. Check that PHP's settings aren't being changed by server configuration.

    Your server's configuration (even .htaccess) can change PHP configuration settings. In Apache, this is done using php_admin_valueand php_admin_flag(docs). For instance, you may find in your .htaccessfile this line: php_admin_flag[log_errors] = off. Remember to restart the server after configuration changes.

    At this point, you should be able to create a test file test.phpwith the contents <?php error_log("test");, restart your server, and open the URL in your browser, and you should be able to see testin your error log (either the server's, or the one specified by error_log =). But keep reading.

  5. Check that PHP's settings aren't being changed at run-time.

    The log_errorsoption can be changed at runtime by running ini_set('log_errors', 1);, and so can the other configuration options error_reportingand error_log. Also note there is a special error_reporting()PHP functionwhich changes the configuration at run-time. Search your code-base for any invocations of ini_setor error_reporting. WordPress for example does run these depending on the value of WP_DEBUG.

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

    这取决于您使用的服务器。要开始使用,请查看 Apache 的ErrorLog选项(如果您使用的是 Apache)或 Nginx 的error_log选项(如果您使用的是 Nginx)。确保将其设置为文件。如果您使用的是 Valet 之类的工具,请注意它在幕后使用了 Nginx 之类的服务器软件。

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

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

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

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

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

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

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

  5. 检查PHP 的设置在运行时没有被更改

    log_errors选项可以在运行时通过运行更改,ini_set('log_errors', 1);其他配置选项error_reportingerror_log. 另请注意,有一个特殊的error_reporting()PHP 函数可以在运行时更改配置。在您的代码库中搜索ini_set或 的任何调用error_reporting。例如,WordPress 确实会根据WP_DEBUG.

Other things to look at: You may be having permission issues in SELinux (see this answer).

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

回答by Zoltan M

In my case, on a CentOS development server, after a full yum updatethe permission on /var/log/httpwas changed to 700 and the user to 'root', so the user 'apache' wasn't able to enter or write into it. It was still able to write into the existing file /var/log/httpd/error.logbut it wasn't able to create a new file, as I use date-suffixed log files. Issuing the command

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

chown apache /var/log/httpd

solved the problem.

解决了这个问题。

回答by Anthony

If you are using Fedora, SELinux (enabled by default) will prevent apache / httpd from appending errors to your log file even when your file is specified in php.ini and its containing directory has all permissions allowed.

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

You can see if this is happening by looking at your system log file in /var/log/messages

您可以通过查看/var/log/messages 中的系统日志文件来查看是否发生这种情况

enter image description here

在此处输入图片说明

The ideal solution is to configure SELinux to allow access on the log file.

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

The quicker solution is to disable SELinux in /etc/selinux/configby setting SELINUX to disabled.

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

You'll need to reboot your system after doing this for the change to take effect.

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

回答by Jim

I don't understand why, but the error log is now working. Here is what I did. I gave up and commented back out the error_log directive and closed the ini file. I ran the script with the parse error to see of Apache would still crash and I got the PHP error in the log file. This is freaky because the ini file no longer has error_log enabled and my script is not using ini_set().

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

Does anyone have an explanation for this madness? Also, Apache no longer crashes.

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

回答by Elvis Ciotti

The way it works on my ubuntu (Apache 2.4.7, PHP 5.5.9) is the following:

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

command line script:

命令行脚本:

  • writes the log into the path pointed by error_logif log_errors = On. Settings in /etc/php5/cli/php.ini;
  • 将日志写入error_logif指向的路径log_errors = On。中的设置 /etc/php5/cli/php.ini

web request via apache:

通过 apache 的网络请求:

  • if log_errors = On(/etc/php5/apache2/php.ini), the error appended into the path pointed by ErrorLogapache directive in the virtualhost. If that directive does not exist, the php.ini error_logpath is used;
  • if log_errors = Offnot logs are written anywhere;
  • if log_errors = On( /etc/php5/apache2/php.ini),错误附加到ErrorLog虚拟主机中 apache 指令指向的路径中。如果该指令不存在,error_log则使用php.ini路径;
  • 如果log_errors = Off没有日志写在任何地方;

As far as I remember, it almost worked this way in most of the linux stacks

据我所知,它在大多数 linux 堆栈中几乎都是这样工作的