php-fpm pool, php-fpm slow execution log, open_basedir, php-fpm process management

php-fpm pool:

The pool of php-fpm is the pool of this php-fpm service. php-fpm supports the definition of multiple pools, which can monitor multiple different socks or monitor different ips. If there are several sites running on Nginx, each site can be configured with a pool. When the php of one of the sites reports 502 or other errors, other sites will not be affected. If all sites use the same pool, when one of the php fails, all sites will be paralyzed, so each site needs to be configured with a pool to isolate these sites.

The configuration pool can be configured in the main configuration file of php-fpm:

vim /usr/local/php-fpm/etc/php-fpm.conf

 

2708

在配置文件中增加如下内容:
[aming.com]
listen = /tmp/test.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

Then test to see if there are any syntax errors, if there is no problem, restart:

[root@aming-01 etc]# /usr/local/php-fpm/sbin/php-fpm -t
[02-May-2018 23:25:33] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
[root@aming-01 etc]# /etc/init.d/php-fpm reload

 

At this time, if you look at the php-fpm process, you will find that there is one more pool, which is the aming.com we just configured:

View the process: ps aux |grep php-fpm

 

Then configure the pool in the virtual host configuration file:

vim /usr/local/nginx/conf/vhost/aaa.com.conf

Add the following:

location ~ \.php$

   {

       include fastcgi_params;

       fastcgi_pass unix:/tmp/aming.sock;

       #fastcgi_pass 127.0.0.1:9000;

       fastcgi_index index.php;

       fastcgi_param SCRIPT_FILENAME /data/wwwroot/aming.com$fastcgi_script_name;

   }

Configuration complete example:

 

Configuring all the pools in the main configuration file feels confusing. We can separate these pools into files. First, add the following to the [global] section of the main php-fpm configuration file:

include = etc/php-fpm.d/*.conf

2712

Then create a php-fpm.d directory:

mkdir /usr/local/php-fpm/etc/php-fpm.d/

Go to this directory:

cd /usr/local/php-fpm/etc/php-fpm.d/

Create a www.conf file:

came www.conf

Then copy the pool content of www in the main configuration file of php-fpm and paste it into this www.conf file:

Then create a test.conf, which is also copied and pasted the pool content of aming.com in the main configuration file of php-fpm:

At this time, you can delete all the pools in the php-fpm main configuration file.

#配置文件
[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

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

After the pool is separated in this way, the main configuration file of php-fpm is much cleaner. There is no need to configure the pool in the main configuration file, just import it with include.

Remember to check if the syntax is wrong after configuration. The above is a brief introduction to how to configure the pool.

Check syntax and restart:

 

 

php-fpm slow execution log:

The slow execution log of php-fpm is a very useful thing. If you need to build a php website, it is recommended to use the lnmp architecture, because we can analyze the slow execution log of php-fpm, which is often encountered in the daily work of operation and maintenance. A troublesome problem is that your boss or your customers often report that the access speed of the website has slowed down, so we must know the root cause of the slow access in order to solve this slow access problem. If it is a php website, you can analyze the reason through the slow execution log, so it is said that the slow execution log of php-fpm is a very useful thing.

The following example describes how to configure the slow execution log:

Edit the www.conf file:

vim /usr/local/php-fpm/etc/php-fpm.d/www.conf

增加以下内容:
request_slowlog_timeout = 1
slowlog = /usr/local/php-fpm/var/log/www-slow.log

2718

After the modification is complete, test the syntax and reload the configuration file:

[root@aming-01 php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t    #检测语法是否有错误
[02-May-2018 23:56:48] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
[root@aming-01 php-fpm.d]# /etc/init.d/php-fpm reload         #重启php-fpm服务
Reload service php-fpm  done

Then check to see if a log file is generated, this time the log file is empty:

 

Next, let's simulate a slow-executing php, and we need to write a script:

vim /data/wwwroot/test.com/sleep.php

The content of the script is as follows:

<?php
echo "test slow log";
sleep(2);
echo "done";
?>

 

Use curl to access this script:

2722

Check the slow execution log after the visit. This log tells us the following information:

2723

Now let's open sleep.php and take a look:

2724

So this is the role of the slow execution log, which can help us record the reason for slow access. However, under normal circumstances, it is better to record the log after 2 seconds, because many script executions will cause the execution time to exceed 1 second due to hardware or framework reasons.

 

 

open_basedir:

The role of open_basedir has been introduced in Apache before, so I won't introduce it too much here, but only introduce the configuration process in Nginx.

Edit configuration file:

vim /usr/local/php-fpm/etc/php-fpm.d/www.conf

Add the following:

php_admin_value[open_basedir]=/data/wwwroot/test.com:/tmp/

After saving and exiting, reload the configuration file, and then use curl to access, there is no problem with normal access:

curl -x127.0.0.1:80 test.com/sleep.php -I

 

Define the error log for php-fpm:

vim /usr/local/php-fpm/etc/php.ini

Search: error_log

Then specify the error log storage path under error_log:

2728

define log level

Search: error_reporting

 

2729

After saving and exiting, modify the pool file, deliberately correct the error, and test whether the error log will be recorded:

2730

Then create an error log file and modify this file with 777 permissions:

2731

 

Restart the php-fpm service:

2732

 

For access, you can see that a 404 error was reported:

2733

 

Now you can go and see if this error message is recorded in the log file:

cat /usr/local/php-fpm/var/log/php_errors.log

2734

 

Then we modify the pool file correctly, restart php-fpm again, and then access it again, the status code is 200, no problem:

2735

 

 

php-fpm process management:

Some differences between static and dynamic:

Static:

2737

The number of these processes can be customized.

 

 

 

 

 

Guess you like

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