Linux基础(day53)

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

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

  1. 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 ~]# 
  1. 切换目录,会看到我们只定义了一个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]#
  1. 其实是支持定义多个池子的,每一个池子都可以监听不同的sock或者TCP/IP,nginx如果有多个站点,那么每个站点都能去使用一个独立的pool,这样做的优势就是,当其中一个php发生502了(502很有可能是资源不够了),如果所有的网站使用了同一个池子,那其中一个网站发生了故障(比如写的程序有问题),那么就会把你的php资源耗尽,这样就会造成其他的站点也会502,所以有必要把每一个站点隔离开来,每一个站点使用独立的pool
  2. 设置多个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保存退出
  1. 检查是否存在语法错误
[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]# 
  1. 若没有语法错误,做一个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]# 
  1. 这里再来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]# 
  1. 在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]# 
  1. 可以把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; 
}
  1. 再把另一个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;}
保存退出
  1. 这样这两个站点就隔离开了
  2. 假设现在test.com请求量很大,最终将进程耗尽了,比如php-fpm耗尽了,这时候又有其他请求来了,就会报502,因为没有多余的php给进程提供服务了,而另一个站点则不受影响,因为使用的是另一个池子,另一个pool,这就是定义多个池子的原因

方法二

  1. 在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]# 
  1. 在php-fpm同样是支持 include vhost/*.conf;
  2. 在主配置文件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
  1. 因为include = etc/php-fpm.d/,所以需要创建/php-fpm.d/的目录
[root@hanfeng etc]# mkdir php-fpm.d
[root@hanfeng etc]# 
  1. 切换路径,并创建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
保存退出
  1. 然后在创建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
保存退出
  1. 这样看着就会很干净,类似于nginx的vhost
  2. 检查是否有错误
[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]# 
  1. 然后重启查看
[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]# 
  1. 这就是多个池子的配置方法

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网站访问慢,通过查看慢日志
  1. 配置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
保存退出
  1. 查看语法错误,并重新加载
[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]# 
  1. 查看/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]# 
  1. 查看日志会看到没有内容,因为没有超过一秒php执行的过程
[root@hanfeng php-fpm.d]# cat /usr/local/php-fpm/var/log/www-slow.log 
[root@hanfeng php-fpm.d]# 
  1. 模拟一个慢执行的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可以不换行,写成一行
  1. 运行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]# 
  1. 可以查看错误日志,但有更简单的方法就是打开它的配置文件
[root@hf-01 php-fpm.d]#  vim /usr/local/php-fpm/etc/php.ini搜索 /display ,找到display_errors将display_errors = Off 改为display_errors = On
  1. 打开display_errors之后,就可以在浏览器上查看到具体的错误是什么
  2. 重启php
[root@hf-01 php-fpm.d]# /etc/init.d/php-fpm reload
Reload service php-fpm  done
[root@hf-01 php-fpm.d]# 
  1. 检查错误,会看到语法错误,有可能是逗号,分号写的不对
[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]# 
  1. 检查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]# 
  1. 再来测试,访问成功,会看到访问的时候停顿了几秒
[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]# 
  1. 查看是否生成了慢日志
[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]# 
  1. 这个就是慢执行日志的功效
  2. 在遇到php网站访问慢的时候,就可以去查看慢日志
  3. 查看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.”
    
  1. 针对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/
  1. 重启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]#
  1. 访问测试
[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]# 
  1. 若open_basedir定义错误,就会导致无法访问,形成404状态码,输出curl直接连会提示“No input file specified.”
  2. 定义错误日志
[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的,别人不能通过浏览器看到你的错误信息,而是把你的错误信息记录到服务器的某一个文件里
  1. 查看设置的错误日志文件,并是否生成
[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]# 
  1. 手动生成错误日志文件,并设置权限为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]# 
  1. 这里为了方便测试,故意将/usr/local/php-fpm/etc/php-fpm.d/www.conf 中的open_basedir路径写错,来方便测试
  2. 重启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]# 
  1. 访问测试,会看到显示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]# 
  1. 查看错误日志,会看到访问地址有限制
[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]# 
  1. 再将配置文件的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个

扩展

  1. nginx中的root和alias区别

  2. nginx的alias和root配置

  • 更详细

转载于:https://my.oschina.net/u/3707314/blog/1604292

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/278652.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Mysql+Navicat for Mysql

一、mysql 1.下载安装 Mysql官网下载地址 下载后解压 .zip &#xff08;或安装.msi&#xff09; 2.可加入全局变量mysqld &#xff08;可选&#xff09; 我的电脑->属性->高级->环境变量->Path(系统变量)&#xff0c;添加mysql下的bin目录&#xff0c;如 D:\Pr…

公钥,私钥和数字签名

一、公钥加密 假设一下&#xff0c;我找了两个数字&#xff0c;一个是1&#xff0c;一个是2。我喜欢2这个数字&#xff0c;就保留起来&#xff0c;不告诉你们(私钥&#xff09;&#xff0c;然后我告诉大家&#xff0c;1是我的公钥。 我有一个文件&#xff0c;不能让别人看&…

MySQL中的日志类型(二)-General query log

简介 General query log记录客户端的连接和断开&#xff0c;以及从客户端发来的每一个SQL语句。 日志内容格式 General query log可以记录在文件中&#xff0c;也可以记录在表中&#xff0c;格式如下&#xff1a;在文件中会记录时间、线程ID、命令类型以及执行的语句示例如下&a…

android wi-fi_如何在Android手机上查找3G或Wi-Fi速度

android wi-fiAre you curious about what kind of connection speed you are getting with your Android phone? Today we’ll take a look at how to easily check your Wi-Fi or 3G speeds with Speedtest.net’s Speed Test app. 您是否对Android手机的连接速度感到好奇&a…

如何在Gmail的图片中插入超链接

Adding hyperlinks is an efficient way of getting your reader to the intended web page. Though it’s no secret that you can add hyperlinks to text, Gmail also lets you add hyperlinks to images in the body of the email. Here’s how to make it happen. 添加超链…

新垣结衣自拍照_如何阻止自拍照出现在iPhone的自拍照专辑中

新垣结衣自拍照Khamosh PathakKhamosh PathakThe Photos app on your iPhone automatically populates all photos from the front-facing camera in the Selfies album. But what if you don’t want a photo to appear there? Here are a couple of solutions. iPhone上的“…

如何设置自定义任务栏图标_如何为任何应用程序自定义Windows 7任务栏图标

如何设置自定义任务栏图标Would you like to change out the icons on your taskbar with a beautiful set of icons that all go together? Here’s how you can change out the random candy-colored icons for a stylish icon set of your choice. 您是否要用一组漂亮的图…

给谷歌浏览器安装vue调试工具:vue-devtools

安装vue-devtools一、拉取项目二、install、build三、添加扩展四、使用举例一、拉取项目 vue-devtools&#xff1a;git地址&#xff08;master分支&#xff09; 非master分支在build的时候会报错。 二、install、build 1、打开cmd进入项目目录&#xff0c;可以选择npm/cnpm/…

如何找到Windows 7或8家庭组密码?

So you’re about to setup your new Windows 7 PC into your Homegroup when you realized that you have no idea what the password is. How do you find it? It’s actually pretty simple, if you know where to look. 因此&#xff0c;当您意识到自己不知道密码是什么时…

MySQL索引背后的数据结构及算法原理

title: MySQL索引背后的数据结构及算法原理 date: 2018-07-25 19:50:16 tags: mysql categories: mysql --- 本文转载自http://blog.codinglabs.org/articles/theory-of-mysql-index.html 摘要 本文以MySQL为研究对象&#xff0c;讨论与数据库索引相关的一些话题。特别需要说明…

使用mcBackup备份Windows 7 Media Center设置

If you’re a HTPC enthusiast and use Windows 7 Media Center, you probably have a lot of scheduled recordings and channel lineups you’d like to backup. Here we take a look at a simple tool that will allow you to do it easily. 如果您是HTPC爱好者并且使用Wind…

linux中service的问题

1.描述问题 2.解决方案 systemctl stop firewalld systemctl mask firewalldThen, install the iptables-services package: yum install iptables-servicesEnable the service at boot-time: systemctl enable iptablesManaging the service systemctl [stop|start|restart] i…

火狐可以打开谷歌打不开_如何设置Firefox以使用Google Apps打开所有内容

火狐可以打开谷歌打不开Google offers a pretty comprehensive set of online applications which many of you probably take advantage of. Here is how to easily configure Firefox to use Google’s online offerings for email, RSS, PDF and office documents as your d…

设置第三方的SMTP服务

取得授权码: 转载于:https://www.cnblogs.com/0909/p/10153499.html

表单重复提交问题

2019独角兽企业重金招聘Python工程师标准>>> 为什么会发生表单重复提交呢&#xff1f; 网络延时 在平时开发中&#xff0c;如果网速比较慢的情况下&#xff0c;用户提交表单后&#xff0c;发现服务器半天都没有响应&#xff0c;那么用户可能会以为是自己没有提交表单…

chrome 网页重新加载_在Chrome中为各个网页设置自定义重新加载时间

chrome 网页重新加载Do you have a webpage that needs to be reloaded every so often or perhaps you have multiple webpages that each need their own individual reload time? Now you can have the best of both with the AutoReloader extension for Google Chrome. 您…

iphone解锁_有人可以用解锁的iPhone做的最糟糕的事情是什么?

iphone解锁Dedi Grigoroiu/Shutterstock.comDedi Grigoroiu / Shutterstock.comWe use our phones for event tickets, reservations, insurance cards, and even driver’s licenses. But what happens when someone takes your unlocked iPhone out of view for a moment—wh…

Alamofire源码导读二:发起请求及内部加锁的逻辑

以创建一个 DataRequest 为例子 &#xfffc; 发起请求 创建 SessionManager 顺带也创建了一个 SessionDelegate 持有一个urlSession&#xff0c;持有一个串行的 DispatchQueue A。注意&#xff0c;这个不是urlSession 回调方法执行时所在的OperationQueue 创建 Requestable 的…

bixby映射软件下载_如何在Samsung Galaxy S8,S9,S10,Note 8或Note 9上重新映射Bixby按钮...

bixby映射软件下载We’ve said before, and we’ll say it again: Bixby sucks. You’re better off using Google Assistant any day of the week. The good news is, it’s now possible to remap the pointless Bixby button without using a third-party app. 我们之前已经…

JavaScript数据结构和算法

前言 在过去的几年中&#xff0c;得益于Node.js的兴起&#xff0c;JavaScript越来越广泛地用于服务器端编程。鉴于JavaScript语言已经走出了浏览器&#xff0c;程序员发现他们需要更多传统语言&#xff08;比如C和Java&#xff09;提供的工具。这些工具包括传统的数据结构&…