The pool, slow execution log, open_basedir, process management of php-fpm in LNMP architecture (6)

Keywords: PHP vim curl Nginx

1. pool of php-fpm

In order to avoid the problem of using the same pool for multiple sites, which is caused by the failure of one site, and then affects the normal operation of other sites using the same pool, it is necessary to set up a separate pool for each site.

1.1 Configure multiple pool s for php-fpm

Edit the php-fpm configuration file:

[root@host etc]# vim /usr/local/php-fpm/etc/php-fpm.conf  

[www]
listen = /tmp/php-fcgi.sock
#listen = 127.0.0.1:9000
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
……
[zhouqun.com]      //Add a new pool
listen = /tmp/zhouqun.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

1.2 Grammar Detection:

[root@host etc]# /usr/local/php-fpm/sbin/php-fpm -t
[12-Sep-2017 23:26:57] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

1.3 Reload the configuration file:

[root@host etc]# /etc/init.d/php-fpm reload
Reload service php-fpm  done

1.4 View the process:

[root@host etc]# ps aux |grep php-fpm

php-fpm  6222  0.0  0.4 226640  4716 ?        S    16:10  0:00 php-fpm: pool www
php-fpm  6223  0.0  0.4 226640  4712 ?        S    16:10  0:00 php-fpm: pool zhouqun.com

1.5 Set up pool for site

[root@host vhost]# vim /usr/local/nginx/conf/vhost/aaa.com.conf

location ~ \.php$
    {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/zhouqun.sock;    //Change the address of fastcgi_pass to the same address as in php-fpm.conf
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/wwwroot/default$fastcgi_script_name;
    }

1.6 Add php-fpm.conf subconfiguration file

For ease of management, each pool in php-fpm can be managed separately. Add the php-fpm sub-configuration file as follows:

[root@host vhost]# vim /usr/local/php-fpm/etc/php-fpm.conf

[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
include = etc/php-fpm.d/*.conf   //Add to
#Add the parameter "include = etc/php-fpm.d/*.conf" in the global variable section. Then you can clear the other parameters in the php-fpm configuration file and set them separately in the php-fpm.d directory.

1.7 Create the specified directory

[root@host vhost]# cd /usr/local/php-fpm/etc/
[root@host etc]# mkdir php-fpm.d

[root@host etc]# cd php-fpm.d/

1.8 Create php-fpm subconfiguration file

[root@host php-fpm.d]# vim www.conf
[www]
listen = /tmp/php-fcgi.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

[root@host php-fpm.d]# vim zhouqun.conf
[zhouqun.com]
listen = /tmp/zhouqun.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

1.9 Check for grammatical errors and restart:

[root@host php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t
[16-Aug-2017 16:49:17] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[root@host php-fpm.d]# /etc/init.d/php-fpm reload
Reload service php-fpm  done

View the php-fpm process information or use the ps command.

2. php-fpm slow execution log

php website inexplicable access is very slow, you can find the crux through slow execution log, so full log is very important, php website strongly recommends the use of LNMP architecture.

2.1 Open slow execution log:

[root@host php-fpm.d]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf
……
request_slowlog_timeout = 1                //Logging starts when requests exceed 1 second
slowlog = /usr/local/php-fpm/var/log/www-slow.log           //Log Deposit Address
//Detection and reloading
[root@host php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t
[root@host php-fpm.d]# /etc/init.d/php-fpm reload

2.2 experiment

Add files to sites using www pool:

[root@host php-fpm.d]# Vim/data/wwroot/test.com/sleep.php// Create a.php file that deliberately lets it sleep for 2 seconds to slow it down
<?php
echo "test slow log";
sleep(2);     
echo "done";
?>

2.3 Detection:

[root@host php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php 
test slow log done

2.4 Check slow log:

[root@host php-fpm.d]# tail /usr/local/php-fpm/var/log/www-slow.log 

[12-Sep-2017 23:42:23]  [pool www] pid 4236
script_filename = /data/wwwroot/test.com/sleep.php
[0x00007fe027r0e2f5] sleep() /data/wwwroot/test.com/sleep.php:3      //The third line of the display file causes slow access because the third line is the sleep command

3. php-fpm defines open_basedir

In php-fpm service, when a server runs multiple websites, the scope of directories on the servers that each site can access is limited by open_basedir, and the open_ basedir can be set for each pool.

3.1 Core configuration parameters:

[root@host ~]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf  
……
php_admin_value[open_basedir]=/data/wwwroot/test.com:/tmp/     //modify

3.2 Create test PHP scripts:

[root@host php-fpm.d]# vim /data/wwwroot/test.com/1.php
<?php
echo "This is a test php of open_basedir";

3.3 Test:

[root@host php-fpm.d]# curl -x127.0.0.1:80 test.com/1.php
This is a test php of open_basedir

4. Php-fpm process management

Analysis of pool configuration parameters in php-fpm:

[root@host php-fpm.d]# vim www.conf
[www]
listen = /tmp/php-fcgi.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
#Set up the process startup mode (dynamic means dynamic, static means static)
#Only if dynamic is set here will the following configuration take effect
pm.max_children = 50   //Maximum number of subprocesses that can be started
pm.start_servers = 20    //Set the number of processes initially started
pm.min_spare_servers = 5      //Represents that php-fpm has at least a few subprocesses when it is idle
pm.max_spare_servers = 35   //Represents that php-fpm has at most a few subprocesses when it is idle
pm.max_requests = 500          //Represents the maximum number of requests that a child process can accept
rlimit_files = 1024                     //Represents how many file handles each subprocess opens
request_slowlog_timeout = 1    //Logging starts when requests exceed 1 second
slowlog = /usr/local/php-fpm/var/log/www-slow.log      //Log Deposit Address
php_admin_value[open_basedir]=/data/wwwroot/test.com:/tmp/

Posted by Virtuali on Fri, 04 Jan 2019 22:18:09 -0800