Linux learning summary (44) php-fpm related configuration of lnmp

1 php-fpm process pool

Before, we added a sentence include vhost/*.conf to the nginx configuration file; and then realized the isolation of each virtual host, php-fpm is an independent service, is there any similar operation? The answer is yes. Here we introduce a php-fpm process pool concept. In the lnmp architecture, pool is a collection of process resources allocated to parse php. php-fpm can set up multiple pools, in which the resources of one of the pools are exhausted, which will cause other sites to fail to access resources and report a 502 error. It is necessary to separate the sites and use separate pools. We can define include in php-fpm.conf to define a separate pool. The specific process is as follows:
vim /usr/local/php-fpm/etc/php-fpm.conf Change the content as follows

[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

cd /usr/local/php-fpm/etc/
mkdir php-fpm.d
cd php-fpm.d/
vim www.conf //Write the following content, add the first pool

[www]
listen = /tmp/www.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

After saving, edit another configuration file and create a pool
vim test.conf

[test]
listen = /tmp/test.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

In this way, there are two sub-configuration files, that is, two pools are created, and the two pools monitor different sockets. We can define different pools for different virtual hosts in the virtual host configuration file, so as to achieve mutual isolation. Purpose.

Under the test, configure and
/usr/local/php-fpm/sbin/php-fpm -t
restart the service and
/etc/init.d/php-fpm restart
check the sock file under /tmp/:
ls /tmp/*.sock
Linux learning summary (44) php-fpm related configuration of lnmp
Then check the process
ps aux |grep php-fpm
and find that there are two pools, www and test.

2php-fpm slow execution log

If a php website can be accessed, it means that the access speed has slowed down. How can we further find the reason? Is there any way to track down the details that cause php parsing to slow down, here is the slow execution log of php-fpm. Through the slow execution log of php-fpm, we can clearly understand where the php script takes a long time to execute, and it can locate the specific code line. How to open and view the log, the specific operation is as follows:
vim /usr/local/php-fpm/etc/php-fpm.d/www.conf
Add the following content at the end

request_slowlog_timeout = 1
slowlog = /usr/local/php-fpm/var/log/www-slow.log

The first line defines the timeout in seconds, that is, when the PHP script execution time exceeds 1 second, the log will be recorded.
The second line defines the path and name of the log.
We write a PHP script to test

 vim /data/wwwroot/test.com/slow.php
<?php
echo "just a slow running test";
sleep (3);
?>
curl -x127.0.0.1:80 test.com/slow.php

I found that the cursor paused for a few seconds before outputting just a slow running test
. Let's check the slow execution log
cat /usr/local/php-fpm/var/log/www-slow.log
Linux learning summary (44) php-fpm related configuration of lnmp

3open_basedir

This concept has been touched upon in lamp before. The purpose of configuring it is for website security. httpd can set an open_basedir for each virtual host, and php-fpm can also set different open_basedir for different pools

vim /usr/local/php-fpm/etc/php-fpm.d/test.conf  //在最后面加入
php_admin_value[open_basedir] = /data/wwwroot/test.com:/tmp/

4php-fpm error log

In lamp, we have defined the error log separately for php, we have turned off the page display of error information in /usr/local/php-fpm/etc/php.ini, defined the error log path and name, and defined the log level. Because the configuration file is the same as the configuration file in lamp, and both come from the php.ini-production file in the php source package, the configurations are the same as before. I won't go into details here.

5php-fpm process management

Below we introduce the meaning of a configuration in php-fpm.conf

 pm = dynamic  //动态进程管理,也可以是static,静态一次性启动最大子进程数,不会变化。
 pm.max_children = 50 //最大子进程数,ps aux可以查看
 pm.start_servers = 20 //启动服务时会启动的进程数
 pm.min_spare_servers = 5 //定义在空闲时段,子进程数的最少数量,如果达到这个数值时,php-fpm服务会自动派生新的子进程。
 pm.max_spare_servers = 35 //定义在空闲时段,子进程数的最大值,如果高于这个数值就开始清理空闲的子进程。
 pm.max_requests = 500  //定义一个子进程最多处理的请求数,也就是说在一个php-fpm的子进程最多可以处理这么多请求,当达到这个数值时,它会自动退出。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325172230&siteId=291194637