ubuntu下php-fpm多实例运行配置

php-fpm服务一般情况下我们只会配置一个php-fpm了,如果我们碰到要实现多实例php-fpm服务要如何来配置呢,下面一起来看看吧。 这里是在LNMP环境的基础上配置多实例的过程。因为我在使用的是LNAMP环境,所以额外编译一份php到/usr/local/php-fpm/,配置文件在/etc/php-fpm/,并在此基础上做的笔记。 先修改三个值:

  1. 以static方式,也就是固定进程数量运行php-fpm。 本文仍 以dynamic方式运行
  2. 每个实例建立100个worker进程。(不宜太多,避免不稳定),本文建立25个
  3. 每个进程执行完200次请求即退出。(避免PHP的内存泄露问题,也就是内存越用越多)

位置: /etc/php5/fpm/pool.d/www.conf 

sed -i 's#pm.max_children = 5#pm.max_children = 25#' /etc/php5/fpm/pool.d/www.conf
sed -i 's#;pm.max_requests = 500#pm.max_requests = 500#' /etc/php5/fpm/pool.d/www.conf

拷贝: cp /etc/php5/fpm /etc/php5/fpm2 

sed -i 's#listen = 127.0.0.1:9000#listen = 127.0.0.1:9001#' /etc/php5/fpm2/pool.d/www.conf

拷贝: cp /etc/init.d/php5-fpm /etc/init.d/php5-fpm2 

修改一下几处,其实就是把对应地方改为fpm2

NAME=php5-fpm2
...
DAEMON_ARGS="--daemonize --fpm-config /etc/php5/fpm2/php-fpm.conf"
PIDFILE=/var/run/php5-fpm2.pid
...
/usr/lib/php5/php5-fpm2-checkconf || return 1
...

拷贝: cp /usr/lib/php5/php5-fpm-checkconf /usr/lib/php5/php5-fpm2-checkconf 

修改对应地方改为fpm2

errors=$(/usr/sbin/php5-fpm --fpm-config /etc/php5/fpm2/php-fpm.conf -t 2>&1 | grep "[ERROR]" || true);

配置完毕,然后启动

service php5-fpm2 start

毫无反应!!!!

关键是,连报错提示都木有。。。

排错中。。。。。

发现在 [ -x "$DAEMON" ] || exit 0 时已经退出执行了,解决办法:cp /usr/sbin/php5-fpm /usr/sbin/php5-fpm2 。

同样问题依旧出现,继续,发现进入了 if init_is_upstart; then 条件判断,导致退出执行,无奈之举 注释了exti 语句。。感觉欠妥,但一时不知更好的处理方法,如果有更好的解决办法,,烦请告知。。在此多谢

终于,,,,可以启动了。。

配置nginx

位置: /etc/nginx/sites-enabled/default 

upstream phpbackend {
    server 127.0.0.1:9000 weight=50 max_fails=10 fail_timeout=30;
    server 127.0.0.1:9001 weight=50 max_fails=10 fail_timeout=30;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    # Make site accessible from http://localhost/
    server_name localhost;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
    }

    location ~.php$ {
            try_files $uri = 404;
            #fastcgi_pass 127.0.0.1:9000;
            fastcgi_pass phpbackend;
            fastcgi_index index.php;
            include fastcgi_params;
    }

}

重启nginx,ok

访问phpinfo,可正常访问

在这有几点疑问,不知道大家是否有同样疑惑,或者知道答案的小伙伴烦请共享下知识,哈哈哈哈

php-fpm明明开启了多实例运行,9000和90001端口,但是通过访问phpinfo看到的信息一直访问的配置文件是/etc/php5/fpm下面的,即便通过日志看到的访问的是9001端口,配置依旧是/etc/php5/fpm。。纳闷

原文地址:https://www.cnblogs.com/wanghaokun/p/10192379.html