2019独角兽企业重金招聘Python工程师标准>>>
12.21 php-fpm的pool
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
php-fpm的pool
- php-fpm有一个概念,叫做pool。 使用ps aux |grep php-fpm最右侧看到的那一列就是pool,也就是它的池子
[root@hanfeng ~]# ps aux |grep php-fpm
root 2280 0.0 0.4 227776 4952 ? Ss 18:29 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm 2281 0.0 0.4 227776 4708 ? S 18:29 0:00 php-fpm: pool www
php-fpm 2282 0.0 0.4 227776 4708 ? S 18:29 0:00 php-fpm: pool www
php-fpm 2283 0.0 0.4 227776 4708 ? S 18:29 0:00 php-fpm: pool www
php-fpm 2284 0.0 0.4 227776 4708 ? S 18:29 0:00 php-fpm: pool www
php-fpm 2285 0.0 0.4 227776 4712 ? S 18:29 0:00 php-fpm: pool www
php-fpm 2286 0.0 0.4 227776 4712 ? S 18:29 0:00 php-fpm: pool www
php-fpm 2287 0.0 0.4 227776 4716 ? S 18:29 0:00 php-fpm: pool www
php-fpm 2288 0.0 0.4 227776 4716 ? S 18:29 0:00 php-fpm: pool www
php-fpm 2289 0.0 0.4 227776 4716 ? S 18:29 0:00 php-fpm: pool www
php-fpm 2290 0.0 0.4 227776 4716 ? S 18:29 0:00 php-fpm: pool www
php-fpm 2291 0.0 0.4 227776 4716 ? S 18:29 0:00 php-fpm: pool www
php-fpm 2292 0.0 0.4 227776 4716 ? S 18:29 0:00 php-fpm: pool www
php-fpm 2293 0.0 0.4 227776 4716 ? S 18:29 0:00 php-fpm: pool www
php-fpm 2294 0.0 0.4 227776 4716 ? S 18:29 0:00 php-fpm: pool www
php-fpm 2295 0.0 0.4 227776 4716 ? S 18:29 0:00 php-fpm: pool www
php-fpm 2296 0.0 0.4 227776 4716 ? S 18:29 0:00 php-fpm: pool www
php-fpm 2297 0.0 0.4 227776 4716 ? S 18:29 0:00 php-fpm: pool www
php-fpm 2298 0.0 0.4 227776 4716 ? S 18:29 0:00 php-fpm: pool www
php-fpm 2299 0.0 0.4 227776 4716 ? S 18:29 0:00 php-fpm: pool www
php-fpm 2300 0.0 0.4 227776 4716 ? S 18:29 0:00 php-fpm: pool www
root 7644 0.0 0.0 112680 984 pts/0 S+ 21:38 0:00 grep --color=auto php-fpm
[root@hanfeng ~]#
- 切换目录,会看到我们只定义了一个www的pool
[root@hanfeng ~]# cd /usr/local/php-fpm/etc/
[root@hanfeng etc]# ls
pear.conf php-fpm.conf php-fpm.conf.default php.ini
[root@hanfeng 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.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@hanfeng etc]#
- 其实是支持定义多个池子的,每一个池子都可以监听不同的sock或者TCP/IP,nginx如果有多个站点,那么每个站点都能去使用一个独立的pool,这样做的优势就是,当其中一个php发生502了(502很有可能是资源不够了),如果所有的网站使用了同一个池子,那其中一个网站发生了故障(比如写的程序有问题),那么就会把你的php资源耗尽,这样就会造成其他的站点也会502,所以有必要把每一个站点隔离开来,每一个站点使用独立的pool
- 设置多个pool,方法一
- 在/usr/local/php-fpm/etc/php-fpm里面,继续增加pool
- 写第二个pool,名称为hanfeng.com,listen = /tmp/hanfeng.sock
- 配置中的其他信息可根据自己的需求来配置
[root@hanfeng etc]# vi /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
[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[hanfeng.com]
listen = /tmp/hanfeng.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@hanfeng etc]# /usr/local/php-fpm/sbin/php-fpm -t
[09-Jan-2018 23:28:58] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
[root@hanfeng etc]#
- 若没有语法错误,做一个php-fpm重启
- /etc/init.d/php-fpm restart 或使用 /etc/init.d/php-fpm reload
[root@hanfeng etc]# /etc/init.d/php-fpm reload
Reload service php-fpm done
[root@hanfeng etc]#
- 这里再来ps查看,会看到最右侧多了一个hanfeng.com,这个就是两个不同的池子
[root@hanfeng etc]# ps aux |grep php-fpm
root 7731 0.0 0.4 227836 4964 ? Ss 22:32 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm 7732 0.0 0.4 227776 4708 ? S 22:32 0:00 php-fpm: pool www
php-fpm 7733 0.0 0.4 227776 4708 ? S 22:32 0:00 php-fpm: pool www
php-fpm 7734 0.0 0.4 227776 4708 ? S 22:32 0:00 php-fpm: pool www
php-fpm 7735 0.0 0.4 227776 4708 ? S 22:32 0:00 php-fpm: pool www
php-fpm 7736 0.0 0.4 227776 4712 ? S 22:32 0:00 php-fpm: pool www
php-fpm 7737 0.0 0.4 227776 4712 ? S 22:32 0:00 php-fpm: pool www
php-fpm 7738 0.0 0.4 227776 4712 ? S 22:32 0:00 php-fpm: pool www
php-fpm 7739 0.0 0.4 227776 4712 ? S 22:32 0:00 php-fpm: pool www
php-fpm 7740 0.0 0.4 227776 4712 ? S 22:32 0:00 php-fpm: pool www
php-fpm 7741 0.0 0.4 227776 4712 ? S 22:32 0:00 php-fpm: pool www
php-fpm 7742 0.0 0.4 227776 4716 ? S 22:32 0:00 php-fpm: pool www
php-fpm 7743 0.0 0.4 227776 4716 ? S 22:32 0:00 php-fpm: pool www
php-fpm 7744 0.0 0.4 227776 4716 ? S 22:32 0:00 php-fpm: pool www
php-fpm 7745 0.0 0.4 227776 4716 ? S 22:32 0:00 php-fpm: pool www
php-fpm 7746 0.0 0.4 227776 4716 ? S 22:32 0:00 php-fpm: pool www
php-fpm 7747 0.0 0.4 227776 4716 ? S 22:32 0:00 php-fpm: pool www
php-fpm 7748 0.0 0.4 227776 4716 ? S 22:32 0:00 php-fpm: pool www
php-fpm 7749 0.0 0.4 227776 4716 ? S 22:32 0:00 php-fpm: pool www
php-fpm 7750 0.0 0.4 227776 4716 ? S 22:32 0:00 php-fpm: pool www
php-fpm 7751 0.0 0.4 227776 4716 ? S 22:32 0:00 php-fpm: pool www
php-fpm 7752 0.0 0.4 227776 4712 ? S 22:32 0:00 php-fpm: pool hanfeng.com
php-fpm 7753 0.0 0.4 227776 4712 ? S 22:32 0:00 php-fpm: pool hanfeng.com
php-fpm 7754 0.0 0.4 227776 4712 ? S 22:32 0:00 php-fpm: pool hanfeng.com
php-fpm 7755 0.0 0.4 227776 4712 ? S 22:32 0:00 php-fpm: pool hanfeng.com
php-fpm 7756 0.0 0.4 227776 4716 ? S 22:32 0:00 php-fpm: pool hanfeng.com
php-fpm 7757 0.0 0.4 227776 4716 ? S 22:32 0:00 php-fpm: pool hanfeng.com
php-fpm 7758 0.0 0.4 227776 4720 ? S 22:32 0:00 php-fpm: pool hanfeng.com
php-fpm 7759 0.0 0.4 227776 4720 ? S 22:32 0:00 php-fpm: pool hanfeng.com
php-fpm 7760 0.0 0.4 227776 4720 ? S 22:32 0:00 php-fpm: pool hanfeng.com
php-fpm 7761 0.0 0.4 227776 4720 ? S 22:32 0:00 php-fpm: pool hanfeng.com
php-fpm 7762 0.0 0.4 227776 4720 ? S 22:32 0:00 php-fpm: pool hanfeng.com
php-fpm 7763 0.0 0.4 227776 4720 ? S 22:32 0:00 php-fpm: pool hanfeng.com
php-fpm 7764 0.0 0.4 227776 4720 ? S 22:32 0:00 php-fpm: pool hanfeng.com
php-fpm 7765 0.0 0.4 227776 4720 ? S 22:32 0:00 php-fpm: pool hanfeng.com
php-fpm 7766 0.0 0.4 227776 4720 ? S 22:32 0:00 php-fpm: pool hanfeng.com
php-fpm 7767 0.0 0.4 227776 4720 ? S 22:32 0:00 php-fpm: pool hanfeng.com
php-fpm 7768 0.0 0.4 227776 4720 ? S 22:32 0:00 php-fpm: pool hanfeng.com
php-fpm 7769 0.0 0.4 227776 4720 ? S 22:32 0:00 php-fpm: pool hanfeng.com
php-fpm 7770 0.0 0.4 227776 4720 ? S 22:32 0:00 php-fpm: pool hanfeng.com
php-fpm 7771 0.0 0.4 227776 4720 ? S 22:32 0:00 php-fpm: pool hanfeng.com
root 7775 0.0 0.0 112676 988 pts/0 R+ 22:39 0:00 grep --color=auto php-fpm
[root@hanfeng etc]#
- 在nginx中使用新的pool
[root@hanfeng etc]# cd /usr/local/nginx/conf/vhost/
[root@hanfeng vhost]# ls
aaa.com.conf load.conf proxy.conf ssl.conf test.com.conf
[root@hanfeng vhost]#
- 可以把test.com.conf定义成fastcgi_pass unix:/tmp/php-fcgi.sock;
- 这是www池子中的php-fcgi.sock
[root@hanfeng vhost]# vim test.com.confserver
{listen 80;server_name test.com test1.com test2.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 192.168.202.130;allow 127.0.0.1;deny all;}
location ~ \.php${include fastcgi_params;fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;}access_log /tmp/test.com.log combined_realip;
}
- 再把另一个aaa.com.conf中 fastcgi_pass unix:/tmp/hanfeng.sock;
- 这是hanfeng.com池子中的hanfeng.sock
- 路径/data/wwwroot/default$fastcgi_script_name;
[root@hanfeng vhost]# vim aaa.com.confserver
{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/hanfeng.sock; fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /data/wwwroot/default$fastcgi_script_name;}
保存退出
- 这样这两个站点就隔离开了
- 假设现在test.com请求量很大,最终将进程耗尽了,比如php-fpm耗尽了,这时候又有其他请求来了,就会报502,因为没有多余的php给进程提供服务了,而另一个站点则不受影响,因为使用的是另一个池子,另一个pool,这就是定义多个池子的原因
方法二
- 在vhost目录下是多个.conf文件,在nginx.conf里面加了一条 include vhost/*.conf;
[root@hanfeng vhost]# ls
aaa.com.conf load.conf proxy.conf ssl.conf test.com.conf
[root@hanfeng vhost]# tail ../nginx.conftcp_nodelay on;gzip on;gzip_min_length 1k;gzip_buffers 4 8k;gzip_comp_level 5;gzip_http_version 1.1;gzip_types text/plain application/x-javascript text/css text/htm application/xml;include vhost/*.conf;
}
[root@hanfeng vhost]#
- 在php-fpm同样是支持 include vhost/*.conf;
- 在主配置文件php-fpm中的 [global] 加入include = etc/php-fpm.d/*.conf ,并将池子拆分出来,将其中的池子pool删除
- 快速删除内容方法,100dd就会给删除
[root@hanfeng 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.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[hanfeng.com]
listen = /tmp/hanfeng.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@hanfeng etc]#
最终如下
[root@hanfeng 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/,所以需要创建/php-fpm.d/的目录
[root@hanfeng etc]# mkdir php-fpm.d
[root@hanfeng etc]#
- 切换路径,并创建www.conf,然后将之前php-fpm.conf中的www部分写入到其中
[root@hanfeng etc]# cd !$
cd php-fpm.d
[root@hanfeng php-fpm.d]# vim 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
保存退出
- 然后在创建hanfeng.conf
[root@hanfeng php-fpm.d]# vim hanfeng.conf[hanfeng.com]
listen = /tmp/hanfeng.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
保存退出
- 这样看着就会很干净,类似于nginx的vhost
- 检查是否有错误
[root@hanfeng php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t
[09-Jan-2018 23:28:58] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful[root@hanfeng php-fpm.d]#
- 然后重启查看
[root@hanfeng php-fpm.d]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@hanfeng php-fpm.d]# !ps
ps aux |grep php-fpm
root 7832 0.2 0.4 227864 4980 ? Ss 23:29 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm 7833 0.0 0.4 227804 4712 ? S 23:29 0:00 php-fpm: pool hanfeng.com
php-fpm 7834 0.0 0.4 227804 4712 ? S 23:29 0:00 php-fpm: pool hanfeng.com
php-fpm 7835 0.0 0.4 227804 4712 ? S 23:29 0:00 php-fpm: pool hanfeng.com
php-fpm 7836 0.0 0.4 227804 4712 ? S 23:29 0:00 php-fpm: pool hanfeng.com
php-fpm 7837 0.0 0.4 227804 4716 ? S 23:29 0:00 php-fpm: pool hanfeng.com
php-fpm 7838 0.0 0.4 227804 4716 ? S 23:29 0:00 php-fpm: pool hanfeng.com
php-fpm 7839 0.0 0.4 227804 4716 ? S 23:29 0:00 php-fpm: pool hanfeng.com
php-fpm 7840 0.0 0.4 227804 4716 ? S 23:29 0:00 php-fpm: pool hanfeng.com
php-fpm 7841 0.0 0.4 227804 4716 ? S 23:29 0:00 php-fpm: pool hanfeng.com
php-fpm 7842 0.0 0.4 227804 4716 ? S 23:29 0:00 php-fpm: pool hanfeng.com
php-fpm 7843 0.0 0.4 227804 4720 ? S 23:29 0:00 php-fpm: pool hanfeng.com
php-fpm 7844 0.0 0.4 227804 4720 ? S 23:29 0:00 php-fpm: pool hanfeng.com
php-fpm 7845 0.0 0.4 227804 4720 ? S 23:29 0:00 php-fpm: pool hanfeng.com
php-fpm 7846 0.0 0.4 227804 4720 ? S 23:29 0:00 php-fpm: pool hanfeng.com
php-fpm 7847 0.0 0.4 227804 4720 ? S 23:29 0:00 php-fpm: pool hanfeng.com
php-fpm 7848 0.0 0.4 227804 4720 ? S 23:29 0:00 php-fpm: pool hanfeng.com
php-fpm 7849 0.0 0.4 227804 4720 ? S 23:29 0:00 php-fpm: pool hanfeng.com
php-fpm 7850 0.0 0.4 227804 4720 ? S 23:29 0:00 php-fpm: pool hanfeng.com
php-fpm 7851 0.0 0.4 227804 4720 ? S 23:29 0:00 php-fpm: pool hanfeng.com
php-fpm 7852 0.0 0.4 227804 4720 ? S 23:29 0:00 php-fpm: pool hanfeng.com
php-fpm 7853 0.0 0.4 227804 4716 ? S 23:29 0:00 php-fpm: pool www
php-fpm 7854 0.0 0.4 227804 4716 ? S 23:29 0:00 php-fpm: pool www
php-fpm 7855 0.0 0.4 227804 4716 ? S 23:29 0:00 php-fpm: pool www
php-fpm 7856 0.0 0.4 227804 4716 ? S 23:29 0:00 php-fpm: pool www
php-fpm 7857 0.0 0.4 227804 4720 ? S 23:29 0:00 php-fpm: pool www
php-fpm 7858 0.0 0.4 227804 4720 ? S 23:29 0:00 php-fpm: pool www
php-fpm 7859 0.0 0.4 227804 4724 ? S 23:29 0:00 php-fpm: pool www
php-fpm 7860 0.0 0.4 227804 4724 ? S 23:29 0:00 php-fpm: pool www
php-fpm 7861 0.0 0.4 227804 4724 ? S 23:29 0:00 php-fpm: pool www
php-fpm 7862 0.0 0.4 227804 4724 ? S 23:29 0:00 php-fpm: pool www
php-fpm 7863 0.0 0.4 227804 4724 ? S 23:29 0:00 php-fpm: pool www
php-fpm 7864 0.0 0.4 227804 4724 ? S 23:29 0:00 php-fpm: pool www
php-fpm 7865 0.0 0.4 227804 4724 ? S 23:29 0:00 php-fpm: pool www
php-fpm 7866 0.0 0.4 227804 4724 ? S 23:29 0:00 php-fpm: pool www
php-fpm 7867 0.0 0.4 227804 4724 ? S 23:29 0:00 php-fpm: pool www
php-fpm 7868 0.0 0.4 227804 4724 ? S 23:29 0:00 php-fpm: pool www
php-fpm 7869 0.0 0.4 227804 4724 ? S 23:29 0:00 php-fpm: pool www
php-fpm 7870 0.0 0.4 227804 4724 ? S 23:29 0:00 php-fpm: pool www
php-fpm 7871 0.0 0.4 227804 4724 ? S 23:29 0:00 php-fpm: pool www
php-fpm 7872 0.0 0.4 227804 4724 ? S 23:29 0:00 php-fpm: pool www
root 7874 0.0 0.0 112676 988 pts/0 R+ 23:29 0:00 grep --color=auto php-fpm
[root@hanfeng php-fpm.d]#
- 这就是多个池子的配置方法
12.22 php-fpm慢执行日志
php-fpm慢执行日志目录概要
- vim /usr/local/php-fpm/etc/php-fpm.d/www.conf//加入如下内容
request_slowlog_timeout = 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
php-fpm慢执行日志
- 在做php网站的时候,建议使用lnmp,因为我们可以分析php的慢执行
- 查询方法:
- 1.系统负载,可以通过各种工具查,查出是哪个进程导致
- 2.PHP网站访问慢,通过查看慢日志
- 配置www.conf 在配置文件中加入
request_slowlog_timeout = 1 //超过一秒钟就要记录日志
slowlog = /usr/local/php-fpm/var/log/www-slow.log //日志放到该路径下
最终如下
[root@hanfeng php-fpm.d]# vim 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
request_slowlog_timeout = 1
slowlog = /usr/local/php-fpm/var/log/www-slow.log
保存退出
- 查看语法错误,并重新加载
[root@hanfeng php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t
[10-Jan-2018 00:04:26] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful[root@hanfeng php-fpm.d]# /etc/init.d/php-fpm reload
Reload service php-fpm done
[root@hanfeng php-fpm.d]#
- 查看/usr/local/php-fpm/var/log/www-slow.log日志是否生成
[root@hanfeng php-fpm.d]# ls /usr/local/php-fpm/var/log/
php-fpm.log www-slow.log
[root@hanfeng php-fpm.d]#
- 查看日志会看到没有内容,因为没有超过一秒php执行的过程
[root@hanfeng php-fpm.d]# cat /usr/local/php-fpm/var/log/www-slow.log
[root@hanfeng php-fpm.d]#
- 模拟一个慢执行的php,写一个脚本,由于php-fcgi它是被在test这个站点用着,所以需要在这个/data/wwwroot/test.com目录下做一些操作
[root@hanfeng php-fpm.d]# vim /data/wwwroot/test.com/sleep.php
加入php休眠两秒钟,故意让它执行慢
<?php
echo “test slow log”;
sleep(2);echo “done”;
?>
保存退出
- php可以不换行,写成一行
- 运行php脚本,但是未输出信息,检测会看到状态码为500,说明有错误
[root@hf-01 php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php
[root@hf-01 php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php -I
HTTP/1.1 500 Internal Server Error
Server: nginx/1.12.1
Date: Tue, 09 Jan 2018 21:47:48 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30[root@hf-01 php-fpm.d]#
- 可以查看错误日志,但有更简单的方法就是打开它的配置文件
[root@hf-01 php-fpm.d]# vim /usr/local/php-fpm/etc/php.ini搜索 /display ,找到display_errors将display_errors = Off 改为display_errors = On
- 打开display_errors之后,就可以在浏览器上查看到具体的错误是什么
- 重启php
[root@hf-01 php-fpm.d]# /etc/init.d/php-fpm reload
Reload service php-fpm done
[root@hf-01 php-fpm.d]#
- 检查错误,会看到语法错误,有可能是逗号,分号写的不对
[root@hf-01 php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php
<br />
<b>Parse error</b>: syntax error, unexpected 'slow' (T_STRING), expecting ',' or ';' in <b>/data/wwwroot/test.com/sleep.php</b> on line <b>2</b><br />
[root@hf-01 php-fpm.d]#
- 检查sleep.php文件,发现里面使用的中文标点,更改后重启配置
[root@hf-01 php-fpm.d]# vim /data/wwwroot/test.com/sleep.php<?php
echo "test slow log";
sleep(2);
echo "done";
?>
保存退出
[root@hf-01 php-fpm.d]# /etc/init.d/php-fpm reload
Reload service php-fpm done
[root@hf-01 php-fpm.d]#
- 再来测试,访问成功,会看到访问的时候停顿了几秒
[root@hf-01 php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php
test slow logdone[root@hf-01 php-fpm.d]#
- 查看是否生成了慢日志
[root@hf-01 php-fpm.d]# cat /usr/local/php-fpm/var/log/www-slow.log [10-Jan-2018 06:02:21] [pool www] pid 3442
script_filename = /data/wwwroot/test.com/sleep.php //提示:是哪个脚本慢
[0x00007f37c5f7c298] sleep() /data/wwwroot/test.com/sleep.php:3 //提示:是脚本的第三慢
[root@hf-01 php-fpm.d]#
- 这个就是慢执行日志的功效
- 在遇到php网站访问慢的时候,就可以去查看慢日志
- 查看www.conf
[root@hf-01 php-fpm.d]# cat 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
request_slowlog_timeout = 1 //一般写两秒,很多脚本都会超过一秒
slowlog = /usr/local/php-fpm/var/log/www-slow.log
[root@hf-01 php-fpm.d]#
12.23 open_basedir
php-fpm定义open_basedir目录概要
- vim /usr/local/php-fpm/etc/php-fpm.d/test.conf//加入如下内容
php_admin_value[open_basedir]=/data/wwwroot/aming.com:/tmp/
- 创建测试php脚本,进行测试
- 再次更改aming.conf,修改路径,再次测试
- 配置错误日志
- 再次测试
- 查看错误日志
php-fpm定义open_basedir
- open_basedir 的作用是限制php在指定的目录里活动。
- 因为如果服务器管理多个网站,在php.ini里定义多个open_basedir就不合适了,所以要么在apache虚拟主机配置文件里面定义,要么在php-fpm配置文件里面定义,我们可以针对不同的池子(pool)定义对应的open_basedir
- 只需要加 下面一行配置即可
php_admin_value[open_basedir]=/data/wwwroot/test.com:/tmp/
- 如果路径出错,就会无法正常访问,导致404页面 curl直接连会提示“No input file specified.”
- 针对www的pool进行配置
- 在最后一行加入配置
[root@hf-01 php-fpm.d]# 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
request_slowlog_timeout = 1
slowlog = /usr/local/php-fpm/var/log/www-slow.log
php_admin_value[open_basedir]=/data/wwwroot/test.com:/tmp/
- 重启php-fpm
[root@hf-01 php-fpm.d]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@hf-01 php-fpm.d]#
- 访问测试
[root@hf-01 php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php -I
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Tue, 09 Jan 2018 22:56:24 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30[root@hf-01 php-fpm.d]#
- 若open_basedir定义错误,就会导致无法访问,形成404状态码,输出curl直接连会提示“No input file specified.”
- 定义错误日志
[root@hf-01 php-fpm.d]# vi /usr/local/php-fpm/etc/php.ini搜索 /display_errors
将display_errors = Off
搜索 /error_log,添加error_log
;error_log = php_errors.log
; Log errors to syslog (Event Log on Windows).
;error_log = syslog
error_log = /usr/local/php-fpm/var/log/php_errors.log //这一段定义错误日志
搜索error_reporting
注释掉自带的error_reporting
;error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
error_reporting = E_ALL //定义错误日志的级别,所有保存退出
- display_errors = Off 正常情况下,在线上这个是off的,别人不能通过浏览器看到你的错误信息,而是把你的错误信息记录到服务器的某一个文件里
- 查看设置的错误日志文件,并是否生成
[root@hf-01 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@hf-01 php-fpm.d]# [root@hf-01 php-fpm.d]# ls /usr/local/php-fpm/var/log/
php-fpm.log www-slow.log
[root@hf-01 php-fpm.d]#
- 手动生成错误日志文件,并设置权限为777,防止不能够正常写入
[root@hf-01 php-fpm.d]# touch /usr/local/php-fpm/var/log/php_errors.log
[root@hf-01 php-fpm.d]# chmod 777 /usr/local/php-fpm/var/log/php_errors.log
[root@hf-01 php-fpm.d]#
- 这里为了方便测试,故意将/usr/local/php-fpm/etc/php-fpm.d/www.conf 中的open_basedir路径写错,来方便测试
- 重启php-fpm
[root@hf-01 php-fpm.d]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@hf-01 php-fpm.d]#
- 访问测试,会看到显示404
[root@hf-01 php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php -I
HTTP/1.1 404 Not Found
Server: nginx/1.12.1
Date: Tue, 09 Jan 2018 23:35:31 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30[root@hf-01 php-fpm.d]#
- 查看错误日志,会看到访问地址有限制
[root@hf-01 php-fpm.d]# cat /usr/local/php-fpm/var/log/php_errors.log
[09-Jan-2018 23:35:31 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/111test.com:/tmp/) in Unknown on line 0
[09-Jan-2018 23:35:31 UTC] PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line 0
[root@hf-01 php-fpm.d]#
- 再将配置文件的open_basedir路径修改过来,重新加载php-fpm,再来访问,会看到正常显示
[root@hf-01 php-fpm.d]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf
[root@hf-01 php-fpm.d]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@hf-01 php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php -I
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Tue, 09 Jan 2018 23:39:30 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30[root@hf-01 php-fpm.d]#
12.24 php-fpm进程管理
php-fpm进程管理
[root@hf-01 ~]# cat /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
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@hf-01 ~]#
- pm = dynamic //表示进程以什么形式启动,dynamic就是动态,动态就是一开始为一个数值,根据需求再自动生成,服务器比较闲的时候还会去销毁,销毁到一定程度还有自动生成
- 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的子进程最多可以处理这么多请求,当达到这个数值时,它会自动退出。
- 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/
dynamic和static
- pm = dynamic //表示进程以什么形式启动,dynamic就是动态,动态就是一开始为一个数值,根据需求再自动生成,服务器比较闲的时候还会去销毁,销毁到一定程度还有自动生成;根据下面的设置去进行设定“start_servers ”、“min_spare_servers ”、“max_spare_servers ”、“max_requests ”
- pm = static 之后,一旦选择这个设置,下面的pm的各种设置只会有一个max_children生效,并启动的时候就生成50个
扩展
-
nginx中的root和alias区别
-
nginx的alias和root配置
- 更详细