温馨提示×

centos怎样部署apache集群

小樊
33
2026-09-09 00:04:42
栏目: 智能运维

CentOS 上部署 Apache 集群,通常指 多台 Apache 服务器 + 负载均衡(高可用) 的架构。下面我按最常见、实用的方案给你讲,从简单到高可用。


一、典型 Apache 集群架构

客户端
  ↓
负载均衡(Nginx / HAProxy / LVS)
  ↓
Apache 节点1  Apache 节点2  Apache 节点3

常见组合:

  • Nginx + Apache(最常用)
  • HAProxy + Apache
  • LVS + Keepalived + Apache(生产级高可用)

二、基础环境准备(所有节点)

1. 系统

cat /etc/centos-release

2. 关闭防火墙(测试环境)或放通端口

systemctl stop firewalld
systemctl disable firewalld

生产建议:

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

3. 安装 Apache(所有 Web 节点)

yum install -y httpd
systemctl enable httpd
systemctl start httpd

4. 测试页面

echo "Node1" > /var/www/html/index.html

(每台机器内容不同,方便区分)


三、方案 1:Nginx 做负载均衡 + Apache 集群(推荐入门)

1. 安装 Nginx(负载均衡节点)

yum install -y nginx

2. 配置负载均衡

vi /etc/nginx/nginx.conf
http {
    upstream apache_cluster {
        server 192.168.1.11:80;
        server 192.168.1.12:80;
        server 192.168.1.13:80;
    }

    server {
        listen 80;
        location / {
            proxy_pass http://apache_cluster;
        }
    }
}

3. 启动 Nginx

systemctl enable nginx
systemctl start nginx

访问 Nginx IP,就能看到 Apache 节点轮询响应。


四、方案 2:HAProxy + Apache(更专业)

1. 安装 HAProxy

yum install -y haproxy

2. 配置

vi /etc/haproxy/haproxy.cfg
frontend http_front
    bind *:80
    default_backend apache_back

backend apache_back
    balance roundrobin
    server apache1 192.168.1.11:80 check
    server apache2 192.168.1.12:80 check
    server apache3 192.168.1.13:80 check

3. 启动

systemctl enable haproxy
systemctl start haproxy

五、方案 3:高可用(Keepalived + LVS / Nginx)

防止负载均衡单点故障。

1. 安装 Keepalived

yum install -y keepalived

2. 主节点配置

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 150
    virtual_ipaddress {
        192.168.1.100
    }
}

3. 备节点

  • state BACKUP
  • priority 100

VIP(192.168.1.100)会自动漂移。


六、Session 问题解决(重点)

Apache 集群 必须处理 Session

方案 1:Session 共享(推荐)

  • Redis / Memcached
yum install -y redis

PHP 示例:

session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379"

方案 2:IP Hash(不推荐大规模)

upstream apache_cluster {
    ip_hash;
    server 192.168.1.11;
}

七、文件同步(可选)

方案

  • rsync
  • NFS
  • GlusterFS

示例(rsync):

rsync -avz /var/www/html/ node2:/var/www/html/

八、生产建议

✅ 至少 2 台 Apache
✅ 负载均衡高可用
✅ 日志集中(ELK)
✅ 监控(Zabbix / Prometheus)


如果你愿意,可以告诉我:

  • CentOS 版本(7 / 8 / Stream)
  • Apache 用途(PHP / 静态 / 反向代理)
  • 是否要 公网 + HTTPS

我可以给你 完整可执行的部署脚本

0