首页主机资讯thinkphp项目在ubuntu上如何实现负载均衡

thinkphp项目在ubuntu上如何实现负载均衡

时间2025-11-27 15:21:03发布访客分类主机资讯浏览1064
导读:在 Ubuntu 上为 ThinkPHP 项目实现负载均衡 一 架构与准备 组件选型:前端使用 Nginx/HAProxy 做七层负载均衡;后端多台 Ubuntu 运行 PHP-FPM(如 PHP 8.x)与 ThinkPHP 6。 网络...

在 Ubuntu 上为 ThinkPHP 项目实现负载均衡

一 架构与准备

  • 组件选型:前端使用 Nginx/HAProxy 做七层负载均衡;后端多台 Ubuntu 运行 PHP-FPM(如 PHP 8.x)与 ThinkPHP 6
  • 网络与目录:确保各后端主机的 站点根目录一致(如都部署在 /var/www/your-app/public),便于统一发布与回源。
  • 会话与缓存:多实例下将 Session缓存统一到 Redis/Memcached,避免单点依赖。
  • 健康检查:为后端开启 健康检查故障摘除,提升稳定性。
  • 域名与证书:准备对外 域名TLS 证书(可用 Let’s Encrypt)。
    以上要点与做法在 ThinkPHP 分布式部署与 Nginx/HAProxy 负载均衡实践中有详细说明与示例。

二 方案一 Nginx 作为负载均衡器

  • 安装与启用

    • 安装:sudo apt update & & sudo apt install nginx
    • 启用:sudo systemctl enable --now nginx
  • 配置示例(/etc/nginx/conf.d/load_balancer.conf)

    • 将请求转发到后端 PHP 实例(注意将 fastcgi_param SCRIPT_FILENAME 指向各实例的 public/index.php
    • 使用 upstream 定义后端池,并开启被动健康检查(max_fails/fail_timeout)
    • 设置必要的请求头,便于后端获取真实客户端信息
    upstream tp_backend {
        
        server 10.0.0.11:9000 max_fails=2 fail_timeout=30s;
        
        server 10.0.0.12:9000 max_fails=2 fail_timeout=30s;
        
        # 可按需增加权重:server 10.0.0.13:9000 weight=2;
    
    }
    
    
    server {
        
        listen 80;
        
        server_name your.domain.com;
        
        return 301 https://$host$request_uri;
    
    }
    
    
    server {
        
        listen 443 ssl http2;
        
        server_name your.domain.com;
        
    
        ssl_certificate /etc/letsencrypt/live/your.domain.com/fullchain.pem;
        
        ssl_certificate_key /etc/letsencrypt/live/your.domain.com/privkey.pem;
        
    
        root /var/www/your-app/public;
        
        index index.php;
    
    
        location / {
        
            try_files $uri $uri/ /index.php?$query_string;
    
        }
    
    
        location ~ \.php$ {
        
            include snippets/fastcgi-php.conf;
        
            fastcgi_param SCRIPT_FILENAME $document_root/index.php;
        
            fastcgi_pass tp_backend;
        
            fastcgi_param HTTP_X_REAL_IP        $remote_addr;
        
            fastcgi_param HTTP_X_FORWARDED_FOR $proxy_add_x_forwarded_for;
        
            fastcgi_param HTTP_X_FORWARDED_PROTO $scheme;
        
            fastcgi_param HTTP_HOST            $host;
    
        }
    
    
        location ~ /\.ht {
        
            deny all;
    
        }
    
    }
        
    
  • 重载与验证

    • 检查配置:sudo nginx -t
    • 生效:sudo systemctl reload nginx
    • 验证:在各后端查看 access.log,确认请求被分发到不同实例
      以上 Nginx 负载均衡配置与部署步骤适用于 Ubuntu,包含 upstream、proxy 头与健康检查等关键点。

三 方案二 HAProxy 作为负载均衡器

  • 安装与启用

    • 安装:sudo apt update & & sudo apt install haproxy
    • 启用:sudo systemctl enable --now haproxy
  • 配置示例(/etc/haproxy/haproxy.cfg)

    • 使用 mode http,balance roundrobin,开启 check 健康检查
    • 通过 reqirep 将请求行改写为指向后端 public/index.php(适用于 PATH_INFO/FastCGI 场景)
    global
        log /dev/log local0
        log /dev/log local1 notice
        daemon
        maxconn 4096
    
    defaults
        log global
        mode http
        option httplog
        option dontlognull
        timeout connect 5000ms
        timeout client  50000ms
        timeout server  50000ms
    
    frontend http-in
        bind *:80
        bind *:443 ssl crt /etc/letsencrypt/live/your.domain.com/fullchain.pem
        redirect scheme https if !{
     ssl_fc }
        
        default_backend tp_servers
    
    backend tp_servers
        balance roundrobin
        option httpchk GET /health
        server app1 10.0.0.11:9000 check
        server app2 10.0.0.12:9000 check
    
    # 可选:将请求改写为 /index.php(适配部分直接到 PHP 的 FastCGI 转发)
    # reqirep ^([^\ ]*)\ /(.*) \1\ /index.php?$2
    
  • 重载与验证

    • 检查配置:sudo haproxy -c -f /etc/haproxy/haproxy.cfg
    • 生效:sudo systemctl reload haproxy
    • 验证:观察后端日志与 HAProxy 统计页面(stats enable)
      以上 HAProxy 配置与命令适用于 Ubuntu,包含前端监听、后端池、健康检查与可选重写规则。

四 ThinkPHP 多实例关键配置

  • 会话共享(推荐 Redis)
    • 配置 config/session.php:‘type’ => ‘cache’, ‘store’ => ‘redis’
    • 配置 config/cache.php:‘default’ => ‘redis’,并填写 Redis 连接信息
    • 确保各实例连接到同一 Redis 服务,避免登录状态不一致
  • 缓存与队列
    • 将默认缓存、队列等也指向 Redis/Memcached,保证多实例一致性
  • 静态资源
    • 建议由负载均衡器或 CDN 托管静态资源,减轻后端压力
  • 上传文件
    • 使用对象存储(如 七牛云)或 NFS/分布式文件系统,避免各实例本地写入不一致
      以上 Session/缓存共享与分布式文件实践是 ThinkPHP 分布式部署的核心环节。

五 测试与运维建议

  • 健康检查与摘除
    • Nginx:在 upstream 中使用 max_fails/fail_timeout;必要时配合主动健康检查模块
    • HAProxy:使用 option httpchk 或 TCP 检查,自动摘除异常实例
  • 压力测试
    • 使用 Apache Bench:ab -n 10000 -c 100 https://your.domain.com/
    • 观察各后端 access.log 的请求分布与错误率
  • 监控与告警
    • 监控负载均衡器与后端 QPS/延迟/5xx,结合 Prometheus + Grafana 可视化
  • 高可用进阶
    • 需要更高可用与更大规模时,可在前端引入 LVS + Keepalived 做四层负载均衡与 VIP 漂移
      以上健康检查、压测与监控优化方法可直接用于生产环境,LVS+Keepalived 适合更高并发与高可用场景。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: thinkphp项目在ubuntu上如何实现负载均衡
本文地址: https://pptw.com/jishu/757943.html
ubuntu中thinkphp日志如何查看与分析 ubuntu上thinkphp项目如何进行版本控制

游客 回复需填写必要信息