在 CentOS 上部署 Apache 集群,通常指 多台 Apache 服务器 + 负载均衡(高可用) 的架构。下面我按最常见、实用的方案给你讲,从简单到高可用。
客户端
↓
负载均衡(Nginx / HAProxy / LVS)
↓
Apache 节点1 Apache 节点2 Apache 节点3
常见组合:
cat /etc/centos-release
systemctl stop firewalld
systemctl disable firewalld
生产建议:
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
yum install -y httpd
systemctl enable httpd
systemctl start httpd
echo "Node1" > /var/www/html/index.html
(每台机器内容不同,方便区分)
yum install -y nginx
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;
}
}
}
systemctl enable nginx
systemctl start nginx
访问 Nginx IP,就能看到 Apache 节点轮询响应。
yum install -y haproxy
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
systemctl enable haproxy
systemctl start haproxy
防止负载均衡单点故障。
yum install -y keepalived
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
virtual_ipaddress {
192.168.1.100
}
}
state BACKUPpriority 100VIP(192.168.1.100)会自动漂移。
Apache 集群 必须处理 Session:
yum install -y redis
PHP 示例:
session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379"
upstream apache_cluster {
ip_hash;
server 192.168.1.11;
}
示例(rsync):
rsync -avz /var/www/html/ node2:/var/www/html/
✅ 至少 2 台 Apache
✅ 负载均衡高可用
✅ 日志集中(ELK)
✅ 监控(Zabbix / Prometheus)
如果你愿意,可以告诉我:
我可以给你 完整可执行的部署脚本。