Log PHP Logs

Source: Internet
Author: User
Tags fpm php error php error log php script

1 , log php error logs

The difference between display_errors and log_errors

Display_errors
Error echo, commonly used in development mode, but many applications also forget to turn off this option in a formal environment. Error echo can expose a lot of sensitive information to facilitate attackers ' next attack. It is recommended to turn this option off.
display_errors = on
On the state, if there is an error, the error message, there are errors
dispaly_errors = Off
Off state, if an error occurs, the prompt: Server error. But there are no error prompts

log_errors
Use this in a formal environment and keep the error message in the log. The error echo can be turned off exactly.
For PHP developers, once a product is put into use, the first thing to do is to turn off the display_errors option to avoid hackers being hacked by the paths, database connections, data tables, and other information that these errors reveal.

When a product is put into use, there will inevitably be error messages, so how do you document these useful information for developers?
The PHP log_errors is turned on, by default it is logged to the Web server's log file, such as Apache's Error.log file. Of course, the error log can also be logged to the specified file.

# Vim/etc/php.ini

Display_errors = Off
Log_errors = On
Error_log =/var/log/php_error/php-error.log

Alternatively, you can set error_log = syslog to log these error messages to the operating system.

Error_reporting set the level of error message return

E_all can be from bad coding practices to harmless prompts to all information that goes wrong. E_all is a bit too thin for the development process because it shows hints on the screen for small things (such as variables not initialized), which can mess up the browser's output
It is best to change the default value to: Error_reporting =e_all & ~e_notice

Default values are: E_all & ~e_deprecated & ~e_strict

php.ini in display_errors = Off resolution of the failure

Questions:
PHP settings file php.ini has been set display_errors = Off, but during the run, the page will still appear error message.

Solution:
After checking log_errors= on, according to the official statement, when this log_errors is set to ON, then you must specify the Error_log file, if not specified or the specified file does not have permission to write, so will output to the normal output channel, then also makes the Display_ Errors this specified off fails, the error message is printed out. So the log_errors = Off, the problem is solved.

2 , implement the configuration method of recording PHP error log when using PHP-FPM in Nginx

Nginx only accesses the page to make access log. There will be no error log information for PHP. Nginx to the PHP request to the PHP-FPM fastcgi process to handle, the default PHP-FPM will only output PHP-FPM error message, in PHP-FPM errors log also can not see the errorlog of PHP.

The reason is that the PHP-FPM profile php-fpm.conf The default is to turn off the worker process error output and redirect them directly to/dev/null, so we're in Nginx error log And PHP-FPM's errorlog don't see PHP error logs.

So we have to make the following settings to see the Nginx under the PHP-FPM does not log php error logging method:

1, Modify php-fpm.conf If there is no increase in the configuration , please add:

[Global]
Error_log =/var/log/php_error/php-fpm.log
[WWW]
Catch_workers_output = yes

2. Modify The configuration in the php.ini , no increase:

Display_errors = Off

Log_errors = On
Error_log =/var/log/php_error/php-error.log
Error_reporting=e_all&~e_notice

3. Restart php-fpm

When PHP executes the error, it can see the error log in "/var/log/php_error/php-fpm.log".

3. Enable slow log configuration in Nginx php-fpm (used to detect slow PHP script execution)

After turning to NGINX+PHP-FPM, he was plagued by 500,502 of problems. When Nginx receives the above error code, it can be determined that the backend php-fpm parsing php out of some kind of problem, such as execution error, execution timeout.

Php-fpm.conf has a parameter in the configuration file request_slowlog_timeout is described in this way

When Request_slowlog_timeout is set to a specific second, request_slowlog_timeout=5 indicates which script executes longer than 5 seconds and logs the script to a slow log file

Request_slowlog_timeout =0 indicates that slow log output is turned off.

The slow log file location defaults to the log folder in the PHP installation directory and can be specified by modifying the Slowlog = log/$pool. Log.slow parameter.

PHP-FPM Slow Log example, the slow log will record the process number, the script name, which file which line of code which function execution time is too long.

Request_slowlog_timeout and Slowlog need to be set at the same time, open request_slowlog_timeout and need to turn on Slowlog

Slow log paths need to be created manually

To turn on the php-fpm slow log step specifically:

VI etc/php-fpm.conf
Remove request_slowlog_timeout, slowlog prefix semicolon '; ', set request_slowlog_timeout = 5;

: Wq
Save exit

Create slow Log directory
mkdir Log

Restart PHP-FPM
Kill-int ' Cat Var/run/php-fpm.pid
sbin/php-fpm

4. All specific steps

1, vi php.ini

Dispaly_errors = Off

Log_errors = On

Error_log =/var/log/php_error/php-error.log

error_reporting = e_all & ~e_deprecated& ~e_strict

2, VI php-fpm.conf

[Global]

Error_log =/var/log/php_error/php-fpm.log

[WWW]

Catch_workers_output = yes

Request_slowlog_timeout = 5

Slowlog =/var/log/php_error/slow.log

3, Mkdir/var/log/php_error

Chown Www:www/var/log/php_error

4, first detection, if there is no error, then perform overloading:/usr/local/php/sbin/php-fpm–t

Reload: Service PHP-FPM Reload

5. See if there is a corresponding log file generation under the/var/log/php_error folder

6. Log polling Vi/etc/logrotate.d/nginx

Add the log path:/var/log/php_error/*.log

Note: 6 Specific reference: Use the Linux command logrotate to cut Nginx logs

Http://www.cnblogs.com/sanduzxcvbnm/p/6077247.html

Log PHP Logs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.