12.21 php-fpm的pool

为了避免因多站点使用同一个pool时因一个站点故障导致pool出问题,进而影响使用同一个pool的其他站点的正常运行,要对每个站点配置一个单独的pool。

为php-fpm增加pool

编辑php-fpm配置文件:

[root@centos-01 etc]# vim 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 [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

语法检测: [root@centos-01inux etc]# /usr/local/php-fpm/sbin/php-fpm -t [16-Aug-2017 16:10:23] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

重新加载配置文件: [root@centos-01inux etc]# /etc/init.d/php-fpm reload Reload service php-fpm done 查看进程:

[root@centos-01inux 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 aming.com 为站点配置pool

[root@centos-01inux vhost]# vim /usr/local/nginx/conf/vhost/aaa.com.conf

location ~ .php$ { include fastcgi_params; fastcgi_pass unix:/tmp/adai.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/wwwroot/default$fastcgi_script_name; } 说明: 把fastcgi_pass地址改为和php-fpm.conf中一样的地址就可以。

添加php-fpm.conf子配置文件

为了便于管理,可以将php-fpm中的每个pool单独进行管理。进行如下操作,添加php-fpm子配置文件:

[root@centos-01inux 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 说明: 在全局变量版块添加参数“include = etc/php-fpm.d/.conf”。然后可以清除php-fpm配置文件中其他参数,再到php-fpm.d目录下进行单独设置。

[root@centos-01inux vhost]# cd /usr/local/php-fpm/etc/

创建指定目录:
[root@centos-01inux etc]# mkdir php-fpm.d

[root@centos-01inux etc]# cd php-fpm.d/

创建php-fpm子配置文件: [root@centos-01 php-fpm.d]# vim www.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

[root@centos-01 php-fpm.d]# vim aming.conf [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

检查语法错误、重新加载: [root@centos-01 php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t [15-May-2018 11:40:22] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[root@centos-01 php-fpm.d]# /etc/init.d/php-fpm restart Gracefully shutting down php-fpm . done Starting php-fpm done 配置完成后使用ps查看php-fpm进程信息。 [root@centos-01 php-fpm.d]# ps aux |grep php-fpm

12.22 php-fpm的慢执行日志

开启慢执行日志:

[root@centos-01inux php-fpm.d]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf …… request_slowlog_timeout = 1 #当请求超过1秒开始记录日志 slowlog = /usr/local/php-fpm/var/log/www-slow.log #日志存放地址

[root@centos-01inux php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t [root@centos-01inux php-fpm.d]# /etc/init.d/php-fpm reload 试验

在使用www pool的站点添加文件:

创建一个.php文件: [root@centos-01inux php-fpm.d]# vim /data/wwwroot/test.com/sleep.php <?php echo "test slow log"; sleep(2); echo "done"; ?> 检测:

[root@centos-01inux php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php test slow logdone 查看慢日志:

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

[16-Aug-2017 17:14:55] [pool www] pid 6451 script_filename = /data/wwwroot/test.com/sleep.php [0x00007fe02560e2e0] sleep() /data/wwwroot/test.com/sleep.php:3

12.23 php-fpm中指定open_basedir

当一台服务器跑多个站点时,使用open_basedir限定各个站点所能访问的服务器上的目录的范围。在php-fpm服务中,可以针对每个pool设定open _ basedir。

核心配置参数:

[root@centos-01inux ~]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf
…… php_admin_value[open_basedir]=/data/wwwroot/test.com:/tmp/ 创建测试PHP脚本:

[root@centos-01inux php-fpm.d]# vim /data/wwwroot/test.com/1.php <?php echo "This is a test php of open_basedir"; 测试:

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

12.24 php-fpm进程管理

php-fpm中pool配置参数解析:

[root@centos-01inux php-fpm.d]# vim www.conf [www] listen = /tmp/php-fcgi.sock listen.mode = 666 user = php-fpm group = php-fpm pm = dynamic ;定义进程启动方式(dynamic表示动态,static表示静态) ;只有此处设置为dynamic,下面的配置才生效 pm.max_children = 50 ;最多可启动的子进程数量 pm.start_servers = 20 ;设定初始启动的进程数量 pm.min_spare_servers = 5 ;表示php-fpm空闲时最少要有几个子进程 pm.max_spare_servers = 35 ;表示php-fpm空闲时最多要有几个子进程 pm.max_requests = 500 ;表示一个子进程最多可接受多少个请求 rlimit_files = 1024 ;表示每个子进程打开的多少个文件句柄 request_slowlog_timeout = 1 ;当请求超过1秒开始记录日志 slowlog = /usr/local/php-fpm/var/log/www-slow.log ;日志存放地址 php_admin_value[open_basedir]=/data/wwwroot/test.com:/tmp/