Apache静态网站部署中,用虚拟主机实现多站点管理,核心是根据请求域名(Host头)将流量分发到不同目录;通过ServerName和DocumentRoot区分站点,无需额外IP或端口,配置清晰、运维简单。

Apache 静态网站部署中,用虚拟主机实现多站点管理,核心是让同一个 Apache 实例根据请求的域名(Host 头)把流量分发到不同目录。不需要额外 IP 或端口,靠 ServerName 和 DocumentRoot 就能区分站点,配置清晰、运维简单。
确认并启用必要模块
Apache 2.4 默认已启用关键模块,但需检查以下两项是否生效:
-
mod_rewrite:支持 .htaccess 重写规则(如伪静态、跳转),建议启用 -
mod_ssl(若需 HTTPS):提前加载,避免后续配证书时报错
在主配置文件(如 /etc/apache2/apache2.conf 或 D:\Apache24\conf\httpd.conf)中确认无注释:
LoadModule rewrite_module modules/mod_rewrite.so<br>LoadModule ssl_module modules/mod_ssl.so
创建独立的站点配置文件
不建议直接改主配置文件,推荐为每个站点单独建配置(便于启停和排查)。例如:
- Debian/Ubuntu:放在
/etc/apache2/sites-available/,如site1.conf - CentOS/RHEL:可统一放
/etc/httpd/conf.d/site1.conf - Windows:建议新建
conf/extra/vhosts-site1.conf并在httpd.conf中Include
内容示例(监听 80 端口,支持 www 别名):
Apache Superset 是一个广泛采用的开源 BI 平台,用于 SQL 探索、图表构建和仪表板交付。当代理需要查询仓库数据、组装仪表板或使用成熟的分析界面解释指标而不是临时笔记本代码时,此技能非常有用。
<VirtualHost *:80><br> ServerName site1.example.com<br> ServerAlias www.site1.example.com<br> DocumentRoot "/var/www/site1"<br> <Directory "/var/www/site1"><br> Options Indexes FollowSymLinks<br> AllowOverride None<br> Require all granted<br> </Directory><br> ErrorLog ${APACHE_LOG_DIR}/site1_error.log<br> CustomLog ${APACHE_LOG_DIR}/site1_access.log combined<br></VirtualHost>
设置目录权限与日志分离
每个站点必须有明确的访问控制,否则可能因权限不足返回 403 错误:
-
DocumentRoot对应的目录需被 Apache 进程用户(如www-data或apache)可读 -
<Directory>块中必须包含Require all granted(Apache 2.4+ 写法) - 为避免日志混杂,给每个站点设独立
ErrorLog和CustomLog路径
常见权限修复命令(Linux):
sudo chown -R $USER:www-data /var/www/site1<br>sudo chmod -R 755 /var/www/site1
启用站点并验证配置
完成配置后,分三步操作:
- 语法检查:
sudo apache2ctl configtest(或httpd -t)——必须显示Syntax OK - 启用站点(Debian/Ubuntu):
sudo a2ensite site1.conf;(CentOS)直接确保 conf 文件在conf.d/下即可 - 重启服务:
sudo systemctl restart apache2(或httpd)
本地测试时,在 /etc/hosts(Windows 是 C:\Windows\System32\drivers\etc\hosts)添加:
127.0.0.1 site1.example.com www.site1.example.com<br>127.0.0.1 site2.example.com
浏览器访问对应域名,即可看到各自根目录下的 index.html。

















