How to output php error log file in nginx
Configuring Nginx
Add some configuration items to the Nginx configuration file to output PHP error log files. Normally, the Nginx configuration file on CentOS systems is saved in /etc/nginx/nginx.conf. Usually located at the top of the file, you can find the configuration section of the http module in the file and add the following configuration item:
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"; } ... } ... }
This configuration item will only be applied when the accessed URL ends with .php, that is, using location ~.php$. The address and port of the PHP FastCGI server are set in fastcgi_pass. fastcgi_param is used to set the parameters of PHP, where SCRIPT_FILENAME specifies the path and file name of the PHP script. To use the default FastCGI parameters, fastcgi_params needs to be included in Nginx. 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.
Create a log file
Create a log file to record PHP error information. This is a step required after configuring Nginx. 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
Testing
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. When you visit http://localhost/test.php, you will find 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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

There are two ways to solve the Nginx cross-domain problem: modify the cross-domain response header: add directives to allow cross-domain requests, specify allowed methods and headers, and set cache time. Use CORS modules: Enable modules and configure CORS rules that allow cross-domain requests, methods, headers, and cache times.
