1. Understanding error reporting and collection methods
In the process of deploying the server, we habitually turn off the error output of PHP. This is because PHP error messages may expose security vulnerabilities or lead to information leakage. But during the development process, we need these error messages to locate problems and debug the program.
One way to solve this problem is to turn on PHP's error output. In PHP we can set the error log level or report errors immediately. We can add some options to the Nginx configuration file to display PHP error messages.
2. PHP error settings in Nginx configuration file
Open the Nginx server configuration file, usually /etc/nginx/nginx.conf
, find the http{}
block, and add the following configuration:
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; } }
Among them, fastcgi_param PHP_VALUE
is used to pass error information to the PHP process requested by the Nginx server. error_reporting=E_ALL
Indicates that all error level information is output. fastcgi_param PHP_VALUE "error_log=/var/log/nginx/php_error.log";
means outputting PHP error information to the /var/log/nginx/php_error.log
file.
After changing the configuration file, reload the Nginx server:
sudo systemctl reload nginx
3. Wrong settings in the PHP configuration file
The PHP configuration file is general For /etc/php/7.4/fpm/php.ini
, find the line error_reporting
and set it to display all error messages:
error_reporting = E_ALL
Then find display_errors
This line, set it to On
, so that all PHP error messages can be displayed on the web page:
display_errors = On
Then save the file and restart PHP- FPM:
sudo systemctl restart php7.4-fpm
The above is the detailed content of How to solve nginx not prompting php error. For more information, please follow other related articles on the PHP Chinese website!