php-fpm的pool

php-fpm.conf可以设置多个pool,在其中一个pool资源耗尽,会导致其他站点无法访问资源,报502错误。有必要把站点进行分离,分别使用单独的pool。

修改php-fpm配置文件代码

# 文件地址:/usr/local/php-fpm/etc/php-fpm.conf
[global]
include=/usr/local/php/etc/php-fpm.d/*.conf

上面的默认配置文件,主要是全局配置文件,我们把每个pool放在/usr/local/php/etc/php-fpm.d文件夹下

# 文件地址 /usr/local/php/etc/php-fpm.d/www.conf
[www]
user = www-data
group = www-data
# listen = /tmp/test.sock # Unix Socket
listen = 127.0.0.1:9000   # TCP Socket
pm = dynamic
pm.max_children = 40
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 1000

当然我们还可以添加文件(新增pool)

# 文件地址 /usr/local/php/etc/php-fpm.d/test.conf
[test]
user = www-data
group = www-data
# listen = /tmp/test.sock # Unix Socket
listen = 127.0.0.1:9001   # TCP Socket
pm = dynamic
pm.max_children = 40
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 1000

设置对应应用的nginx虚拟主机配置文件

# 文件地址 /usr/local/nginx/conf/vhost/www.com.conf
# 每个虚拟主机都是独立的,实现不同虚拟主机访问资源pool的隔离,互不影响
server {
        listen       80;
        server_name  blog.com;

        location ~* \.php$ {
            root /var/blog/public;
            fastcgi_index   index.php;
            fastcgi_pass    127.0.0.1:9000; # 这里调用就是www的进程pool
            include         fastcgi_params;
            fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
        }

        error_log    /usr/local/nginx/logs/blog.error.log   error;
        #access_log   /usr/local/nginx/logs/blog.access.log  main;

    }
# 文件地址 /usr/local/nginx/conf/vhost/test.com.conf 
# 每个虚拟主机都是独立的,实现不同虚拟主机访问资源pool的隔离,互不影响
server {
        listen       80;
        server_name  test.com;

        location ~* \.php$ {
            root /var/test/public;
            fastcgi_index   index.php;
            fastcgi_pass    127.0.0.1:9001; # 这里调用就是test的进程pool
            include         fastcgi_params;
            fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
        }

        error_log    /usr/local/nginx/logs/test.error.log   error;
        #access_log   /usr/local/nginx/logs/test.access.log  main;

    }

修改好配置文件后,记得重启服务!

查看配置是否生效

这里www池在前,test池在后

$ ps aux | grep php-fpm
root       695  0.0  1.7 384300 33124 ?        Ss   21:38   0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
www-data 18258  0.0  1.7 390512 33884 ?        S    22:11   0:01 php-fpm: pool www
www-data 18284  0.0  1.7 388592 32044 ?        S    22:11   0:01 php-fpm: pool www
www-data 18286  0.0  1.5 390180 28332 ?        S    22:11   0:00 php-fpm: pool www
www-data 18357  0.0  1.7 390512 33884 ?        S    22:11   0:01 php-fpm: pool test
www-data 18383  0.0  1.7 388592 32044 ?        S    22:11   0:01 php-fpm: pool test
www-data 18385  0.0  1.5 390180 28332 ?        S    22:11   0:00 php-fpm: pool test