必须同时修改httpd.conf和httpd-vhosts.conf:在httpd.conf中添加listen端口并启用include行,在httpd-vhosts.conf中配置含directory权限的virtualhost,否则将出现403、500或连接被拒绝错误。

能配,但必须同时改 httpd.conf 和 httpd-vhosts.conf,漏掉任一环节都会 403、500 或直接端口不响应。
Listen 指令必须显式添加,不能只靠 VirtualHost
Apache 不会自动为未声明的端口启动监听。只在 httpd-vhosts.conf 里写 <virtualhost></virtualhost>,但没在 httpd.conf 里加 Listen 8080,浏览器访问 http://localhost:8080 就是连接被拒绝(ERR_CONNECTION_REFUSED)。
- 打开
C:\XAMPP\apache\conf\httpd.conf - 找到
Listen 80行,在其下方新增你需要的端口,每行一个:Listen 8080Listen 8081Listen 8001 - 端口号避开 1–1023(系统保留),也别用已被其他程序占用的(比如 Skype 默认占 80/443,QQ 占 8000)
VirtualHost 必须带 Directory 权限配置,否则 403 是常态
新版 Apache(2.4+)默认禁止所有目录访问,httpd-vhosts.conf 里只写 DocumentRoot 和 ServerName,不配 <directory></directory> 块,99% 触发 Access forbidden! 错误。
XAMPP 8.0.30 是一款免费、开源的跨平台 Web 服务器集成包,专为快速搭建本地 PHP 开发环境而设计。该版本核心组件包括:Apache 2.4.56、MySQL 8.0.33、PHP 8.0.30、phpMyAdmin 5.2.1 等。它支持 Windows、Linux 和 macOS 系统,可让开发者在个人电脑上轻松模拟服务器环境,无需复杂配置即可运行 WordPress、Thin
- 在
C:\XAMPP\apache\conf\extra\httpd-vhosts.conf底部追加完整块(以 8080 端口为例):<virtualhost> ServerName localhost DocumentRoot "C:/xampp/htdocs/project-a" <directory> Options Indexes FollowSymLinks AllowOverride All Require all granted </directory></virtualhost> -
Require all granted是关键,不是Allow from all(那是 2.2 语法,XAMPP 7.4+ 默认用 2.4) - 路径用正斜杠
/,Windows 下反斜杠\容易引发解析错误
别忘了开启 vhosts 配置加载
很多教程跳过这步,结果改完两个文件重启 Apache 也没反应——因为 httpd.conf 默认注释掉了虚拟主机配置的引入行。
- 在
httpd.conf中搜索Include conf/extra/httpd-vhosts.conf - 确认该行前面没有
#,即确保它是:Include conf/extra/httpd-vhosts.conf - 如果被注释了(
#Include conf/extra/httpd-vhosts.conf),删掉开头的#并保存
localhost 访问失效?你可能覆盖了默认站点
一旦启用 httpd-vhosts.conf,Apache 会忽略 httpd.conf 中的全局 DocumentRoot,所有未匹配 ServerName 的请求(比如纯 http://localhost)会落到第一个 <virtualhost></virtualhost> 上,而它很可能指向了某个子目录,导致原 htdocs 不再可访问。
- 在
httpd-vhosts.conf开头或结尾显式补上本地默认站:<virtualhost> ServerName localhost DocumentRoot "C:/xampp/htdocs" <directory> Options Indexes FollowSymLinks AllowOverride All Require all granted </directory></virtualhost> - 否则,
http://localhost可能 403 或跳转到某个项目根目录下报错
最容易被忽略的是权限配置和 Include 行开关——它们不出现在任何错误日志里,只静默失败。每次改完务必检查 Apache 启动日志(XAMPP 控制面板 → Apache → Logs),看有没有 Invalid command 'Require'(说明用了 2.4 语法但 Apache 实际是 2.2)或 VirtualHost *:8080 -- mixing * ports and non-* ports with a NameVirtualHost address is not supported(说明混用了 NameVirtualHost,新版已废弃,直接删掉)。










