ubuntu

如何配置php-fpm在ubuntu上的并发连接数

小樊
43
2025-07-13 15:38:21
栏目: 编程语言

要配置PHP-FPM(FastCGI Process Manager)在Ubuntu上的并发连接数,你需要调整几个关键参数。以下是详细的步骤:

1. 安装PHP-FPM

如果你还没有安装PHP-FPM,可以使用以下命令进行安装:

sudo apt update
sudo apt install php-fpm

2. 配置PHP-FPM

PHP-FPM的配置文件通常位于 /etc/php/版本号/fpm/pool.d/www.conf。你需要编辑这个文件来调整并发连接数。

2.1 打开配置文件

sudo nano /etc/php/版本号/fpm/pool.d/www.conf

版本号 替换为你安装的PHP版本,例如 7.48.0

2.2 调整并发连接数参数

www.conf 文件中,找到以下参数并进行调整:

例如,如果你想设置最大并发连接数为100,可以这样配置:

pm = dynamic
pm.max_children = 100
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35

3. 重启PHP-FPM服务

保存并关闭配置文件后,重启PHP-FPM服务以应用更改:

sudo systemctl restart php版本号-fpm

版本号 替换为你安装的PHP版本。

4. 验证配置

你可以通过查看PHP-FPM的状态来验证配置是否生效:

sudo systemctl status php版本号-fpm

5. 调整Nginx或Apache配置(如果使用)

如果你使用的是Nginx或Apache作为Web服务器,还需要确保它们的配置文件中的PHP处理部分也进行了相应的调整。

Nginx

在Nginx配置文件中,找到 location ~ \.php$ 块,确保有类似以下的配置:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php版本号-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

Apache

在Apache配置文件中,确保启用了 mod_proxy_fcgimod_fastcgi,并且有类似以下的配置:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/html

    <FilesMatch \.php$>
        SetHandler "proxy:fcgi://unix:/var/run/php/php版本号-fpm.sock|fcgi://localhost"
    </FilesMatch>
</VirtualHost>

6. 重启Web服务器

最后,重启Nginx或Apache服务以应用更改:

sudo systemctl restart nginx
# 或者
sudo systemctl restart apache2

通过以上步骤,你应该能够成功配置PHP-FPM在Ubuntu上的并发连接数。

0
看了该问题的人还看了