简体   繁体   English

PHP 的配置设置“error_log”不起作用

[英]PHP's configuration setting 'error_log' is not working

What am I doing wrong here?我在这里做错了什么?

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 error_reporting = E_ALL | E_STRICT . 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 purposefully 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.这是我在一次错误后的事件日志。 Look 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.

This has been asked 1000 times and I have browsed through the different posts before posting this, but I have not found an answer.这已被问过 1000 次,我在发布之前浏览了不同的帖子,但我没有找到答案。 As long as I have been programming with PHP, this have always been a nightmare to get working.只要我一直在用 PHP 编程,这一直是开始工作的噩梦。

Check PHP-FPM is not explicitly setting error_log :检查 PHP-FPM 没有明确设置error_log

Make sure the file /etc/php-fpm.d/www.conf does not contain php_admin_value settings for error_log.确保文件/etc/php-fpm.d/www.conf不包含 error_log 的php_admin_value设置。 Search for the following and comment them out using a semi-colon:搜索以下内容并使用分号将其注释掉:

; 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_log value using检查 Apache .htaccess 文件没有使用设置error_log

php_admin_value settings in apache configuration files cannot be overridden, so make sure you dont have any php_admin_value for the error_log setting in the Apache configuration files. apache 配置文件中的php_admin_value设置不能被覆盖,因此请确保 Apache 配置文件中的error_log设置没有任何 php_admin_value。 Also check for php_value settings just in case.还要检查php_value设置以防万一。

PHP Website - How to change configuration settings PHP 网站 - 如何更改配置设置

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

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_log must be set to the complete path to the log for error_log() to work ( error_log = c:\\apache\\php_errors.log ).在 Windows 上, error_log必须设置为日志的完整路径才能使error_log()工作( error_log = c:\\apache\\php_errors.log )。 However, if error_log = php_errors.log with no path, php will still be able to log startup errors such as但是,如果error_log = php_errors.log没有路径,php 仍然可以记录启动错误,例如

PHP Startup: Unable to load dynamic library 'ext\php_mysqli.dll' - The specified module could not be found

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.我所有的 .htaccess 设置都是正确的,PHP 只是无法写入错误日志,因为它没有权限。 This fixed it right up for me:这对我来说是正确的:

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 更改为您用于日志的任何文件。

Flimm's Troubleshooting Guide Flimm 的故障排除指南

Here is my troubleshooting guide to error_log() calls not working.这是我对error_log()调用不起作用的故障排除指南。

  1. Look at your server's configuration to 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 ErrorLog option if you're using Apache or Nginx's error_log option if you're using Nginx.为了让您开始,如果您使用的是 Apache,请查看 Apache 的ErrorLog选项,如果您使用的是 Nginx,请查看 Nginx 的error_log选项 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.如果您使用的是 Valet 之类的工具,请注意它在幕后使用了 Nginx 之类的服务器软件。

  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.在类 Unix 系统上,它应该可由正确的用户和组写入,并且父目录及其所有祖先的权限也需要正确。 Use chmod and chown .使用chmodchown

  3. Check the configuration of PHP in the .ini files .检查.ini文件中 PHP配置 Specifically, check for log_errors = On and error_reporting = E_ALL | E_STRICT具体来说,检查log_errors = Onerror_reporting = E_ALL | E_STRICT error_reporting = E_ALL | E_STRICT and error_log = /tmp/example/php_errors.log (see docs for log_errors , error_reporting and error_log configuration settings). error_reporting = E_ALL | E_STRICTerror_log = /tmp/example/php_errors.log (请参阅文档以了解log_errorserror_reportingerror_log配置设置)。 To find the .ini file, look at the output of phpinfo();要找到.ini文件,请查看phpinfo();的输出phpinfo(); . . If error_log is not set, by default it goes to the error log for the server, mentioned in the previous steps.如果未设置error_log ,默认情况下它会转到服务器的错误日志,在前面的步骤中提到过。 If error_log is set to a file, it should already exist and be writeable, just like in previous steps.如果error_log被设置为一个文件,它应该已经存在并且是可写的,就像在前面的步骤中一样。 Remember to restart the server after configuration changes.请记住在配置更改后重新启动服务器。

  4. Check that PHP's settings aren't being changed by server configuration .检查PHP 的设置没有被服务器配置更改 Your server's configuration (even .htaccess ) can change PHP configuration settings.您服务器的配置(甚至.htaccess )可以更改 PHP 配置设置。 In Apache, this is done using php_admin_value and php_admin_flag ( docs ).在 Apache 中,这是使用php_admin_valuephp_admin_flag ( docs ) 完成的。 For instance, you may find in your .htaccess file this line: php_admin_flag[log_errors] = off .例如,您可能会在.htaccess文件中找到这一行: 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.php with the contents <?php error_log("test");此时,您应该能够创建一个测试文件test.php ,其内容为<?php error_log("test"); , restart your server, and open the URL in your browser, and you should be able to see test in your error log (either the server's, or the one specified by error_log = ). ,重新启动服务器,并在浏览器中打开 URL,您应该能够在错误日志中看到test (服务器的,或由error_log =指定的日志)。 But keep reading.但继续阅读。

  5. Check that PHP's settings aren't being changed at run-time .检查PHP 的设置在运行时没有被更改 The log_errors option can be changed at runtime by running ini_set('log_errors', 1);可以在运行时通过运行ini_set('log_errors', 1);更改log_errors选项ini_set('log_errors', 1); , and so can the other configuration options error_reporting and error_log . ,其他配置选项error_reportingerror_log Also note there is a special error_reporting() PHP function which changes the configuration at run-time.另请注意,有一个特殊的error_reporting() PHP 函数可在运行时更改配置。 Search your code-base for any invocations of ini_set or error_reporting .在您的代码库中搜索任何对ini_seterror_reporting调用。 WordPress for example does run these depending on the value of WP_DEBUG .例如,WordPress 确实根据WP_DEBUG的值运行这些。

