12.21 PHP-FPM's Pool
- Requirements background:
Suppose Nginx has multiple sites, each with its own pool, so that when a site has a problem (such as a 502 error), other sites are unaffected.
- The following is an example of setting up [www] and [Karu] two pool as a case study.
Specific procedures:
Vim/usr/local/php-fpm/etc/php-fpm.conf #在 [Global] section increased
Include = etc/php-fpm.d/*.conf
- mkdir/usr/local/php-fpm/etc/php-fpm.d/
- cd/usr/local/php-fpm/etc/php-fpm.d/
- Add two new profiles Www.con and karu.com and edit as follows:
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
Vim karu.conf #内容如下
[Karu]
Listen =/tmp/karu.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
- /usr/local/php-fpm/sbin/php-fpm-t #检测是否有语法错误
- /ETC/INIT.D/PHP-FPM Restart #重启php-fpm service for configuration to take effect
12.22 php-fpm Slow Execution log [very useful]
- Requirements background:
For sites with LNMP architecture, if the site is found to be slow, you can use PHP-FPM's slow execution log to troubleshoot the mitigation issues caused by PHP.
- The specific steps to implement are as follows:
- Vim/usr/local/php-fpm/etc/php-fpm.d/karu.conf
- At the end, add the following:
Request_slowlog_timeout = 1 #记录php执行超过1秒的操作
Slowlog =/usr/local/php-fpm/var/log/www-slow.log #定义慢执行日志所在位置
- /usr/local/php-fpm/sbin/php-fpm-t
- /ETC/INIT.D/PHP-FPM Reload
- Simulate a slow-executing PHP:
- vim/data/wwwroot/default/sleep.php #写入如下内容
<?php
echo "Test slow Log";
Sleep (2);
echo "Done";
?>
- Test results:
Curl-x127.0.0.1:80 default/sleep.php
Cat/usr/local/php-fpm/var/log/www-slow.log
12.23 Open_basedir
- (1) The role of Open_basedir in PHP-FPM is to restrict PHP activity in the specified directory to improve the security of the site.
- The specific configuration is as follows:
Vim/usr/local/php-fpm/etc/php-fpm.d/karu.conf
- Add the following:
php_admin_value[open_basedir]=/data/wwwroot/default:/tmp/
- /usr/local/php-fpm/sbin/php-fpm-t
- /ETC/INIT.D/PHP-FPM Reload
Test:
Curl-x127.0.0.1:80 default/sleep.php
Curl-x127.0.0.1:80 Default/sleep.php-i
- (2) Configuring the PHP-FPM error log
The steps are as follows:
Vi/usr/local/php-fpm/etc/php.ini
Display_errors = Off #不让其他人能通过浏览器看到错误信息
Log_errors = on #打开错误日志记录功能
Error_log =/usr/local/php-fpm/var/log/php_errors.log #错误日志存放位置
error_reporting = E_all & ~e_deprecated & ~e_strict #定义错误日志级别
- Edit Karu.conf again
Vim/usr/local/php-fpm/etc/php-fpm.d/karu.conf
- Deliberately write it wrong, such as:
php_admin_value[open_basedir]=/data/wwwroot/asdefault:/tmp/
- /usr/local/php-fpm/sbin/php-fpm-t
/ETC/INIT.D/PHP-FPM Reload
- Test:
Curl-x127.0.0.1:80 default/sleep.php
Cat/usr/local/php-fpm/var/log/php_errors.log #查看错误日志
12.24 PHP-FPM Process Management
- There are process management related configuration items in the PHP-FPM configuration file, such as:
Vim/usr/local/php-fpm/etc/php-fpm.d/karu.conf
- The specific meanings are as follows:
- PM = dynamic
Dynamic process management can also be static. Note that when it is static, the following items do not take effect!
- Pm.max_children = 50
Maximum number of child processes
- Pm.start_servers = 20
Number of processes that will start when the service is started
- Pm.min_spare_servers = 5
Defines the minimum number of child processes in the idle period, and if this value is reached, the PHP-FPM service automatically derives the new child process.
- Pm.max_spare_servers = 35
Defines the maximum number of child processes in the idle period, or, if higher than this value, begins to clean up idle child processes.
- Pm.max_requests = 500
Defines the maximum number of requests processed by a child process, meaning that at most one php-fpm child process can handle so many requests, and when this value is reached, it exits automatically.
2018-3-19 Linux Learning Notes