Table of Contents
Configuring Nginx
Create a log file
Testing
Home Operation and Maintenance Nginx How to output php error log file in nginx

How to output php error log file in nginx

May 18, 2023 pm 04:59 PM
php 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";
        }
        ...
    }
    ...
}
Copy after login

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
Copy after login

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
Copy after login

Testing

When testing, you can create a PHP script and an error occurs, for example:

<?php
echo 1/0;
?>
Copy after login

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
Copy after login

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
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

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 check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

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 How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

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's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

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.

How to check whether nginx is started? How to check whether nginx is started? Apr 14, 2025 pm 12:48 PM

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: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

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.

How to start nginx in Linux How to start nginx in Linux Apr 14, 2025 pm 12:51 PM

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.

How to solve the problem of nginx cross-domain How to solve the problem of nginx cross-domain Apr 14, 2025 am 10:15 AM

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.

See all articles