php-fpm的pool
当很多php站点共用一个池的时候,若是其中一个站点影响了这个pool,会导致其他站点也出现问题,为避免这个问题发生,需要对每个站点新建一个pool,进行隔离,保障服务安全
1、[root@centos7 ~]# vim /usr/local/php-fpm/etc/php-fpm.conf
新增:
[test.com]
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
2、[root@centos7 ~]# /usr/local/php-fpm/sbin/php-fpm -t
3、[root@centos7 ~]# /etc/init.d/php-fpm reload
4、[root@centos7 ~]# vim /usr/local/nginx/conf/vhost/aaa.com.conf
5、新增如下:
[root@centos7 ~]# vim /usr/local/nginx/conf/vhost/aaa.com.conf.bak
server
{
listen 80 default_server;
#有'default_server'标记的就是默认虚拟主机
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/test.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/default$fastcgi_script_name;
}
}
6、[root@centos7 ~]# /etc/init.d/nginx reload
7、可以看出有两个pool了
[root@centos7 ~]# ps aux |grep php-fpm
php-fpm 8403 0.0 0.2 227144 4808 ? S 17:22 0:00 php-fpm: pool www
php-fpm 8423 0.0 0.2 227144 4812 ? S 17:22 0:00 php-fpm: pool test.com
添加php-fpm.conf子配置文件
为了方便管理php-fpm.conf上配置文件,类似于nginx上的虚拟主机conf,也可以分开填写文件
1、
[root@centos7 php-fpm.d]# vi /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 = /usr/local/php-fpm.d/*.conf
2、
[root@centos7 php-fpm.d]# vim /usr/local/php-fpm.d/test.conf
[test.com]
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
3、
[root@centos7 php-fpm.d]# vim /usr/local/php-fpm.d/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
4、
[root@centos7 php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t
5、
[root@centos7 php-fpm.d]# /etc/init.d/php-fpm reload
验证:是否有2个pool
[root@centos7 php-fpm.d]# ps -ef | grep php-fpm
php-fpm慢执行日志
当php访问变慢时,可以通过慢日志来查看是哪个php脚本导致,类似mysql的慢日志
1、新增内容
[root@centos7 php-fpm.d]# vim /usr/local/php-fpm.d/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
request_slowlog_timeout = 1
#请求超过1秒就记录到日志里
slowlog = /usr/local/php-fpm/var/log/www-slow.log
2、实验脚本编写
[root@centos7 php-fpm.d]# vim /data/wwwroot/test.com/sleep.php
#输出test slow log,sleep2秒,结束
<?php
echo "test slow log";
sleep(2);
echo "done";
?>
[root@centos7 php-fpm.d]# /etc/init.d/php-fpm reload
3、验证:
[root@centos7 php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php
test slow logdone[root@cen
[root@centos7 php-fpm.d]# tail /usr/local/php-fpm/var/log/www-slow.log
#记录了日志
[16-Nov-2017 17:41:59] [pool www] pid 8669
script_filename = /data/wwwroot/test.com/sleep.php
[0x00007f15620aa2e8] sleep() /data/wwwroot/test.com/sleep.php:3
open_basedir设置
当一台服务器跑多个站点时,使用open_basedir限定各个站点所能访问的服务器上的目录的范围。在php-fpm服务中,可以针对每个pool设定open _ basedir。
1、新增内容
[root@centos7 php-fpm.d]# vim /usr/local/php-fpm.d/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
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/
2、
[root@centos7 php-fpm.d]# vim /data/wwwroot/test.com/1.php
<?php
echo "This is open_basedir";
3、验证:
[root@centos7 php-fpm.d]# /etc/init.d/php-fpm reload
Reload service php-fpm done
[root@centos7 php-fpm.d]# curl -x127.0.0.1:80 test.com/1.php
This is open_basedir
php-fpm进程管理
php-fpm中pool配置参数解析:
[root@adailinux 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/