nginx php-fpm 輸出php錯誤記錄檔

來源:互聯網
上載者:User

nginx是一個web伺服器,因此nginx的access日誌只有對訪問頁面的記錄,不會有php 的 error log資訊。

nginx把對php的請求發給php-fpm fastcgi進程來處理,預設的php-fpm只會輸出php-fpm的錯誤資訊,在php-fpm的errors log裡也看不到php的errorlog

原因是php-fpm的設定檔php-fpm.conf中預設是關閉worker進程的錯誤輸出,直接把他們重新導向到/dev/null,所以我們在nginx的error log 和php-fpm的errorlog都看不到php的錯誤記錄檔。

調試起來就很痛苦了。解決nginx下php-fpm不記錄php錯誤記錄檔的辦法:

1.修改php-fpm.conf中配置 沒有則增加
catch_workers_output = yes
error_log = log/error_log

2.修改php.ini中配置,沒有則增加
log_errors = On
error_log = "/usr/local/lnmp/php/var/log/error_log"
error_reporting=E_ALL&~E_NOTICE

3.重啟php-fpm,
當PHP執行錯誤時就能看到錯誤記錄檔在"/usr/local/lnmp/php/var/log/error_log"中了

請注意:

1. php-fpm.conf 中的php_admin_value[error_log] 參數 會覆蓋php.ini中的 error_log 參數
所以確保你在phpinfo()中看到的最終error_log檔案具有可寫入權限並且沒有設定php_admin_value[error_log] 參數,否則錯誤記錄檔會輸出到php-fpm的錯誤記錄檔裡。


2.找不到php.ini位置,使用php的phpinfo()結果查看


3.如何修改PHP錯誤記錄檔不輸出到頁面或螢幕上

修改php.ini

display_errors = off //不顯示錯誤資訊(不輸出到頁面或螢幕上)
log_errors = on //記錄錯誤資訊(儲存到記錄檔中)
error_reporting = E_ALL //捕獲所有錯誤資訊
error_log = //設定記錄檔名

程式中修改以上配置

ini_set("display_errors",0)
ini_set("error_reporting",E_ALL); //這個值好像是個PHP的常量
ini_set("error_log","<記錄檔名>")
ini_set("log_errors",1);

4.如何將php的錯誤記錄檔輸出到nginx的錯誤記錄檔裡

在PHP 5.3.8及之前的版本中,通過FastCGI啟動並執行PHP,在使用者訪問時出現錯誤,會首先寫入到PHP的errorlog中

如果PHP的errorlog無法寫入,則會將錯誤內容返回給FastCGI介面,然後nginx在收到FastCGI的錯誤返回後記錄到了nginx的errorlog中

在PHP 5.3.9及之後的版本中,出現錯誤後PHP只嘗試寫入PHP的errorlog中,如果失敗則不會再返回到FastCGI了,錯誤記錄檔會輸出到php-fpm的錯誤記錄檔裡。
所以如果想把php錯誤記錄檔輸出到nginx錯誤記錄檔,需要使用php5.3.8之前的版本,並且設定檔中php的error_log對於php worker進程不可寫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.