Other things to look at: You may be having permission issues in SELinux (see this answer ).其他需要注意的事项:您可能在 SELinux 中遇到权限问题(请参阅此答案)。

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.如果设置了 error_log 指令,该文件将用于记录 php 错误,如果未设置,则错误将记录到 Apache 日志中。 Take a look at http://us3.php.net/manual/en/errorfunc.configuration.php#ini.error-log .看看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. error_log 文件及其所在的目录必须可由运行 Apache 的用户写入。 If the file isn't being created, it's probably due to a permissions issue.如果未创建文件,则可能是由于权限问题。

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 会在你身上崩溃,但我猜这是某种权限问题。

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 中的系统日志文件来查看是否发生这种情况

在此处输入图片说明

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/config by 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.执行此操作后,您需要重新启动系统以使更改生效。

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.我放弃并注释掉 error_log 指令并关闭了 ini 文件。 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.我运行带有解析错误的脚本以查看 Apache 仍然会崩溃,并且我在日志文件中收到了 PHP 错误。 This is freaky because the ini file no longer has error_log enabled and my script is not using ini_set().这很奇怪,因为 ini 文件不再启用 error_log 并且我的脚本没有使用 ini_set()。

Does anyone have an explanation for this madness?有没有人对这种疯狂有任何解释? Also, Apache no longer crashes.此外,Apache 不再崩溃。

In my case, on a CentOS development server, after a full yum update the permission on /var/log/http was changed to 700 and the user to 'root', so the user 'apache' wasn't able to enter or write into it.就我而言,在 CentOS 开发服务器上,在完整的yum update后, /var/log/http上的权限更改为 700 并且用户更改为“root”,因此用户“apache”无法输入或写入进去。 It was still able to write into the existing file /var/log/httpd/error.log but it wasn't able to create a new file, as I use date-suffixed log files.它仍然能够写入现有文件/var/log/httpd/error.log但它无法创建新文件,因为我使用日期后缀的日志文件。 Issuing the command发出命令

chown apache /var/log/httpd

solved the problem.解决了这个问题。

