linux

Linux PHP-FPM如何负载均衡

小樊
44
2025-10-18 08:14:42
栏目: 云计算

在Linux系统中,使用PHP-FPM(FastCGI Process Manager)进行负载均衡可以通过多种方式实现。以下是两种常见的方法:

方法一:使用Nginx作为反向代理服务器

  1. 安装Nginx

    sudo apt update
    sudo apt install nginx
    
  2. 配置Nginx: 编辑Nginx的配置文件,通常位于 /etc/nginx/nginx.conf/etc/nginx/sites-available/default

    server {
        listen 80;
        server_name example.com;
    
        location / {
            root /var/www/html;
            index index.php index.html index.htm;
            try_files $uri $uri/ =404;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.4-fpm.sock; # 根据你的PHP版本调整
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
    
  3. 配置多个PHP-FPM实例: 为了实现负载均衡,可以配置多个PHP-FPM实例。编辑PHP-FPM的配置文件,通常位于 /etc/php/7.4/fpm/pool.d/www.conf

    [www]
    user = www-data
    group = www-data
    listen = /run/php/php7.4-fpm.sock
    pm = dynamic
    pm.max_children = 5
    pm.start_servers = 2
    pm.min_spare_servers = 1
    pm.max_spare_servers = 3
    

    复制并修改配置文件以创建多个实例:

    sudo cp /etc/php/7.4/fpm/pool.d/www.conf /etc/php/7.4/fpm/pool.d/www1.conf
    sudo cp /etc/php/7.4/fpm/pool.d/www.conf /etc/php/7.4/fpm/pool.d/www2.conf
    

    修改 listen 参数:

    [www1]
    listen = /run/php/php7.4-fpm1.sock
    
    [www2]
    listen = /run/php/php7.4-fpm2.sock
    

    重启PHP-FPM服务:

    sudo systemctl restart php7.4-fpm
    
  4. 配置Nginx使用多个PHP-FPM实例: 在Nginx配置文件中,为每个PHP-FPM实例创建一个 fastcgi_pass

    server {
        listen 80;
        server_name example.com;
    
        location / {
            root /var/www/html;
            index index.php index.html index.htm;
            try_files $uri $uri/ =404;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.4-fpm.sock; # 默认实例
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.4-fpm1.sock; # 第一个实例
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.4-fpm2.sock; # 第二个实例
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
    

方法二:使用HAProxy进行负载均衡

  1. 安装HAProxy

    sudo apt update
    sudo apt install haproxy
    
  2. 配置HAProxy: 编辑HAProxy的配置文件,通常位于 /etc/haproxy/haproxy.cfg

    global
        log /dev/log local0
        log /dev/log local1 notice
        daemon
    
    defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        timeout connect 5000ms
        timeout client  50000ms
        timeout server  50000ms
    
    frontend http_front
        bind *:80
        default_backend http_back
    
    backend http_back
        balance roundrobin
        server php1 unix:/run/php/php7.4-fpm.sock check
        server php2 unix:/run/php/php7.4-fpm1.sock check
    
  3. 重启HAProxy服务

    sudo systemctl restart haproxy
    

通过以上两种方法,你可以在Linux系统中使用PHP-FPM进行负载均衡。选择哪种方法取决于你的具体需求和环境。

0
看了该问题的人还看了