LNMP Architecture (6)

A php-fpm pool

    The pool is called a pool. For example, www in the figure below is a pool. Currently, only one pool is defined in php-fpm. In fact, php-fpm supports the definition of multiple pools. Each pool can monitor different sockets or IP+ ports. For example, nginx has multiple sites, then each site can use a pool. The advantage of this is that when one of the php has a 502, the 502 is likely to be insufficient php resources. If all the sites use the same A pool, then one of the websites has some failures. For example, some programmers write programs that have problems, it will exhaust php resources, and this will cause a result that other sites will also 502, so we have to Isolate each site and use a separate pool for each site

    How to write multiple pools, php can continue to add pools in the configuration file

1. Edit the configuration file

    # vi /usr/local/php-fpm/etc/php-fpm.conf

We add a pool lijie.com to the configuration file

[lijie.com]
listen = /tmp/lijie.sock
#listen = 127.0.0.1:9000
listen.mode = 666
user = php-fpm
group = php-fpm
#listen.owner = nobody
#listen.group = nobody
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

2. Check for grammar errors

    # /usr/local/php-fpm/sbin/php-fpm -t

3. Reload the php-fpm service

    # /etc/init.d/php-fpm reload

4. Check whether the new pool takes effect

    # ps aux |grep pool //As you can see from the figure below, lijie.com already exists in the pool

  

5. Use of pool

    If you want to configure the newly added pool for the website aaa.com, you can do the following configuration in the corresponding nginx virtual host configuration file /usr/local/nginx/conf/vhost/aaa.com.conf:

    The above configuration requires the listening path (socket or IP) to be the same as the pool listening path configured in php-fpm.conf

    After the configuration is completed, it is realized that test.com requests the www pool, and aaa.com requests the lijie.com pool. At this time, if the request volume of test.com is large, the process of php-fpm is exhausted. , a maximum of 50 requests can be requested. As a result, when 50 requests have been completed, when test.com has a new request, the result will report 502, because it has no extra php process to provide services again. At this time The site aaa.com is not affected because it uses another pool