For me, manually creating the file, /var/log/php_error.log , solved the issue:对我来说,手动创建文件/var/log/php_error.log解决了这个问题:

  1. In file php.ini , configure log to file在文件php.ini中,配置日志到文件

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

     sudo systemctl restart apache2
  3. Create the log file, for example:创建日志文件,例如:

     sudo touch /var/log/php_error.log
  4. Give the correct rights (owner must be Apache)赋予正确的权限(所有者必须是 Apache)

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

    (And if not already set) (如果尚未设置)

     sudo chmod 0644 /var/log/php_error.log
  5. Test this way, for example:以这种方式测试,例如:

     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. Check the error is written检查错误是否写入

    sudo less /var/log/php_error.log

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_log if log_errors = On .如果log_errors = On则将日志写入error_log指向的路径。 Settings in /etc/php5/cli/php.ini ; /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 ErrorLog apache directive in the virtualhost.如果log_errors = On ( /etc/php5/apache2/php.ini ),则错误附加到虚拟主机中ErrorLog apache 指令指向的路径中。 If that directive does not exist, the php.ini error_log path is used;如果该指令不存在,则使用 php.ini error_log路径;
  • if log_errors = Off not logs are written anywhere;如果log_errors = Off不将日志写入任何地方;

As far as I remember, it almost worked this way in most of the linux stacks据我所知,它在大多数 linux 堆栈中几乎都是这样工作的

As bradym said, check whether you have write permissions to the directory where you php error log is located for apache user.正如 Bradym 所说,检查您是否对 apache 用户的 php 错误日志所在的目录具有写入权限。 If you created a log file with written permissions it's not enough, the dir should have them too.如果您创建了一个具有写入权限的日志文件,这还不够,目录也应该有它们。

For further debugging: phpinfo();进一步调试: phpinfo();

Shows the current error_log setting can be useful when a vhost isn't logging.显示当前的 error_log 设置在 vhost 未记录时很有用。

Ended up here after that setting got overriden from php.ini and the vhost.在该设置从 php.ini 和 vhost 被覆盖后结束。 Was ended up logging to syslog.最终登录到系统日志。

Had the same issue.有同样的问题。 Fixed it by doing this:通过这样做修复它:

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

I think PHP runs as user www-data while access to /var/log/apache2 is limited to root .我认为 PHP 以用户www-data运行,而对/var/log/apache2访问仅限于root So, this fixes it.所以,这解决了它。

The error_log = "C:\\php\\Log\\error.log" was not working for me either. error_log = "C:\\php\\Log\\error.log" 对我也不起作用。 The solution for me was that you shouldn't create the error.log yourself, because PHP will do it for you.我的解决方案是你不应该自己创建 error.log,因为 PHP 会为你做。 See the PHP message board.请参阅 PHP 留言板。 I'm using PHP 5.2 on a Windows 2008 server我在 Windows 2008 服务器上使用 PHP 5.2

This did the trick for me.这对我有用。

setsebool -P httpd_unified 1

Please note, this would be the preferred method below to try first:请注意,这将是以下首选方法:

semanage fcontext -a -t httpd_sys_rw_content_t 'errorLogNameHere.error.log
restorecon -v 'errorLogNameHere.error.log'

This answer was derived from the logs from executing this command line:此答案来自执行此命令行的日志:

journalctl -xe 

Further information on the system I was running on: PHP 7.0 and CentOS 7有关我运行的系统的更多信息:PHP 7.0 和 CentOS 7

Not sure if it's not obvious, but the issue was Apache's configuration to writing files.不确定这是否不明显,但问题是 Apache 写入文件的配置。 I did try chmod 777, chmod a+w on the logging directory, but this didn't work for me.我确实在日志目录上尝试了 chmod 777、chmod a+w,但这对我不起作用。

Hope this can help somebody.希望这可以帮助某人。

Make sure and also set确保并设置

display_errors = On

And try并尝试

 error_reporting(E_ALL);

In your code.在你的代码中。 Often times I include a runtime error config script that turns errors on when I'm developing, and turns them back off when I'm not.很多时候,我包含一个运行时错误配置脚本,它在我开发时打开错误,并在我不开发时关闭它们。 It looks something roughly like this:它看起来大致是这样的:

if ($debugmode == 'on') {
    error_reporting(E_ALL); 
    ini_set("display_errors", 1);
}else { 
    error_reporting(0); 
    ini_set("display_errors", 0);
}

Hope this helps.希望这可以帮助。

** I didn't read that correctly, you want to log errors instead of display, in that case Chaos' answer is what you're looking for. ** 我没有正确阅读,您想记录错误而不是显示,在这种情况下,混沌的答案就是您正在寻找的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM