php-fpm 配置进程池
什么是 php-fpm :php 是作为一个独立服务存在的,这个服务叫做 php-fpm
什么是 php-fpm pool :也就是 php-fpm 的进程池,这个进程池中运行了多个子进程,用来并发处理所有连接的动态请求
为什么要配置多个 pool :Nginx 接收到 php 动态请求会传给 php-fpm 处理,php-fpm 调用 pool 中的子进程来处理动态请求,如果这个 pool 资源耗尽,会导致其他站点无法访问资源,报 502 错误,因此有必要设置多个 php-fpm pool
默认只配置了一个 php-fpm pool :
[root@localhost ~]$ cat /usr/local/php/etc/php-fpm.conf [global] # 全局配置 pid = /usr/local/php/var/run/php-fpm.pid # php-fpm pid 文件 error_log = /usr/local/php/var/log/php-fpm.log # php-fpm 错误日志路径 [www] # 进程池名称 listen = /tmp/php-fcgi.sock # The address on which to accept FastCGI requests listen.mode = 666 # Set permissions for unix socket, read/write permissions must be set in order to allow connections from a web server user = php # Set the user who can accept FastCGI group = php # Set the group who can accept FastCGI pm = dynamic # 设置进程池中子进程的数目为动态的,它的数目基于下面的指令的值,关于 pm 的设置可以参考:https://gist.github.com/sotarok/2767859 pm.max_children = 50 # 同一时刻能够存在的最大子进程的数量 pm.start_servers = 20 # 在启动 php-fpm 时,要启动的子进程数量 pm.min_spare_servers = 5 # 处于空闲状态的最小子进程数量 pm.max_spare_servers = 35 # 处于空闲状态的最大子进程数量 pm.max_requests = 500 # 每个子进程最多能处理多少个请求,达到数量后就自动退出进程池 rlimit_files = 1024 # 限制打开的文件描述符数量,这个一般是通过 ulimit 设置
配置多个 php-fpm pool :
[root@localhost ~]$ cat /usr/local/php/etc/php-fpm.conf [global] pid = /usr/local/php/var/run/php-fpm.pid error_log = /usr/local/php/var/log/php-fpm.log [www] listen = /tmp/php-fcgi.sock listen.mode = 666 user = php group = php 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 [www2] listen = /tmp/php-fcgi2.sock listen.mode = 666 user = php group = php 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@localhost ~]$ /usr/local/php/sbin/php-fpm -t [root@localhost ~]$ /etc/init.d/php-fpm reload [root@localhost ~]$ ps aux | grep php-fpm
Nginx 如何使用不同的 php-fpm pool :
[root@localhost ~]$ cat /usr/local/nginx/conf/vhost/test.com.conf server { listen 80; server_name www.test.com; index index.html index.html index.php; root /data/www; location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; # 在这里指定使用哪个进程池 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name; } }