6、include vhost/*.conf

    In nginx.conf, we support the writing method of include vhost/*.conf to separate the configuration files of different virtual hosts into different files. The same is also supported in php-fpm. After writing this, you can Write different pools separately in different files

    First create a directory to store all the pools

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

    Then add the following statement to [global] in /usr/local/php-fpm/etc/php-fpm.conf:

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

    At the same time, write the original two pools into two newly created files, and put these two files in the /usr/local/php-fpm/etc/php-fpm.d/ directory,

     The revised result is as follows:

    1) The content in php-fpm.conf is as follows:

    2) The files in the php-fpm.d directory are as follows:

    3) The content in www.conf is as follows:

    4) The contents of lijie.conf are as follows:

 

Two php-fpm slow execution log

    This slow execution log is very useful. One of the reasons why LNMP is recommended is that we can analyze the slow execution logs of php-fpm. In our operation and maintenance work, we often encounter a problem. The boss or customers report that the website is slow. If you want to know the reason for the slow website, let’s not talk about the java website first. The PHP website has a way to check where the website is slow, like the system When the load is high, we can use various tools, such as vmstat, iostat, stop, etc., to find out which process is causing the high load. When analyzing the slow website here, we need to find out the evidence to prove the slowness. Let's go Configuring slow execution logs for LNMP

1. Edit the configuration file of php-fpm

        # vi /usr/local/php-fpm/etc/php-fpm.d/www.conf

    Add the following to the above file:

request_slowlog_timeout=1 //Record the log if it exceeds 1 second

slowlog = /usr/local/php-fpm/var/log/www-slow.log //Define the log storage path

2. Check the php-fpm configuration file for syntax errors and reload the configuration file

        # /usr/local/php-fpm/sbin/php-fpm -t

        # /etc/init.d/php-fpm reload

3. Check whether the log file is generated

        # ls /usr/local/php-fpm/var/log/

The above figure shows that the log file has been generated

4. View the contents of the log file

        # cat /usr/local/php-fpm/var/log/www-slow.log

    You can see that there is currently no content in the log file

5. Simulate logs of slow-executing websites

    Let's write a script first. The pool configuration file www.conf is used by the website test.com, so we create a script file sleep.php under the directory /data/wwwroot/test.com/ of this website

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

    Add the following to this script file:

<?php echo

"test slow log";

sleep(2);

echo "done";

?>

6. Test verification

        Visit test.com/sleep.php, you can find that after a pause in the execution process, the following result appears:

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

    Troubleshooting skills: If there is a problem with the above access, you can generally view the error log, but we can also set display_error = on in the php.ini configuration file, which means returning error information to the user's browser

7. View the slow log log

    # cat /usr/local/php-fpm/var/log/www-slow.log

    In the picture above, you can see that the log content indicates that the third line of the sleep.php file of the website is slow. Next, let's check the third line of this file.

    # cat /data/wwwroot/test.com/sleep.php

    This is because the third line of our file executes sleep for 2 seconds, and our php-fpm configuration file is set to execute more than 1 second and it will be recorded in the log, so it is recorded here. In actual work, Due to some code framework reasons, there are still many code executions that exceed 1 second, so we'd better set the timeout to 2 seconds

三 open_basedir

    The role of open_basedir is to limit the activities of php in the specified directory. What I learned earlier is to define open_basedir in php.ini. If the server has multiple websites, it is not appropriate to define it in php.ini. All are either defined in the apache virtual host configuration file or configured in php-fpm Defined in the file, we can configure open_basedir for each pool, let's expand in the second way

1. Edit the pool configuration file of the test.com website

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

    Add the following to the above configuration file:

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

2. Reload the configuration file

    # /usr/local/php-fpm/sbin/php-fpm -t

    # /etc/init.d/php-fpm reload

3. Test verification

    # curl -x127.0.0.1:80 test.com/3.php //The following error occurs in the test

    # curl -x127.0.0.1:80 test.com/3.php -I //The result is 404

4. View the error log

    1) First, you need to turn on the error log switch in the php.ini configuration file, and then edit the configuration file

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

We first find display_error = Off (under normal circumstances, you need to turn off the switch to display the error message, so that others cannot see the error message in the browser)

Then find log_errors and set the value log_errors=on to record the error log

Find error_log again, set the recording location of the error log error_log=/usr/local/php-fpm/var/log/php_errors.log

There is also an error_report, set the log level to E_ALL

    2) The path to create the error log file

    # touch /usr/local/php-fpm/var/log/php_errors.log

    3) curl again

    # curl -x127.0.0.1:80 test.com/3.php //At this time, after executing this command, the error log will record the corresponding error information

    4) View the error message

    # cat /usr/local/php-fpm/var/log/php_errors.log //The error message is as follows

    The error message above shows that the path is unknown

5. Modify the corresponding document according to the error prompt

    From the above tips, we can know that there is a problem with the path of open_basedir. Let's check the www configuration file of the pool

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

    As you can see from the above figure, our path is indeed wrong, and the correct one should be the path in the figure below

6. After the modification is completed, reload the configuration file, and then execute the command

    # curl -x127.0.0.1:80 test.com/3.php -I //Access successful 200 OK

 

Four php-fpm process management

There is a section in the php configuration file as follows:

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、pm = dynamic  

    This section is used to indicate in what form the process is started, where dynamic is dynamic, and dynamic is for example, I can let it start 20 sub-processes at the beginning, and automatically generate new sub-processes according to requirements, such as a large number of subsequent visits. Process, if the server is relatively idle, it can also be automatically destroyed. When the destruction reaches a certain level, it will automatically generate a new child process. So how to control when it is destroyed and when it is generated? It is defined by the following parameters. Only when pm = dynamic, the following parameters will take effect.

pm.max_children = 50  //Maximum number of child processes
pm.start_servers = 20     //This line means that 20 child processes are generated at the beginning
pm.min_spare_servers = 5   //Indicates that at least 5 child processes are reserved when idle
pm.max_spare_servers = 35   / /Define the maximum number of child processes when idle, if it is higher than this number, start to clean up the idle child processes
pm.max_requests = 500   //Define a child process that can handle up to 500 requests, when this value is reached, the child process The process will automatically exit. If it does not exit and the child process has problems, an exception will occur, so the process needs to be killed. Because of the above parameters, the child process can be automatically derived.
    We configure lijie.conf as follows:

    After reloading the config file, view the progress

    # ps aux |grep pool

    You can see that the number of child processes started by default is 20, but the maximum number will not exceed 50

2、pm = static

    If you don't want to use pm = dynamic, you can also use pm = static static. Static means static. If pm = static, then only the configuration of pm.max_children = 50 will take effect. At this time, 50 child processes will be generated directly when starting. We will now configure the following according to this scheme:

    Tip: Comments are represented by a semicolon ";"

    After reloading the config file, view the progress

    # ps aux |grep pool

    In the above two pictures, you can see that there are 50 sub-processes of www.

 


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

 

The recommended link
is the difference between root and alias in  nginx http://blog.csdn.net/21aspnet/article/details/6583335 
nginx alias and root configuration  http://www.ttlsa.com/nginx/nginx-root_alias-file- path-configuration/
http://www.iigrowing.cn/shi-yan-que-ren-nginx-root-alias-location-zhi-ling-shi-yong-fang-fa.html  this is more detailed

 

Guess you like

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