[英]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.