12.21 php-fpm的pool
php-fpm支持定义多个pool,每个pool可以监听不同的socket或者不同的tcp/ip;
如果nginx有多个不同的站点,那么每个站点都可以使用一个pool;
如果所有网站都使用了同一个pool,当其中给一个php资源不够了,或者其他原因导致502了,网站出问题了,那么所有站点都不能正常使用了;
因此我们有必要把不同的站点隔离开,使用不同pool。
设置多个pool:
[[email protected] ~]# vim /usr/local/php-fpm/etc/php-fpm.conf
[[email protected] ~]# /usr/local/php-fpm/sbin/php-fpm -t [17-Mar-2018 22:31:02] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful [[email protected] ~]# /etc/init.d/php-fpm reload Reload service php-fpm done [[email protected] ~]# ps aux|grep php-fpm
此时有两个pool能够使用,那么将另外一个站点定义在aming.com这个pool下
[[email protected] ~]# vim /usr/local/nginx/conf/vhost/aaa.com.conf
为了让不同的pool能够显示的更加清楚,可以把不同pool的参数信息分开到不同的conf中
[[email protected] ~]# cd /usr/local/php-fpm/etc/ [[email protected] etc]# mkdir php-fpm.d/ [[email protected] etc]# cd php-fpm.d/
然后将原先php-fpm.conf中定义的两个不同的pool差分开
[[email protected] php-fpm.d]# vim www.conf
[[email protected] php-fpm.d]# vim aming.conf
删除原先php-fpm.conf中pool的信息,并且在[global]中添加include = etc/php-fpm.d/*.conf
(这个类似于nginx中对不同vhost的配置方法)
[[email protected] php-fpm.d]# vim /usr/local/php-fpm/etc/php-fpm.conf
[[email protected] php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t [17-Mar-2018 23:00:48] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful [[email protected] php-fpm.d]# /etc/init.d/php-fpm restart Gracefully shutting down php-fpm . done Starting php-fpm done [[email protected] php-fpm.d]# ps aux|grep php-fpm
aming.com在www之前,因此pool生效了
12.22 php-fpm慢执行日志
目的:为了记录网站访问慢的原因,方便查找原因
·增加请求超时记录的配置:
[[email protected] ~]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf
[[email protected] ~]# /usr/local/php-fpm/sbin/php-fpm -t [18-Mar-2018 14:01:03] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful [[email protected] ~]# /etc/init.d/php-fpm reload Reload service php-fpm done [[email protected] ~]# ls /usr/local/php-fpm/var/log/ php-fpm.log www-slow.log
写一个php脚本,模拟超过1秒的php执行
因为php-fcgi.sock被test.com这个站点使用者,因此在test.com下创建cleep.php
[[email protected] ~]# vim /data/wwwroot/test.com/sleep.php
[[email protected] ~]# curl -x127.0.0.1:80 test.com/sleep.php test slow logdone[[email protected] ~]# cat /usr/local/php-fpm/var/log/www-slow.log [18-Mar-2018 14:51:15] [pool www] pid 1578 script_filename = /data/wwwroot/test.com/sleep.php [0x00007f39874b9280] sleep() /data/wwwroot/test.com/sleep.php:2
(上方显示sleep.php脚本的第2行慢)
·一般请求时间request_slowlog_timeout = 定义大于2秒,因为大部分php执行时间介于1~2秒之间,定义大于2秒可以过滤掉很多不需要的信息。
12.23 open_basedir
[[email protected] ~]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf
[[email protected] ~]# curl -x127.0.0.1:80 test.com/3.php -I HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Sun, 18 Mar 2018 07:11:24 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive X-Powered-By: PHP/5.6.30
·配置php-fpm错误日志:
·关闭显示错误:display_errors = Off
·选择记录日志级别:error_reportint = E_ALL
·打开错误日志开关:log_errors = On
·配置错误日志保存位置:error_log = /usr/local/php-fpm/var/log/php_errors.log
·错误日志路径如果不存在的话,需要重新创建
[[email protected] ~]# touch /usr/local/php-fpm/var/log/php_errors.log [[email protected] ~]# chmod 777 !$ chmod 777 /usr/local/php-fpm/var/log/php_errors.log
·我们把www.conf中配置open_basedir改错,继续访问,查看错误日志
[[email protected] ~]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf
[[email protected] ~]# /etc/init.d/php-fpm restart Gracefully shutting down php-fpm . done Starting php-fpm done [[email protected] ~]# curl -x127.0.0.1:80 test.com/3.php No input file specified. [[email protected] ~]# curl -x127.0.0.1:80 test.com/3.php -I HTTP/1.1 404 Not Found Server: nginx/1.12.2 Date: Sun, 18 Mar 2018 07:30:30 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive X-Powered-By: PHP/5.6.30 [[email protected] ~]# cat /usr/local/php-fpm/var/log/php_errors.log[object Object]
(路径不正确)
[[email protected] ~]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf
[[email protected] ~]# /etc/init.d/php-fpm restart Gracefully shutting down php-fpm . done Starting php-fpm done [[email protected] ~]# !curl curl -x127.0.0.1:80 test.com/3.php -I HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Sun, 18 Mar 2018 07:36:44 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive X-Powered-By: PHP/5.6.30
(可以访问)
12.24 php-fpm进程管理
·配置进程管理参数:
·pm = dynamic //动态进程管理,也可以是static
·pm.max_children = 50 //最大子进程数,ps aux可以查看
·pm.start_servers = 20 //启动服务时会启动的进程数
·pm.min_spare_servers = 5 //定义在空闲时段,子进程数的最少数量,如果达到这个数值时,php-fpm服务会自动派生新的子进程。
·pm.max_spare_servers = 35 //定义在空闲时段,子进程数的最大值,如果高于这个数值就开始清理空闲的子进程。
·pm.max_requests = 500 //定义一个子进程最多处理的请求数,也就是说在一个php-fpm的子进程最多可以处理这么多请求,当达到这个数值时,它会自动退出
[[email protected] ~]# cat /usr/local/php-fpm/etc/php-fpm.d/www.conf
pm=dynamic 动态,一开始先启动20个,之后根据需求去生成或者销毁子进程
如果把dynamic改为static静态,那么红框中配置将不再生效,启动直接生成50个进程
[[email protected] ~]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf
[[email protected] ~]# /etc/init.d/php-fpm restart Gracefully shutting down php-fpm . done Starting php-fpm done [[email protected] ~]# ps aux|grep php-fpm
(www这个pool有50个进程)
如有错误,欢迎指正,互相学习,共同进步!!!
原文地址:http://blog.51cto.com/11530642/2088737