首頁  >  文章  >  運維  >  nginx不提示php錯誤如何解決

nginx不提示php錯誤如何解決

PHPz
PHPz轉載
2023-05-26 13:03:461623瀏覽

一、理解錯誤報告及蒐集方法

在部署伺服器的過程中,我們習慣性地關閉了PHP的錯誤輸出,這是因為PHP的錯誤訊息可能會導致安全隱患暴露,或導致資訊外洩。但是在開發過程中,我們需要這些錯誤訊息來定位問題和偵錯程式。

解決這個問題的方法之一是開啟PHP的錯誤輸出。在PHP中我們可以設定錯誤日誌等級或即時報告錯誤。我們可以在Nginx的設定檔中加入一些選項,以顯示PHP錯誤訊息。

二、Nginx 設定檔中的PHP 錯誤設定

#開啟Nginx伺服器的設定文件,一般為/etc/nginx/nginx.conf,找到http{}區塊,新增如下配置:

server {
    # server settings
    ...
    
    # server block location rules
    ...

    # php-fpm status check
    location ~ ^/(status|ping)$ {
        access_log off;
        # php-fpm settings
        fastcgi_param PHP_VALUE "error_reporting=E_ALL";
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    }

    # php error logs
    location ~ \.php$ {
        fastcgi_param PHP_VALUE "error_log=/var/log/nginx/php_error.log";
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    }

    # error pages
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    # static files
    location ~* \.(jpg|jpeg|gif|png|css|js|ico)$ {
        expires 7d;
        access_log off;
    }

    # disable direct access to .ht files
    location ~ /\.ht {
        deny all;
    }
}

其中,fastcgi_param PHP_VALUE用於傳遞錯誤訊息給Nginx伺服器請求的PHP進程。 error_reporting=E_ALL表示輸出所有錯誤等級的資訊。 fastcgi_param PHP_VALUE "error_log=/var/log/nginx/php_error.log";表示將PHP錯誤訊息輸出到/var/log/nginx/php_error.log檔案中。

更改完設定檔後,重新載入Nginx伺服器:

sudo systemctl reload nginx

三、PHP 設定檔中的錯誤設定

PHP的設定檔一般為/etc/php/7.4/fpm/php.ini,找到error_reporting這一行,將其設定為顯示所有的錯誤訊息:

error_reporting = E_ALL

然後找到display_errors這一行,將其設定為On,這樣就可以在網頁上顯示所有的PHP錯誤訊息了:

display_errors = On

然後儲存檔案並重新啟動PHP- FPM:

sudo systemctl restart php7.4-fpm

以上是nginx不提示php錯誤如何解決的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除