溫馨提示×

php-fpm在Ubuntu中如何實現負載均衡

小樊
65
2025-02-21 19:11:04
欄目: 云計算

在Ubuntu系統中,使用PHP-FPM(FastCGI Process Manager)實現負載均衡可以通過多種方式來完成。以下是一些常見的方法:

方法一:使用Nginx作為反向代理

  1. 安裝Nginx

    sudo apt update
    sudo apt install nginx
    
  2. 配置Nginx: 編輯Nginx配置文件(通常位于/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. 啟用配置: 創建符號鏈接以啟用配置:

    sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/
    
  4. 重啟Nginx

    sudo systemctl restart nginx
    

方法二:使用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 server1 192.168.1.101:80 check
        server server2 192.168.1.102:80 check
    
  3. 重啟HAProxy

    sudo systemctl restart haproxy
    

方法三:使用PHP-FPM集群

  1. 安裝多個PHP-FPM實例: 你可以為每個PHP-FPM實例配置不同的socket文件。

    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
    
  2. 編輯配置文件: 編輯www1.confwww2.conf,修改listen參數:

    ; www1.conf
    listen = /run/php/php7.4-fpm-www1.sock
    
    ; www2.conf
    listen = /run/php/php7.4-fpm-www2.sock
    
  3. 重啟PHP-FPM

    sudo systemctl restart php7.4-fpm
    
  4. 配置Nginx: 編輯Nginx配置文件,添加多個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-www1.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    
        location /api/ {
            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-www2.sock;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
            }
        }
    }
    
  5. 重啟Nginx

    sudo systemctl restart nginx
    

通過以上方法,你可以在Ubuntu系統中使用PHP-FPM實現負載均衡。選擇哪種方法取決于你的具體需求和環境。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女