要優化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的連接數,提升應用的性能和穩定性。