php-fpm的pool设置

@李彪  June 12, 2018

LNMP架构——php-fpm的进程pool设置

php-fpm的pool

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

修改php-fpm配置文件代码

新增一个pool代码设置

# 文件地址:/usr/local/php-fpm/etc/php-fpm.conf
# vim php-fpm.conf
...
# 新增一个pool代码设置
[test]
listen = /tmp/test.sock
#listen = 127.0.0.1:9000
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

修改虚拟主机配置文件

// 这里设置另外一个虚拟主机
// 实现不同虚拟主机访问资源pool的隔离,互不影响
# cat /usr/local/nginx/conf/vhost/aaa.com.conf 
server
{
    listen 80 default_server;
    index index.html index.htm index.php
    root /data/www/default;
        location ~ \.php$
        {
            include fastcgi_params;
            # 这里写对应php-fpm.conf内设置的sock
            fastcgi_pass unix:/tmp/test.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /data/www/default$fastcgi_script_name;
        }
}

重启服务

# /usr/local/php-fpm/sbin/php-fpm -t
[05-Jan-2018 19:37:17] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

# /etc/init.d/php-fpm reload
Reload service php-fpm  done

查看配置是否生效

# 已经可以看到创建的pool --- test
# ps aux | grep php-fpm
root       2895  1.7  0.3 227208  5008 ?        Ss   19:37   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
...
php-fpm    2915  0.0  0.3 227148  4712 ?        S    19:37   0:00 php-fpm: pool www
php-fpm    2916  1.4  0.3 227148  4708 ?        S    19:37   0:00 php-fpm: pool test
...
root       2937  0.0  0.0 112680   976 pts/0    S+   19:37   0:00 grep --color=auto php-fpm

实现代码分离

在php-fpm.conf内添加如下代码就可以实现类似虚拟主机配置文件分离的效果。

# 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
include = etc/php-fpm.d/*.conf

# 将原文件内的www、test pool模块分别写在不同的conf文件内
# vim php-fpm.d/www.conf
[www]
#listen = /tmp/php-fcgi.sock
listen = 127.0.0.1:9000
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 php-fpm.d/test.conf
[test]
listen = /tmp/test.sock
#listen = 127.0.0.1:9000
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

重启php-fpm服务

# /usr/local/php-fpm/sbin/php-fpm -t
[05-Jan-2018 20:19:10] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done

查看pool是否配置成功

# 这里test池在前,www池在后
# ps aux | grep php-fpm
root       3068  0.2  0.3 227236  4980 ?        Ss   20:20   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm    3069  0.0  0.3 227176  4712 ?        S    20:20   0:00 php-fpm: pool test
php-fpm    3070  0.0  0.3 227176  4712 ?        S    20:20   0:00 php-fpm: pool test
php-fpm    3071  0.0  0.3 227176  4712 ?        S    20:20   0:00 php-fpm: pool test
php-fpm    3075  0.0  0.3 227176  4716 ?        S    20:20   0:00 
...
php-fpm    3079  0.0  0.3 227176  4716 ?        S    20:20   0:00 php-fpm: pool test
php-fpm    3080  0.0  0.3 227176  4716 ?        S    20:20   0:00 php-fpm: pool test
php-fpm    3081  0.0  0.3 227176  4716 ?        S    20:20   0:00 php-fpm: pool test
php-fpm    3089  0.0  0.3 227176  4716 ?        S    20:20   0:00 php-fpm: pool www
...
php-fpm    3107  0.0  0.3 227176  4724 ?        S    20:20   0:00 php-fpm: pool www
php-fpm    3108  0.0  0.3 227176  4724 ?        S    20:20   0:00 php-fpm: pool www
root       3110  0.0  0.0 112680   976 pts/0    S+   20:20   0:00 grep --color=auto php-fpm

评论已关闭