PHP-FPM pool, slow execution log, process management, Open_basedir

Source: Internet
Author: User
Tags fpm php script

The pool of PHP-FPM

To avoid the pool problem caused by one site failure due to multiple sites using the same pool, and to affect the normal operation of other sites using the same pool, configure a separate pool for each site.

    • Add Pool to PHP-FPM


To edit the PHP-FPM configuration file:
......
[Huang.com]
Listen =/tmp/huang.sock
Listen.mode = 666
user = PHP-FPM
Group = PHP-FPM
PM = dynamic
Pm.max_children = 50
Pm.start_servers = 20
Pm.min_spare_servers = 5
Pm.max_spare_servers = 35
Pm.max_requests = 500
Rlimit_files = 1024
......
[Huang.com] Modified by the previous [www] pool.

检测并重载配置文件:[[email protected] ~]# /usr/local/php-fpm/sbin/php-fpm -t[09-Jan-2018 22:36:02] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful[[email protected] ~]# /etc/init.d/php-fpm reloadReload service php-fpm  done

    • View process


[[Email protected] ~]# PS aux | grep php-fpm
......
PHP-FPM 2569 0.0 0.3 227800 5936? S 22:36 0:00 Php-fpm:pool www
PHP-FPM 2570 0.0 0.3 227800 5932? S 22:36 0:00 Php-fpm:pool huang.com
......

    • Configure a pool for a site


[Email protected] ~]# vim/usr/local/nginx/conf/vhost/aaa.com.conf
......
Location ~. php$
{
Include Fastcgi_params;
Fastcgi_pass Unix:/tmp/huang.sock;
Fastcgi_index index.php;
Fastcgi_param Script_filename/data/wwwroot/default$fastcgi_script_name;
}
Add the above parsing PHP configuration. Change the Fastcgi_pass address to PHP-FPM. The same address in Conf is available.
......

检测并重载配置:[[email protected] ~]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload


To add a php-fpm.conf child configuration file

For ease of administration, each pool in the PHP-FPM can be managed separately. Do the following to add the PHP-FPM child configuration file.



    • Edit php-fpm.conf

[[email protected] ~]# vim/usr/local/php-fpm/etc/php-fpm.conf[global]pid =/usr/local/php-fpm/var/run/ Php-fpm.piderror_log =/usr/local/php-fpm/var/log/php-fpm.loginclude = etc/php-fpm.d/*.conf//Add parameter in global variables section "include = Etc/php-fpm.d/*.conf ". You can then clear the other parameters in the PHP-FPM configuration file and set them separately under the PHP-FPM.D directory. Create PHP-FPM Sub-profile directory: [[email protected] ~]# MKDIR/USR/LOCAL/PHP-FPM/ETC/PHP-FPM.D Create Child profile [[email protected] ~]# Vim/usr/local/php-fpm/etc/php-fpm.d/www.conf[www]listen =/tmp/php-fcgi.socklisten.mode = 666user = Php-fpmgroup = PHP-FPMPM = Dynamicpm.max_children = 50pm.start_servers = 20pm.min_spare_servers = 5pm.max_spare_servers = 35pm.max_ Requests = 500rlimit_files = 1024//add above [[email protected] ~]# vim/usr/local/php-fpm/etc/php-fpm.d/huang.conf[ Huang.com]listen =/tmp/huang.socklisten.mode = 666user = Php-fpmgroup = PHP-FPMPM = Dynamicpm.max_children = 50pm.start_s ervers = 20pm.min_spare_servers = 5pm.max_spare_servers = 35pm.max_requests = 500rlimit_files = 1024//Add the above content detection configuration file, and Overload php-FPM service: [[email protected] ~]#/usr/local/php-fpm/sbin/php-fpm-t[09-jan-2018 23:18:55] notice:configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful[[email protected] ~]#/etc/init.d/php-fpm reloadReload Service PHP-FPM Done

    • Test


[[Email protected] ~]# PS aux | grep php-fpm
......
PHP-FPM 2816 0.0 0.3 227836 5948? S 23:19 0:00 Php-fpm:pool huang.com
PHP-FPM 2817 0.0 0.3 227836 5944? S 23:19 0:00 Php-fpm:pool www
......



Slow execution log for PHP-FPM

By php-fpm Slow log, we can clearly understand the PHP script where the execution time is long, it can be targeted to specific rows.

    • Turn on slow execution log


Turn on slow execution log for [Www]pool:
[Email protected] ~]# vim/usr/local/php-fpm/etc/php-fpm.d/www.conf
......
Request_slowlog_timeout = 1
#当请求超过1秒开始记录日志, generally write for 2 seconds
Slowlog =/usr/local/php-fpm/var/log/www-slow.log
#日志存放地址
Add the above two lines of content

检测配置文件,重启php-fpm服务:[[email protected] ~]# /usr/local/php-fpm/sbin/php-fpm -t[09-Jan-2018 23:38:14] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful[[email protected] ~]# /etc/init.d/php-fpm reloadReload service php-fpm  done

