摘要
2018年6月13日任务
12.21 php-fpm的pool 12.22 php-fpm慢执行日志 12.23 open_basedir 12.24 php-fpm进程管理12.21 php-fpm的pool
• vim /usr/local/php/etc/php-fpm.conf//在[global]部分增加
• include = etc/php-fpm.d/*.conf
• mkdir /usr/local/php/etc/php-fpm.d/
• cd /usr/local/php/etc/php-fpm.d/
• vim www.conf //内容如下
[www]
listen = /tmp/www.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
•
• 继续编辑配置文件
• vim aming.conf //内容如下
[aming]
listen = /tmp/aming.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
• /usr/local/php/sbin/php-fpm –t
• /etc/init.d/php-fpm restart
实例:
在/usr/local/php/etc/php-fpm.conf中添加test.com
查看定义的pid[root@localhost vhost]# cd /usr/local/php-fpm/etc/[root@localhost etc]# lspear.conf php-fpm.conf php-fpm.conf.default php.ini[root@localhost etc]# cat php-fpm.conf[global]pid = /usr/local/php-fpm/var/run/php-fpm.piderror_log = /usr/local/php-fpm/var/log/php-fpm.log[www]listen = /tmp/php-fcgi.sock#listen = 127.0.0.1:9000user = php-fpmgroup = php-fpmpm = dynamicpm.max_children = 50pm.start_services = 20pm.min_spare_services = 5pm.max_spare_service = 35pm.max_requests = 500rlimit_files = 1024[test.com]listen = /tmp/test.sock#listen = 127.0.0.1:9000user = php-fpmgroup = php-fpmpm = dynamicpm.max_children = 50pm.start_services = 20pm.min_spare_services = 5pm.max_spare_service = 35pm.max_requests = 500rlimit_files = 1024重新加载[root@localhost etc]# /usr/local/php-fpm/sbin/php-fpm -t[root@localhost etc]# /etc/local/php-fpm/sbin/php-fpm -s reload[root@localhost etc]# ps aux | grep php-fpm #查看添加的test.com[root@localhost etc]#
使用添加的test pool
[root@localhost etc]# cd /usr/local/nginx/conf/vhost/[root@localhost vhost]# lsaaa.com.conf ld.conf proxy.conf ssl.conf test.com.conf[root@localhost vhost]# vi aaa.com.confserver{ listen 80 default_server; server_name aaa.com; index index.html index.html index.php; root /data/wwwroot/default;location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/wzq.sock; # fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/wwwroot/default$fastcgi_script_name; }}[root@localhost vhost]# lsaaa.com.conf ld.conf proxy.conf ssl.conf test.com.conf[root@localhost vhost]# tail ../nginx.conf tcp_nodelay on; gzip on; gzip_min_length 1k; gzip_buffers 4 8k; gzip_com_level 5; gzip_http_version 1.1; gzip_types text/plain applition/x-javascript text/css text/htm; application/xml; include vhost/*.conf;
- vim /usr/local/php/etc/php-fpm.conf//在[global]部分增加
[root@localhost vhost]# cd /usr/local/php-fpm/etc/[root@localhost etc]#lspear.conf php-fpm.conf php-fpm.conf.default php.ini[root@localhost etc]#vi /usr/local/php/etc/php-fpm.conf/[global]pid = /usr/local/php-fpm/var/run/php-fpm.piderror_log = /usr/local/php-fpm/var/log/php-fpm.loginclude = /etc/php-fpm.d/*.conf #添加的内容,在www上面,在www行是100dd,删除下面的行[www][root@localhost etc]# mkdir php-fpm.d[root@localhost etc]# cd php-fpm.d/[root@localhost php-fpm.d]# vi www.conf[www]#listen = /tmp/php-fcgi.socklisten = 127.0.0.1:9000user = php-fpmgroup = php-fpmpm = dynamic #进程启动的形式pm.max_children = 50pm.start_services = 20pm.min_spare_services = 5pm.max_spare_service = 35pm.max_requests = 500rlimit_files = 1024[root@localhost etc]# vi test.conf[test.com] #只需要把括号里面的内容,更改成你想要的名字#listen = /tmp/test.socklisten = 127.0.0.1:9000user = php-fpmgroup = php-fpmpm = dynamicpm.max_children = 50pm.start_services = 20pm.min_spare_services = 5pm.max_spare_service = 35pm.max_requests = 500rlimit_files = 1024[root@localhost etc]# vi www.conf[www.com]#listen = /tmp/test.socklisten = 127.0.0.1:9000user = php-fpmgroup = php-fpmpm = dynamicpm.max_children = 50pm.start_services = 20pm.min_spare_services = 5pm.max_spare_service = 35pm.max_requests = 500rlimit_files = 1024[root@localhost etc]# lstest.conf www.conf[root@localhost ]# cat ../php-fpm.conf[global]pid = /usr/local/php-fpm/var/run/php-fpm.piderror_log = /usr/local/php-local/php-fpm/var/log/php-fpm.loginclude = /etc/php-fpm.d/*.conf检测[root@localhost etc]# /usr/local/php-fpm/sbin/php-fpm -t[root@localhost etc]# /etc/init.d/php-fpm restart[root@localhost etc]#ps aux |grep php-fpm #查看pool.添加的www
12.22 php-fpm慢执行日志
• vim /usr/local/php-fpm/etc/php-fpm.d/www.conf//加入如下内容
request_slowlog_timeout = 1 #日志记录的时候1秒
slowlog = /usr/local/php-fpm/var/log/www-slow.log #日志记录的位置
• 配置nginx的虚拟主机test.com.conf,把unix:/tmp/php-fcgi.sock改为unix:/tmp/www.sock
• 重新加载nginx服务
• vim /data/wwwroot/test.com/sleep.php//写入如下内容
• <?php echo “test slow log”;sleep(2);echo “done”;?>
• curl -x127.0.0.1:80 test.com/sleep.php
• cat /usr/local/php-fpm/var/log/www-slow.log
实例:
vim /usr/local/php-fpm/etc/php-fpm.d/www.conf//加入如下内容
[root@localhost etc]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf [www]#listen = /tmp/php-fcgi.socklisten = 127.0.0.1:9000user = php-fpmgroup = php-fpmpm = dynamic #进程启动的形式pm.max_children = 50pm.start_services = 20pm.min_spare_services = 5pm.max_spare_service = 35pm.max_requests = 500rlimit_files = 1024request_slowlog_timeout = 1 #添加的内容,,请求超过一秒钟,就记录日志。slowlog = /usr/local/php-fpm/var/log/www-slow.log #日志存放的位置#检查设置是否有误[root@localhost etc]# /usr/local/php-fpm/sbin/php-fpm -t[root@localhost etc]# /etc/init.d/php-fpm restart[root@localhost etc]# cat www.conf #查看日志有没有生成[root@localhost etc]# ls /usr/local/php-fpm/var/log/ #查看有没有生成 www-slow.logphp-fpm.log www-slow.log[root@localhost etc]# ls /usr/local/php-fpm/var/log/www-slow.log /usr/local/php-fpm/var/log/www-slow.log [root@localhost etc]# cat /usr/local/php-fpm/var/log/www-slow.log #查看内容cat /usr/local/php-fpm/var/log/www-slow.log #显示为空,因为没有执行php
配置nginx的虚拟主机test.com.conf,把unix:/tmp/php-fcgi.sock改为unix:/tmp/www.sock
[root@localhost ]# vi test.com.conf把unix:/tmp/php-fcgi.sock改为unix:/tmp/www.sock
设置慢执行日志
[root@localhost php-fpm.d]# vim /data/wwwroot/test.com/sleep.php #编写一个休眠2秒的php文件 [root@localhost php-fpm.d]# curl -x 127.0.0.1:80 test.com/sleep.php[root@localhost php-fpm.d]# curl -x 127.0.0.1:80 test.com/sleep.php -I 、#-I检测有没有语法错误....... 500 ........[root@localhost php-fpm.d]# vi /usr/local/php-fpm/etc/php.ini/display_err #搜索的内容/display_errors = On #设置成On,之后就可以在浏览器上面浏览 正常情况下,是off[root@localhost php-fpm.d]# /etc/init.dphp-fpm reload #加载[root@localhost php-fpm.d]# curl -x 127.0.0.1 test.com/sleep.php #重新链接[root@localhost php-fpm.d]# vi /data/wwwroot/test.com/sleep.php2.js 3.php admin/ index.html sleep.php upload[root@localhost php-fpm.d]# vi /data/wwwroot/test.com/sleep.php [root@localhost php-fpm.d]# curl -x 127.0.0.1 test.com/sleep.php #再执行一次[root@localhost php-fpm.d]# cat /usr/local/php-fpm/var/log/www-slow.log ##再查看一次有没有生成 慢执行日志.网站慢的时候,,,,查看/usr/local/php-fpm/var/log/www-slow.log [12 -Jul-2018 13:20:24] [pool www] pid 1223script_filename = /data/wwwroot/test.com/sleep.php[123v12h3h21h] sleep () /data/wwwroot/test.com/sleep.php:3 #那个脚本慢,第三行慢[root@localhost php-fpm.d]# cat /data/wwwroot/test.com/sleep.php
12.23 open_basedir
• vim /usr/local/php-fpm/etc/php-fpm.d/aming.conf//加入如下内容
php_admin_value[open_basedir]=/data/wwwroot/aming.com:/tmp/
v
• 创建测试php脚本,进行测试
• 再次更改aming.conf,修改路径,再次测试
• 配置错误日志
• 再次测试
• 查看错误日志
实例:
vim /usr/local/php-fpm/etc/php-fpm.d/aming.conf//加入如下内容
[root@localhost php-fpm.d]# vi www.conf//加入如下内容[www]#listen = /tmp/php-fcgi.socklisten = 127.0.0.1:9000user = php-fpmgroup = php-fpmpm = dynamic #进程启动的形式pm.max_children = 50pm.start_services = 20pm.min_spare_services = 5pm.max_spare_service = 35pm.max_requests = 500rlimit_files = 1024request_slowlog_timeout = 1 slowlog = /usr/local/php-fpm/var/log/www-slow.logphp_admin_value[open_basedir]=/data/wwwroot/test.com:/tmp/ #添加的内容[root@localhost php-fpm.d]# cat www.conf[root@localhost php-fpm.d]# netstat -lntp #查看80端口[root@localhost php-fpm.d]# vi /dta/wwwroot/test.com/3.php
查看错误日志
[root@localhost php-fpm.d]#vi /usr/local/php-fpm/etc/php.ini/display_error //搜索的内容/display_errors = Off //修改成原来的off/error_log //搜索的内容,,,在error_log = syslog下面添加error_log = /usr/local/php-fpm/var/log/php_errors.log/error_reporting //搜索的内容在;error_reporting = E_ALL & -E_DEPRECATED & -E_STRICT下面添加然后 把添加的哪一行上面的一行注释掉。error_reporting = E_ALL[root@localhost php-fpm.d]# grep error_log /usr/local/php-fpm/etc/php.ini #查看错误日志error_log = /usr/local/php-fpm/var/log/php_errors.logroot@localhost php-fpm.d]# touch /usr/local/php-fpm/var/log/php_errors.log手动生成一个错误日志root@localhost php-fpm.d]# chmod 777 /usr/local/php-fpm/var/log/php_errors.logroot@localhost php-fpm.d]#/etc/init.d/php-fpm restart #重启一下root@localhost php-fpm.d]# curl -x 127.0.0.1:80 test.com/3.php -I #重新访问root@localhost php-fpm.d]# cat /usr/local/php-fpm/var/log/php_errors.log #查看错误日志
注意:open_basedir 的作用是限制php在指定的目录里活动。
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的子进程最多可以处理这么多请求,当达到这个数值时,它会自动退出。
实例:
[root@localhost php-fpm.d]# vim /usr/local/php-fpm/etc/php-fpm.d/aming.conf//加入如下内容[www]#listen = /tmp/php-fcgi.socklisten = 127.0.0.1:9000user = php-fpmgroup = php-fpmpm = static #只有最大的生效pm.max_children = 50 #只有这一个生效pm.start_services = 20 #启动的个数是20个pm.min_spare_services = 5 #最低启动5个pm.max_spare_service = 35pm.max_requests = 500rlimit_files = 1024request_slowlog_timeout = 1 php_admin_value[open_basedir]=/data/wwwroot/aming.com:/tmp/ [root@localhost php-fpm.d]# ps aux | grep php-fpm #查看启动的phpphp-fpm .... .. ... .. .. ... ... aming.com[root@localhost php-fpm.d]# ps aux | grep aming.com[root@localhost php-fpm.d]# ps aux | grep aming.com |wc -l #统计行数21[root@localhost php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t[root@localhost php-fpm.d]# /etc/init.d/php-fpm restart[root@localhost php-fpm.d]# ps aux | grep php-fpm #查看启动的php
常见问题: