12.21 php-fpm的pool
为了避免因多站点使用同一个pool时因一个站点故障导致pool出问题,进而影响使用同一个pool的其他站点的正常运行,要对每个站点配置一个单独的pool。
为php-fpm增加pool
[root@cham002 cham.com]# cd /usr/local/php-fpm/etc/
[root@cham002 etc]# ls
pear.conf php-fpm.conf php-fpm.conf.default php.ini
[root@cham002 etc]# cat 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
[root@cham002 etc]# vi !$
vi php-fpm.conf
[champin.com]
#listen = /tmp/champin.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@cham002 etc]# /usr/local/php-fpm/sbin/php-fpm -t
[09-Jan-2018 00:36:35] ERROR: [pool champin.com] unable to set listen address as it's already used in another pool 'www'
[09-Jan-2018 00:36:35] ERROR: failed to post process the configuration
[09-Jan-2018 00:36:35] ERROR: FPM initialization failed
原因是我以为名字不对,后来换了名字还是不行,仔细看配置文件发现原因
[root@cham002 etc]# /usr/local/php-fpm/sbin/php-fpm -t
[09-Jan-2018 00:45:20] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
[root@cham002 etc]# /etc/init.d/php-fpm reload
Reload service php-fpm done
[root@cham002 etc]# ps aux |grep php-fpm
root 6251 0.5 0.4 227212 4980 ? Ss 00:49 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm 6252 0.0 0.4 227152 4712 ? S 00:49 0:00 php-fpm: pool www
php-fpm 6253 0.0 0.4 227152 4712 ? S 00:49 0:00 php-fpm: pool www
php-fpm 6254 0.0 0.4 227152 4712 ? S 00:49 0:00 php-fpm: pool www
php-fpm 6255 0.0 0.4 227152 4712 ? S 00:49 0:00 php-fpm: pool www
php-fpm 6256 0.0 0.4 227152 4716 ? S 00:49 0:00 php-fpm: pool www
php-fpm 6257 0.0 0.4 227152 4716 ? S 00:49 0:00 php-fpm: pool www
php-fpm 6258 0.0 0.4 227152 4716 ? S 00:49 0:00 php-fpm: pool www
php-fpm 6259 0.0 0.4 227152 4720 ? S 00:49 0:00 php-fpm: pool www
php-fpm 6260 0.0 0.4 227152 4720 ? S 00:49 0:00 php-fpm: pool www
php-fpm 6261 0.0 0.4 227152 4720 ? S 00:49 0:00 php-fpm: pool www
php-fpm 6262 0.0 0.4 227152 4720 ? S 00:49 0:00 php-fpm: pool www
php-fpm 6263 0.0 0.4 227152 4720 ? S 00:49 0:00 php-fpm: pool www
php-fpm 6264 0.0 0.4 227152 4720 ? S 00:49 0:00 php-fpm: pool www
php-fpm 6265 0.0 0.4 227152 4720 ? S 00:49 0:00 php-fpm: pool www
php-fpm 6266 0.0 0.4 227152 4720 ? S 00:49 0:00 php-fpm: pool www
php-fpm 6267 0.0 0.4 227152 4720 ? S 00:49 0:00 php-fpm: pool www
php-fpm 6268 0.0 0.4 227152 4720 ? S 00:49 0:00 php-fpm: pool www
php-fpm 6269 0.0 0.4 227152 4720 ? S 00:49 0:00 php-fpm: pool www
php-fpm 6270 0.0 0.4 227152 4720 ? S 00:49 0:00 php-fpm: pool www
php-fpm 6271 0.0 0.4 227152 4720 ? S 00:49 0:00 php-fpm: pool www
php-fpm 6272 0.0 0.4 227152 4716 ? S 00:49 0:00 php-fpm: pool champ
php-fpm 6273 0.0 0.4 227152 4716 ? S 00:49 0:00 php-fpm: pool champ
php-fpm 6274 0.0 0.4 227152 4716 ? S 00:49 0:00 php-fpm: pool champ
php-fpm 6275 0.0 0.4 227152 4720 ? S 00:49 0:00 php-fpm: pool champ
php-fpm 6276 0.0 0.4 227152 4724 ? S 00:49 0:00 php-fpm: pool champ
php-fpm 6277 0.0 0.4 227152 4724 ? S 00:49 0:00 php-fpm: pool champ
php-fpm 6278 0.0 0.4 227152 4724 ? S 00:49 0:00 php-fpm: pool champ
php-fpm 6279 0.0 0.4 227152 4724 ? S 00:49 0:00 php-fpm: pool champ
php-fpm 6280 0.0 0.4 227152 4724 ? S 00:49 0:00 php-fpm: pool champ
php-fpm 6281 0.0 0.4 227152 4724 ? S 00:49 0:00 php-fpm: pool champ
php-fpm 6282 0.0 0.4 227152 4724 ? S 00:49 0:00 php-fpm: pool champ
php-fpm 6283 0.0 0.4 227152 4724 ? S 00:49 0:00 php-fpm: pool champ
php-fpm 6284 0.0 0.4 227152 4724 ? S 00:49 0:00 php-fpm: pool champ
php-fpm 6285 0.0 0.4 227152 4724 ? S 00:49 0:00 php-fpm: pool champ
php-fpm 6286 0.0 0.4 227152 4724 ? S 00:49 0:00 php-fpm: pool champ
php-fpm 6287 0.0 0.4 227152 4724 ? S 00:49 0:00 php-fpm: pool champ
php-fpm 6288 0.0 0.4 227152 4724 ? S 00:49 0:00 php-fpm: pool champ
php-fpm 6289 0.0 0.4 227152 4724 ? S 00:49 0:00 php-fpm: pool champ
php-fpm 6290 0.0 0.4 227152 4724 ? S 00:49 0:00 php-fpm: pool champ
php-fpm 6291 0.0 0.4 227152 4728 ? S 00:49 0:00 php-fpm: pool champ
root 6293 1.0 0.0 112680 976 pts/1 R+ 00:49 0:00 grep --color=auto php-fpm
为站点配置pool
[root@cham002 etc]# cd /usr/local/nginx/conf/vhost/
[root@cham002 vhost]# ls
aaa.com.conf load.conf proxy.conf ssl.conf test.com.conf
[root@cham002 vhost]# vim aaa.com.conf
server
{
listen 80 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/champ.sock;
#fastcgi_pass 127.0.0.1:9000;
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@cham002 vhost]# cd /usr/local/php-fpm/etc/
[root@cham002 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
include = etc/php-fpm.d/*.conf
说明: 在全局变量版块添加参数“include = etc/php-fpm.d/*.conf”。然后可以清除php-fpm配置文件中其他参数,再到php-fpm.d目录下进行单独设置。
[root@cham002 vhost]# cd /usr/local/php-fpm/etc/
[root@cham002 etc]# mkdir php-fpm.d
[root@cham002 etc]# cd php-fpm.d/
[root@cham002 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@cham002 php-fpm.d]# ls
www.conf
[root@cham002 php-fpm.d]# vim champ.conf
[champ]
listen = /tmp/champ.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@cham002 php-fpm.d]# ls
champ.conf www.conf
[root@cham002 php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t
[09-Jan-2018 01:20:26] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
[root@cham002 php-fpm.d]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@cham002 php-fpm.d]# ps aux |grep php-fpm
root 6508 1.5 0.4 227240 4988 ? Ss 01:26 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm 6509 0.0 0.4 227180 4720 ? S 01:26 0:00 php-fpm: pool champ
php-fpm 6510 0.0 0.4 227180 4720 ? S 01:26 0:00 php-fpm: pool champ
php-fpm 6511 0.0 0.4 227180 4720 ? S 01:26 0:00 php-fpm: pool champ
php-fpm 6512 0.0 0.4 227180 4720 ? S 01:26 0:00 php-fpm: pool champ
php-fpm 6513 0.0 0.4 227180 4724 ? S 01:26 0:00 php-fpm: pool champ
php-fpm 6514 0.0 0.4 227180 4724 ? S 01:26 0:00 php-fpm: pool champ
php-fpm 6515 0.0 0.4 227180 4724 ? S 01:26 0:00 php-fpm: pool champ
php-fpm 6516 0.0 0.4 227180 4724 ? S 01:26 0:00 php-fpm: pool champ
php-fpm 6517 0.0 0.4 227180 4724 ? S 01:26 0:00 php-fpm: pool champ
php-fpm 6518 0.0 0.4 227180 4724 ? S 01:26 0:00 php-fpm: pool champ
php-fpm 6519 0.0 0.4 227180 4724 ? S 01:26 0:00 php-fpm: pool champ
php-fpm 6520 0.0 0.4 227180 4724 ? S 01:26 0:00 php-fpm: pool champ
php-fpm 6521 0.0 0.4 227180 4724 ? S 01:26 0:00 php-fpm: pool champ
php-fpm 6522 0.0 0.4 227180 4724 ? S 01:26 0:00 php-fpm: pool champ
php-fpm 6523 0.0 0.4 227180 4724 ? S 01:26 0:00 php-fpm: pool champ
php-fpm 6524 0.0 0.4 227180 4728 ? S 01:26 0:00 php-fpm: pool champ
php-fpm 6525 0.0 0.4 227180 4728 ? S 01:26 0:00 php-fpm: pool champ
php-fpm 6526 0.0 0.4 227180 4728 ? S 01:26 0:00 php-fpm: pool champ
php-fpm 6527 0.0 0.4 227180 4728 ? S 01:26 0:00 php-fpm: pool champ
php-fpm 6528 0.0 0.4 227180 4728 ? S 01:26 0:00 php-fpm: pool champ
php-fpm 6529 0.0 0.4 227180 4724 ? S 01:26 0:00 php-fpm: pool www
php-fpm 6530 0.0 0.4 227180 4724 ? S 01:26 0:00 php-fpm: pool www
php-fpm 6531 0.0 0.4 227180 4724 ? S 01:26 0:00 php-fpm: pool www
php-fpm 6532 0.0 0.4 227180 4724 ? S 01:26 0:00 php-fpm: pool www
php-fpm 6533 0.0 0.4 227180 4728 ? S 01:26 0:00 php-fpm: pool www
php-fpm 6534 0.0 0.4 227180 4728 ? S 01:26 0:00 php-fpm: pool www
php-fpm 6535 0.0 0.4 227180 4728 ? S 01:26 0:00 php-fpm: pool www
php-fpm 6536 0.0 0.4 227180 4728 ? S 01:26 0:00 php-fpm: pool www
php-fpm 6537 0.0 0.4 227180 4728 ? S 01:26 0:00 php-fpm: pool www
php-fpm 6538 0.0 0.4 227180 4728 ? S 01:26 0:00 php-fpm: pool www
php-fpm 6539 0.0 0.4 227180 4728 ? S 01:26 0:00 php-fpm: pool www
php-fpm 6540 0.0 0.4 227180 4732 ? S 01:26 0:00 php-fpm: pool www
php-fpm 6541 0.0 0.4 227180 4732 ? S 01:26 0:00 php-fpm: pool www
php-fpm 6542 0.0 0.4 227180 4732 ? S 01:26 0:00 php-fpm: pool www
php-fpm 6543 0.0 0.4 227180 4732 ? S 01:26 0:00 php-fpm: pool www
php-fpm 6544 0.0 0.4 227180 4732 ? S 01:26 0:00 php-fpm: pool www
php-fpm 6545 0.0 0.4 227180 4732 ? S 01:26 0:00 php-fpm: pool www
php-fpm 6546 0.0 0.4 227180 4732 ? S 01:26 0:00 php-fpm: pool www
php-fpm 6547 0.0 0.4 227180 4732 ? S 01:26 0:00 php-fpm: pool www
php-fpm 6548 0.0 0.4 227180 4732 ? S 01:26 0:00 php-fpm: pool www
root 6550 0.0 0.0 112680 976 pts/1 R+ 01:26 0:00 grep --color=auto php-fpm
配置完成后使用ps查看php-fpm进程信息。
12.22 php-fpm的慢执行日志
开启慢执行日志:
[root@cham002 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
request_slowlog_timeout = 1
#当请求超过1秒开始记录日志
slowlog = /usr/local/php-fpm/var/log/www-slow.log
#日志存放路径
试验
在使用www pool的站点添加文件:
[root@cham002 php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t
[09-Jan-2018 01:31:57] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
[root@cham002 php-fpm.d]# /etc/init.d/php-fpm reload
Reload service php-fpm done
[root@cham002 php-fpm.d]# ls /usr/local/php-fpm/var/log/www-slow.log
/usr/local/php-fpm/var/log/www-slow.log
[root@cham002 php-fpm.d]# vim /data/wwwroot/test.com/sleep.php
<?php
echo "test slow log";
sleep(2);
echo "done";
?>
试一下
[root@cham002 php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php
<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@cham002 php-fpm.d]# cat /usr/local/nginx/conf/vhost/test.com.conf
server
{
listen 80;
#报502的错误,查看配置文件,发现监听的地址被注重掉了
[root@cham002 php-fpm.d]# cat /usr/local/nginx/conf/vhost/test.com.conf
server
{
listen 80;
server_name test.com test2.com test3.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
if ($host != 'test.com' ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
# location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
# {
# expires 7d;
# access_log off;
# }
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$
{
expires 7d;
valid_referers none blocked server_names *.test.com ;
if ($invalid_referer) {
return 403;
}
access_log off;
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
location /admin/
{
allow 127.0.0.1;
allow 192.168.230.135;
deny all;
}
location ~ .*(upload|image)/.*\.php$
{
deny all;
}
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
{
return 403;
}
location ~ \.php$
{
include fastcgi_params;
#fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
}
access_log /tmp/test.com.log cham;
}
[root@cham002 php-fpm.d]# /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@cham002 php-fpm.d]# /usr/local/nginx/sbin/nginx -s reload
[root@cham002 php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php
test slow logdone
Reload service php-fpm done
[root@cham002 php-fpm.d]# cat /usr/local/php-fpm/var/log/www-slow.log
##查看日志!!!!!
[09-Jan-2018 01:50:17] [pool www] pid 6700
script_filename = /data/wwwroot/test.com/sleep.php
[0x00007f98c94b02f8] sleep() /data/wwwroot/test.com/sleep.php:3
日志中表明详细信息
说明:一般会写成2秒,多数php脚本都会超过1秒
12.23 php-fpm中指定open_basedir
当一台服务器跑多个站点时,使用open_basedir限定各个站点所能访问的服务器上的目录的范围。在php-fpm服务中,可以针对每个pool设定open _ basedir。
核心配置参数:
[root@cham002 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
request_slowlog_timeout = 1
slowlog = /usr/local/php-fpm/var/log/www-slow.log
php_admin_value[open_basedir]=/data/wwwroot/test.com:/tmp/
[root@cham002 php-fpm.d]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@cham002 php-fpm.d]# curl -x127.0.0.1:80 test.com/3.php -I
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Mon, 08 Jan 2018 18:05:28 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30
[root@cham002 php-fpm.d]# vi /usr/local/php-fpm/etc/php.ini
[root@cham002 php-fpm.d]# grep error_log /usr/local/php-fpm/etc/php.ini
; server-specific log, STDERR, or a location specified by the error_log
; Set maximum length of log_errors. In error_log information about the source is
;error_log = php_errors.log
;error_log = syslog
error_log = /usr/local/php-fpm/var/log/php_errors.log
; OPcache error_log file name. Empty string assumes "stderr".
;opcache.error_log=
[root@cham002 php-fpm.d]# ls /usr/local/php-fpm/var/log/
php-fpm.log www-slow.log
[root@cham002 php-fpm.d]# vim www.conf 故意写错地址
[root@cham002 php-fpm.d]# touch /usr/local/php-fpm/var/log/php_errors.log
[root@cham002 php-fpm.d]# chmod 777 /usr/local/php-fpm/var/log/php_errors.log
[root@cham002 php-fpm.d]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@cham002 php-fpm.d]# curl -x127.0.0.1:80 test.com/3.php
No input file specified.
[root@cham002 php-fpm.d]# curl -x127.0.0.1:80 test.com/3.php -I
HTTP/1.1 404 Not Found
Server: nginx/1.12.1
Date: Mon, 08 Jan 2018 18:17:50 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30
[root@cham002 php-fpm.d]# cat /usr/local/php-fpm/var/log/php_errors.log
[08-Jan-2018 18:17:44 UTC] PHP Warning: Unknown: open_basedir restriction in effect. File(/data/wwwroot/test.com/3.php) is not within the allowed path(s): (/data/wwwroot/etest.com:/tmp/) in Unknown on line 0
[08-Jan-2018 18:17:44 UTC] PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line 0
[08-Jan-2018 18:17:50 UTC] PHP Warning: Unknown: open_basedir restriction in effect. File(/data/wwwroot/test.com/3.php) is not within the allowed path(s): (/data/wwwroot/etest.com:/tmp/) in Unknown on line 0
[08-Jan-2018 18:17:50 UTC] PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line 0
改正之后再来测试
[root@cham002 php-fpm.d]# vim www.conf
[root@cham002 php-fpm.d]# curl -x127.0.0.1:80 test.com/3.php -I
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Mon, 08 Jan 2018 18:21:54 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30
12.24 php-fpm进程管理
php-fpm中pool配置参数解析:
[root@cham002 php-fpm.d]# cat 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
;定义进程启动方式(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/