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.conf

server
{
    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.conf

server
{
    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.conf
    tcp_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]#

最終以下javascript

[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. 這就是多個池子的配置方法
相關文章
相關標籤/搜索