Home >Backend Development >PHP Problem >How to output php error log file in nginx
Nginx is an open source, high-performance web server that can be used as a reverse proxy server, load balancer, etc. And PHP is a commonly used programming language used to develop dynamic websites and web applications.
When using Nginx to deploy PHP programs, you may encounter PHP errors. In order to better troubleshoot and solve problems, PHP error information needs to be recorded in the log file. This article will introduce how to use Nginx to output PHP error log files to facilitate subsequent troubleshooting and processing.
To output the PHP error log file, we need to add some configuration items to the Nginx configuration file. On CentOS systems, the Nginx configuration file is usually located at /etc/nginx/nginx.conf. Find the configuration section of the http module in the file, usually at the top of the file, and add the following configuration items:
http { ... server { ... location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; # error log fastcgi_param PHP_VALUE "error_log=/var/log/nginx/php_errors.log"; } ... } ... }
location ~ .php$ means that it will only be used when accessing URLs ending with .php Configuration items. fastcgi_pass specifies the address and port number of PHP's FastCGI server. fastcgi_param is used to set the parameters of PHP, where SCRIPT_FILENAME specifies the path and file name of the PHP script. include fastcgi_params tells Nginx to use the default FastCGI parameters. The last line adds fastcgi_param PHP_VALUE "error_log=/var/log/nginx/php_errors.log";, which means recording PHP error information to the /var/log/nginx/php_errors.log file.
After configuring Nginx, we need to create a log file to record PHP error information. Assume that we want to save the log file to /var/log/nginx/php_errors.log. We can use the following command to create the file:
sudo touch /var/log/nginx/php_errors.log
Then use the following command to modify the owner and permissions of the file:
sudo chown nginx:nginx /var/log/nginx/php_errors.log sudo chmod 644 /var/log/nginx/php_errors.log
When testing, you can create a PHP script and an error occurs, for example:
<?php echo 1/0; ?>
Save this script as test.php, and then place it in In the web root directory of Nginx, for example /usr/share/nginx/html/test.php. Visit http://localhost/test.php in your browser and you will see a PHP error. Then use the following command to view the contents of the log file:
sudo tail /var/log/nginx/php_errors.log
If everything is normal, you should see an error message similar to the following:
[17-Oct-2021 20:52:42 UTC] PHP Warning: Division by zero in /usr/share/nginx/html/test.php on line 2
In this article, we introduced how to output PHP error log files in Nginx, which is very important for troubleshooting and handling PHP problems. To output a PHP error log file, first add fastcgi_param PHP_VALUE "error_log=/var/log/nginx/php_errors.log"; to the Nginx configuration file, then create a log file and test the output.
The above is the detailed content of How to output php error log file in nginx. For more information, please follow other related articles on the PHP Chinese website!