在 CentOS 上让 Apache 绑定(绑定域名 / 绑定 IP / 绑定端口),通常分几种常见场景。下面按最常见情况给你说明。
systemctl status httpd
# 没装的话
yum install httpd -y
systemctl start httpd
systemctl enable httpd
vi /etc/httpd/conf.d/example.conf
内容示例(绑定 www.example.com):
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/example
ErrorLog /var/log/httpd/example-error.log
CustomLog /var/log/httpd/example-access.log combined
</VirtualHost>
创建网站目录:
mkdir -p /var/www/example
echo "hello example" > /var/www/example/index.html
httpd -t
systemctl reload httpd
在 DNS 控制台把:
www.example.com → 服务器公网IP
如果服务器有多个 IP:
<VirtualHost 192.168.1.100:80>
ServerName www.example.com
DocumentRoot /var/www/example
</VirtualHost>
vi /etc/httpd/conf/httpd.conf
找到:
Listen 80
改为:
Listen 8080
或增加:
Listen 8080
<VirtualHost *:8080>
ServerName www.example.com
DocumentRoot /var/www/example
</VirtualHost>
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload
✅ SELinux(CentOS 默认开启)
setsebool -P httpd_can_network_connect 1
✅ 目录权限
chown -R apache:apache /var/www/example
chmod -R 755 /var/www/example
✅ 查看错误
tail -f /var/log/httpd/error_log
| 需求 | 配置位置 |
|---|---|
| 绑定域名 | ServerName |
| 绑定 IP | <VirtualHost IP:端口> |
| 绑定端口 | Listen |
| 多站点 | 多个 VirtualHost |
如果你是指 “CentOS Apache 绑定 HTTPS / 绑定多个域名 / 绑定内网”,可以告诉我具体场景,我可以直接给你完整配置。