12.21 php-fpm的pool
[root@hyc-01-01 ~]# cd /usr/local/php-fpm/etc
[root@hyc-01-01 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] 用户可以在php-fpm配置文件中监听多个pool,不同的pool可以监听不同的端口或socket
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
nginx有多个站点,若只有一个php的pool,当某个站点或某些站点将pool的资源耗尽,就会导致其他使用该pool的站点出现502(可能php资源耗尽)错误
最好将每个站点隔离,为每个站点设置单独的pool
在php配置文件中增加新的pool:
[root@hyc-01-01 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
/在php-fpm配置文件中增加以下配置/
[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[test]
listen = /tmp/hyc.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@hyc-01-01 etc]# /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@hyc-01-01 etc]# /etc/init.d/php-fpm reload 重启服务
Reload service php-fpm done
[root@hyc-01-01 etc]# ps aux|grep php-fpm
…
php-fpm 64635 0.0 0.4 227240 4716 ? S 20:03 0:00 php-fpm: pool www
php-fpm 64636 0.0 0.4 227240 4716 ? S 20:03 0:00 php-fpm: pool www
php-fpm 64637 0.0 0.4 227240 4712 ? S 20:03 0:00 php-fpm: pool test
php-fpm 64638 0.0 0.4 227240 4712 ? S 20:03 0:00 php-fpm: pool test
php-fpm 64639 0.0 0.4 227240 4712 ? S 20:03 0:00 php-fpm: pool test
…
可以发现php-fpm多了名为test的pool
将两个站点隔离在两个pool中:
[root@hyc-01-01 vhost]# vim test.com.conf
…
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock; 这里指定监听的是www的socket地址
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
}
…
[root@hyc-01-01 vhost]# vim aaa.com.conf
server
{
listen 80;
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/hyc.sock; 指定监听test的socket地址
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/default$fastcgi_script_name;
}
}
一个pool中有多个php进程提供服务,当请求过多时,pool中的所有php进程都处于忙状态时,则没有php进程能为新的请求提供服务,此时用户浏览器会报502错误,如果该pool仅为一个站点提供服务,那么其他pool的站点将不受影响
可以在php-fpm的主配置文件中增加以下配置,使每个pool单独写一个配置文件,类似nginx的vhost:
[root@hyc-01-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
include = etc/php-fpm.d/*.conf 这里include行必须在最后,否则-t会报错
[root@hyc-01-01 etc]# mkdir php-fpm.d 创建存放每个pool的配置文件的目录
[root@hyc-01-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@hyc-01-01 php-fpm.d]# vim test.conf
[test]
listen = /tmp/hyc.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
12.22 php-fpm慢执行日志
做php的网站,建议使用LNMP,这样可以分析php-fpm的慢执行日志,当php网站运行慢时可以分析网站为什么慢
配置:
[root@hyc-01-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
request_slowlog_timeout = 1 php网站访问耗时超过1s记录日志
slowlog = /usr/local/php-fpm/var/log/www.slow.log 慢执行日志记录路径
检测并重启服务:
[root@hyc-01-01 php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t
[19-Aug-2018 22:04:33] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
[root@hyc-01-01 php-fpm.d]# /etc/init.d/php-fpm reload
[root@hyc-01-01 php-fpm.d]# ls /usr/local/php-fpm/var/log
php-fpm.log www.slow.log 生成了慢执行日志
测试生成慢执行日志过程:
[root@hyc-01-01 php-fpm.d]# cat /usr/local/nginx/conf/vhost/test.com.conf
…
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock; 该站点监听该socket地址
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
}
}
[root@hyc-01-01 vhost]# curl -x127.0.0.1:80 test.com/slow.php 执行测试脚本
test slow logdone
[root@hyc-01-01 php-fpm.d]# cat /usr/local/php-fpm/var/log/www.slow.log 查看生成的慢执行日志
[19-Aug-2018 22:35:25] [pool www] pid 64965
script_filename = /data/wwwroot/test.com/slow.php 执行慢的文件
[0x00007fcb453872d0] sleep() /data/wwwroot/test.com/slow.php:3 具体执行慢的行号
12.23 open_basedir
当服务器上有多个网站时,不适合在php.ini文件中定义openbasedir,可以在虚拟主机配置文件中定义或在php-fpm中定义
[root@hyc-01-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
request_slowlog_timeout = 1
slowlog = /usr/local/php-fpm/var/log/www.slow.log
php_admin_value[open_basedir]=/data/wwwroot/test.com:/tmp/
openbasedir路径一定要定义正确否则无法访问
故意将open_basedir定义错误:
测试:
[root@hyc-01-01 php-fpm.d]# curl -x127.0.0.1:80 test.com/3.php
No input file specified.
php-fpm错误日志:
[root@hyc-01-01 test.com]# vim /usr/local/php-fpm/etc/php.ini
447 ; Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT
448 ; http://php.net/error-reporting
449 error_reporting = E_ALL 定义错误日志级别
450
451 ; This directive controls whether or not and where PHP will output errors,
…
464 ; Production Value: Off
465 ; http://php.net/display-errors
466 display_errors = off 一般设置为off,否则用户可以通过浏览器看到错误信息
467
468 ; The display of errors which occur during PHP's startup sequence are handled
…
486 ; http://php.net/log-errors
487 log_errors = On 开启错误日志记录功能
488
489 ; Set maximum length of log_errors. In error_log information about the source is
…
572 ;error_log = php_errors.log
573 ; Log errors to syslog (Event Log on Windows).
574 ;error_log = syslog
575 error_log = /usr/local/php-fpm/var/log/php_errors.log 输出日志的路径
576
…
为确保php-fpm错误日志文件能正常生成,需要检查生成错误日志的路径是否存在:
[root@hyc-01-01 ~]# cd /usr/local/php-fpm/var/log/
[root@hyc-01-01 log]# pwd
/usr/local/php-fpm/var/log
创建错误日志文件:
[root@hyc-01-01 log]# touch php_errors.log
[root@hyc-01-01 log]# chmod 777 php_errors.log 为防止错误日志无法正常写入,修改成较高的权限
故意将www.conf中的open_basedir路径改错:
[root@hyc-01-01 php-fpm.d]# vim www.conf
…
php_admin_value[open_basedir]=/data/wwwroot/aaa.com:/tmp/
[root@hyc-01-01 test.com]# curl -x127.0.0.1:80 test.com/3.php
No input file specified.
[root@hyc-01-01 test.com]# curl -x127.0.0.1:80 test.com/3.php -I
HTTP/1.1 404 Not Found
Server: nginx/1.12.2
Date: Mon, 20 Aug 2018 12:30:06 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30
查看错误日志:
[root@hyc-01-01 php-fpm.d]# cat /usr/local/php-fpm/var/log/php_errors.log
[20-Aug-2018 12:26:00 UTC] PHP Deprecated: Comments starting with '#' are deprecated in Unknown on line 1 in Unknown on line 0
[20-Aug-2018 12:26:00 UTC] PHP Deprecated: Comments starting with '#' are deprecated in Unknown on line 1 in Unknown on line 0
[20-Aug-2018 12:30:00 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/aaa.com:/tmp/) in Unknown on line 0
[20-Aug-2018 12:30:00 UTC] PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line 0
[20-Aug-2018 12:30:06 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/aaa.com:/tmp/) in Unknown on line 0
3.php文件在test.com路径下,而open_basedir路径限制在aaa.com路径下
[20-Aug-2018 12:30:06 UTC] PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line 0
12.24 php-fpm进程管理
[root@hyc-01-01 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表示动态,此处只有设置了dynamic后面的pm相关参数才会生效
若将dynamic修改为static则只有pm.max_children会生效,表示启动时就会生成50个进程
pm.max_children = 50
pm.start_servers =20
刚开始启动20个进程,后期再根据访问量增加,如果服务器较空闲时也会自动销毁进程
pm.min_spare_servers =5
若php-fpm进程空闲就会销毁部分进程,但至少会保留5个子进程
pm.max_spare_servers =35
空闲态下子进程最大值,多于35就会开始销毁子进程
pm.max_requests =500
一个子进程最多处理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/
修改子进程配置文件:
[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 = 20
pm.start_servers =10
pm.min_spare_servers =8
pm.max_spare_servers =15
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@hyc-01-01 php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t
[20-Aug-2018 22:03:50] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
[root@hyc-01-01 php-fpm.d]# /etc/init.d/php-fpm reload
Reload service php-fpm done
[root@hyc-01-01 php-fpm.d]# ps aux|grep php-fpm
…
php-fpm 1419 0.0 0.4 227232 4808 ? S 22:03 0:00 php-fpm: pool www
php-fpm 1420 0.0 0.4 227232 4812 ? S 22:03 0:00 php-fpm: pool www
php-fpm 1421 0.0 0.4 227232 4812 ? S 22:03 0:00 php-fpm: pool www
php-fpm 1422 0.0 0.4 227232 4812 ? S 22:03 0:00 php-fpm: pool www
php-fpm 1423 0.0 0.4 227232 4816 ? S 22:03 0:00 php-fpm: pool www
php-fpm 1424 0.0 0.4 227232 4816 ? S 22:03 0:00 php-fpm: pool www
php-fpm 1425 0.0 0.4 227232 4816 ? S 22:03 0:00 php-fpm: pool www
php-fpm 1426 0.0 0.4 227232 4816 ? S 22:03 0:00 php-fpm: pool www
php-fpm 1427 0.0 0.4 227232 4816 ? S 22:03 0:00 php-fpm: pool www
php-fpm 1428 0.0 0.4 227232 4816 ? S 22:03 0:00 php-fpm: pool www
在php-fpm刚启动时www派生了10个子进程