在 CentOS 上,通常使用的是 Apache HTTP Server(httpd),而不是 Debian/Ubuntu 中的 apache2 名称。下面以 CentOS 7/8 为例,说明如何配置 Apache 虚拟主机(Virtual Host)。
# CentOS 7
yum install httpd -y
# CentOS 8
dnf install httpd -y
启动并设置开机自启:
systemctl start httpd
systemctl enable httpd
mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.bak
例如创建一个站点 example.com:
mkdir -p /var/www/example.com/public_html
chown -R apache:apache /var/www/example.com
chmod -R 755 /var/www/example.com
创建测试页面:
echo "<h1>example.com</h1>" > /var/www/example.com/public_html/index.html
在 CentOS 中,虚拟主机一般放在:
/etc/httpd/conf.d/
新建配置文件:
vi /etc/httpd/conf.d/example.com.conf
内容示例:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
<Directory /var/www/example.com/public_html>
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/httpd/example.com_error.log
CustomLog /var/log/httpd/example.com_access.log combined
</VirtualHost>
httpd -t
systemctl restart httpd
修改本地 hosts 文件(Windows / Linux / macOS):
192.168.1.100 example.com
浏览器访问:
http://example.com
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
如果页面打不开:
setsebool -P httpd_read_user_content 1
或临时关闭测试:
setenforce 0
可使用 Let’s Encrypt:
yum install certbot python2-certbot-apache -y
certbot --apache -d example.com -d www.example.com
如果你用的是 CentOS + apache2(非 httpd) 或 Docker / 面板环境,可以告诉我具体场景,我可以给你更精确的配置。