To see if the slow execution log file is generated:
[Email protected] ~]# Ls/usr/local/php-fpm/var/log/www-slow.log
/usr/local/php-fpm/var/log/www-slow.log

    • Test

      Add files using the [Www]pool site]



To create a test script file:
[Email protected] ~]# vim/data/wwwroot/test.com/sleep.php
<?php
echo "Test slow Log";
Sleep (2);
echo "Done";
?>

检测:[[email protected] ~]# curl -x127.0.0.1:80 test.com/sleep.phptest slow logdone查看慢执行日志:[09-Jan-2018 23:55:46]  [pool www] pid 2992script_filename = /data/wwwroot/test.com/sleep.php //提示哪个脚本慢[0x00007f86ce814090] sleep() /data/wwwroot/test.com/sleep.php:3//脚本的第几行慢//php网站访问慢,可以查看慢执行日志,排查慢的原因。
    • After opening the php.ini file diplay_errors, you can view the specific error on the browser, this operation should not be used in the official on-line system!


Vim/usr/local/php-fpm/etc/php.ini
Change display_errors = Off to display_errors = On
/ETC/INIT.D/PHP-FPM Reload

执行命令时错误会直接输出在屏幕上。


PHP-FPM definition Open_basedir

Because if the server manages multiple Web sites, it is not appropriate to define multiple Open_basedir in php.ini, so it is either defined in the Apache virtual host configuration file or defined in the PHP-FPM configuration file. We can define the corresponding Open_basedir for different pools (pool).

    • Configure Open_basedir


Configuration for the [WWW] pool
[Email protected] ~]# vim/usr/local/php-fpm/etc/php-fpm.d/www.conf
......
php_admin_value[open_basedir]=/data/wwwroot/test.com:/tmp/
......
Add the previous line configuration

重启php-fpm:[[email protected] ~]# /etc/init.d/php-fpm reloadReload service php-fpm  done
    • Test


[Email protected] ~]# curl-x127.0.0.1:80 test.com/1.txt
Test Open_basedir

Note: If the OPEN_BASEDIR definition error, it will result in inaccessible, form a 404 status Code, output curl directly connected will prompt "No input file specified."

    • Defining error logs

      Display_errors = off Normally, on-line This is OFF, others cannot see your error message through the browser, but instead log your error message to a file on the server



[Email protected] ~]# Vi/usr/local/php-fpm/etc/php.ini
......
Display_errors=off//Search display_errors to see if it is off, not the word change to off.

;error_log = syslogerror_log = /usr/local/php-fpm/var/log/php_error.log   //搜索error_log,在下面添加一行,定义错误日志路径。586行搜索error_reporting注释掉自带的error_reporting;error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICTerror_reporting = E_ALL  //定义错误日志的级别,所有 460行。......保存退出手动创建错误日志文件:[[email protected] ~]# touch /usr/local/php-fpm/var/log/php_error.log[[email protected] ~]# chmod 777 /usr/local/php-fpm/var/log/php_error.log//防止蹦年正常写入
    • Test

      In order to facilitate testing, deliberately write the Open_basedir path in the/usr/local/php-fpm/etc/php-fpm.d/www.conf error, to facilitate testing



[Email protected] ~]# curl-x127.0.0.1:80 test.com/sleep.php-i
http/1.1 404 Not Found
server:nginx/1.8.0
Date:tue, 2018 16:57:28 GMT
content-type:text/html; Charset=utf-8
Connection:keep-alive
x-powered-by:php/7.1.6

To view the error log:
[Email protected] ~]# Cat/usr/local/php-fpm/var/log/php_errors.log
[09-jan-2018 16:57:28 UTC] PHP Warning:Unknown:open_basedir restriction in effect. File (/data/wwwroot/test.com/sleep.php) is not within the allowed path (s): (/data/wwwroot/wwtest.com:/tmp/) in Unknown on Line 0
[09-jan-2018 16:57:28 UTC] PHP Warning:Unknown:failed to open stream:operation not permitted in Unknown on line 0
Error message Access restricted

PHP-FPM Process Management

Vim/usr/local/php-fpm/etc/php-fpm.d/www.conf
[WWW]
Listen =/tmp/php-fcgi.sock
Listen.mode = 666
user = PHP-FPM
Group = PHP-FPM
PM = dynamic
Define how the process is started (dynamic, static)
The following configuration only takes effect when set to dynamic here
Pm.max_children = 50
Maximum number of child processes that can be started
Pm.start_servers = 20
Set the number of processes initially started
Pm.min_spare_servers = 5
Indicates that PHP-FPM has at least a few child processes when idle
Pm.max_spare_servers = 35
Indicates that PHP-FPM is idle for up to a few child processes
Pm.max_requests = 500
Represents the maximum number of requests a child process can accept
Rlimit_files = 1024
Represents the number of file handles opened by each child process
Request_slowlog_timeout = 1
Log when request is more than 1 seconds to start logging
Slowlog =/usr/local/php-fpm/var/log/www-slow.log
Log storage Address
php_admin_value[open_basedir]=/data/wwwroot/test.com:/tmp/

        

PHP-FPM pool, slow execution log, process management, Open_basedir

Related Article

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.