php-fpm的pool
为了避免因多站点使用同一个pool时因一个站点故障导致pool出问题,进而影响使用同一个pool的其他站点的正常运行,要对每个站点配置一个单独的pool。
-
为php-fpm增加pool
编辑php-fpm配置文件:
......
[huang.com]
listen = /tmp/huang.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
......
//仿照之前的[www] pool修改的[huang.com]。检测并重载配置文件:
[root@localhost ~]# /usr/local/php-fpm/sbin/php-fpm -t
[09-Jan-2018 22:36:02] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful[root@localhost ~]# /etc/init.d/php-fpm reload
Reload service php-fpm done -
查看进程
[root@localhost ~]# ps aux | grep php-fpm
......
php-fpm 2569 0.0 0.3 227800 5936 ? S 22:36 0:00 php-fpm: pool www
php-fpm 2570 0.0 0.3 227800 5932 ? S 22:36 0:00 php-fpm: pool huang.com
...... -
为站点配置pool
[root@localhost ~]# vim /usr/local/nginx/conf/vhost/aaa.com.conf
......
location ~ .php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/huang.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/default$fastcgi_script_name;
}
//加入上面这段解析php的配置。把fastcgi_pass地址改为php-fpm。conf中一样的地址就可以。
......检测并重载配置:
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
添加php-fpm.conf子配置文件
为了便于管理,可以将php-fpm中的每个pool单独进行管理。进行如下操作,添加php-fpm子配置文件。
-
编辑php-fpm.conf
[root@localhost ~]# 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目录下进行单独设置。创建php-fpm子配置文件目录:
[root@localhost ~]# mkdir /usr/local/php-fpm/etc/php-fpm.d创建子配置文件
[root@localhost ~]# vim /usr/local/php-fpm/etc/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
//添加上面内容[root@localhost ~]# vim /usr/local/php-fpm/etc/php-fpm.d/huang.conf
[huang.com]
listen = /tmp/huang.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
//添加上面内容检测配置文件,并重载php-fpm服务:
[root@localhost ~]# /usr/local/php-fpm/sbin/php-fpm -t
[09-Jan-2018 23:18:55] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
[root@localhost ~]# /etc/init.d/php-fpm reload
Reload service php-fpm done -
测试
[root@localhost ~]# ps aux | grep php-fpm
......
php-fpm 2816 0.0 0.3 227836 5948 ? S 23:19 0:00 php-fpm: pool huang.com
php-fpm 2817 0.0 0.3 227836 5944 ? S 23:19 0:00 php-fpm: pool www
......
php-fpm 的慢执行日志
通过php-fpm慢日志,我们可以非常清晰的了解到php的脚本哪里执行时间长,它可以定位到具体的行。
-
开启慢执行日志
针对[www]pool开启慢执行日志:
[root@localhost ~]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf
......
request_slowlog_timeout = 1
#当请求超过1秒开始记录日志,一般写2秒
slowlog = /usr/local/php-fpm/var/log/www-slow.log
#日志存放地址
//添加上面两行内容检测配置文件,重启php-fpm服务:
[root@localhost ~]# /usr/local/php-fpm/sbin/php-fpm -t
[09-Jan-2018 23:38:14] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful[root@localhost ~]# /etc/init.d/php-fpm reload
Reload service php-fpm done查看慢执行日志文件是否生成:
[root@localhost ~]# ls /usr/local/php-fpm/var/log/www-slow.log
/usr/local/php-fpm/var/log/www-slow.log -
测试
使用[www]pool的站点添加文件
创建测试脚本文件:
[root@localhost ~]# vim /data/wwwroot/test.com/sleep.php
<?php
echo "test slow log";
sleep(2);
echo "done";
?>检测:
[root@localhost ~]# curl -x127.0.0.1:80 test.com/sleep.php
test slow logdone查看慢执行日志:
[09-Jan-2018 23:55:46] [pool www] pid 2992
script_filename = /data/wwwroot/test.com/sleep.php //提示哪个脚本慢
[0x00007f86ce814090] sleep() /data/wwwroot/test.com/sleep.php:3//脚本的第几行慢//php网站访问慢,可以查看慢执行日志,排查慢的原因。
-
打开php.ini文件diplay_errors后,可以在浏览器上查看具体报错,该操作不宜在正式上线系统中使用!
vim /usr/local/php-fpm/etc/php.ini
将display_errors = Off 改为display_errors = On
/etc/init.d/php-fpm reload执行命令时错误会直接输出在屏幕上。
php-fpm定义open_basedir
因为如果服务器管理多个网站,在php.ini里定义多个open_basedir就不合适了,所以要么在apache虚拟主机配置文件里面定义,要么在php-fpm配置文件里面定义,我们可以针对不同的池子(pool)定义对应的open_basedir。
-
配置open_basedir
针对[www]的pool进行配置
[root@localhost ~]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf
......
php_admin_value[open_basedir]=/data/wwwroot/test.com:/tmp/
......
//添加上面一行配置重启php-fpm:
[root@localhost ~]# /etc/init.d/php-fpm reload
Reload service php-fpm done -
测试
[root@localhost ~]# curl -x127.0.0.1:80 test.com/1.txt
test open_basedir
注意: 若open_basedir定义错误,就会导致无法访问,形成404状态码,输出curl直接连会提示“No input file specified.”
-
定义错误日志
display_errors = Off 正常情况下,在线上这个是off的,别人不能通过浏览器看到你的错误信息,而是把你的错误信息记录到服务器的某一个文件里
[root@localhost ~]# vi /usr/local/php-fpm/etc/php.ini
......
display_errors=Off //搜索display_errors 看是否为off,不是的话改为off。;error_log = syslog
error_log = /usr/local/php-fpm/var/log/php_error.log //搜索error_log,在下面添加一行,定义错误日志路径。586行搜索error_reporting
注释掉自带的error_reporting
;error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
error_reporting = E_ALL //定义错误日志的级别,所有 460行。
......
保存退出手动创建错误日志文件:
[root@localhost ~]# touch /usr/local/php-fpm/var/log/php_error.log
[root@localhost ~]# chmod 777 /usr/local/php-fpm/var/log/php_error.log
//防止蹦年正常写入 -
测试
为了方便测试,故意将/usr/local/php-fpm/etc/php-fpm.d/www.conf 中的open_basedir路径写错,来方便测试
[root@localhost ~]# curl -x127.0.0.1:80 test.com/sleep.php -I
HTTP/1.1 404 Not Found
Server: nginx/1.8.0
Date: Tue, 09 Jan 2018 16:57:28 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.1.6查看错误日志:
[root@localhost ~]# cat /usr/local/php-fpm/var/log/php_errors.log
[09-Jan-2018 16:57:28 UTC] PHP Warning: Unknown: open_basedir restriction in effect. File(/data/wwwroot/test.com/sleep.php) is not within the allowed path(s): (/data/wwwroot/wwtest.com:/tmp/) in Unknown on line 0
[09-Jan-2018 16:57:28 UTC] PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line 0
//错误提示访问有限制
php-fpm 进程管理
vim /usr/local/php-fpm/etc/php-fpm.d/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/
本文转自 豆渣锅 51CTO博客,原文链接:
http://blog.51cto.com/754599082/2059253