要优化Linux PHP-FPM连接数,可以从以下几个方面进行调整:
PHP-FPM的配置文件通常是/etc/php-fpm.d/www.conf
或/etc/php/7.x/fpm/pool.d/www.conf
(具体路径取决于你的PHP版本和安装方式)。
pm
:进程管理方式,可选dynamic
、ondemand
、static
。pm.max_children
:最大子进程数。pm.start_servers
:启动时的服务器进程数。pm.min_spare_servers
:最小空闲服务器进程数。pm.max_spare_servers
:最大空闲服务器进程数。pm.max_requests
:每个子进程在重启之前可以处理的请求数。例如,如果你使用的是dynamic
模式,可以这样设置:
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
如果你使用Nginx作为Web服务器,也需要调整Nginx的配置以匹配PHP-FPM的连接数。
fastcgi_pass
:指定PHP-FPM的监听地址和端口。fastcgi_connect_timeout
:连接超时时间。fastcgi_send_timeout
:发送数据超时时间。fastcgi_read_timeout
:接收数据超时时间。例如:
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php index.html index.htm;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 根据你的PHP版本调整
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
}
}
有时候,系统级别的参数也会影响PHP-FPM的性能。
fs.file-max
:系统允许的最大文件描述符数。net.core.somaxconn
:监听队列的最大长度。vm.max_map_count
:进程可以拥有的最大内存映射区域数。例如,可以在/etc/sysctl.conf
中添加以下配置:
fs.file-max = 100000
net.core.somaxconn = 4096
vm.max_map_count = 262144
然后运行sysctl -p
使配置生效。
使用监控工具(如top
、htop
、php-fpm status
)来监控PHP-FPM的性能和资源使用情况。根据监控结果,进一步调整上述参数。
通过以上步骤,你可以有效地优化Linux PHP-FPM的连接数,提升应用的性能和稳定性。