phpEnv本身不处理域名绑定,需手动配置hosts文件和Apache虚拟主机;hosts必须添加127.0.0.1 sitea.test等映射并执行ipconfig /flushdns,Apache的httpd-vhosts.conf中每个站点需独立VirtualHost块,ServerName与hosts严格一致,DocumentRoot指向public目录,且<Directory>路径匹配并含Require all granted。

phpEnv 本身不处理域名绑定,它只是 Windows 下的 PHP 运行环境集成包(类似 XAMPP、WampServer),真正的多站点必须靠 Apache/Nginx 配置 + hosts 文件配合实现。你改 phpEnv 的界面设置或“站点管理”按钮,本质都是在帮你自动生成或修改 Apache 的 httpd-vhosts.conf 和系统 hosts,底层逻辑和原生配置完全一致。
hosts 文件必须先配对,否则所有域名都解析失败
这是最常被跳过的一步,也是后续一切失效的根源。phpEnv 不会自动改 hosts,你得手动加:
- 用管理员权限打开
C:\Windows\System32\drivers\etc\hosts - 追加至少两行(不能有 tab,不能带
http://,不能写端口):127.0.0.1 sitea.test 127.0.0.1 siteb.test
- 保存后立即执行
ipconfig /flushdns刷新 DNS 缓存 —— 不执行这步,浏览器仍走公网 DNS,本地绑定无效
Apache 虚拟主机配置要独立且路径准确
phpEnv 默认启用 conf/extra/httpd-vhosts.conf,但不会自动为你建多个 <VirtualHost> 块。你得手动编辑该文件,每个站点一个块:
-
ServerName必须和 hosts 里写的域名**完全一致**(包括大小写、有无 www) -
DocumentRoot要指向项目真实的public或web入口目录,不是整个项目根目录(比如 ThinkPHP 是/path/to/thinkphp/public,不是/path/to/thinkphp) -
<Directory>路径必须和DocumentRoot一致,且必须包含Require all granted(旧版 Apache 用Allow from all) - 确保
httpd.conf中Include conf/extra/httpd-vhosts.conf这行没被注释
示例(两个站点):
<VirtualHost *:80>
ServerName sitea.test
DocumentRoot "D:/phpEnv/www/sitea/public"
<Directory "D:/phpEnv/www/sitea/public">
Require all granted
AllowOverride All
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName siteb.test
DocumentRoot "D:/phpEnv/www/siteb/public"
<Directory "D:/phpEnv/www/siteb/public">
Require all granted
AllowOverride All
</Directory>
</VirtualHost>
phpEnv 界面操作容易掩盖真实问题
phpEnv 的“网站管理”功能会生成配置并重启 Apache,但它不校验路径是否存在、不提示 DocumentRoot 权限错误、也不检查 hosts 是否生效。常见故障现象:
立即学习“PHP免费学习笔记(深入)”;
- 访问
sitea.test显示 phpEnv 默认首页 → hosts 没生效 或ServerName拼错 - 报 403 Forbidden →
<Directory>路径和DocumentRoot不一致,或缺少Require all granted - 报 404 且日志显示找不到 index.php →
DocumentRoot指向了项目根目录,而非 public 目录 - 两个域名都指向同一个站点 → 两个
<VirtualHost>的ServerName写成一样,或 Apache 没真正重载(需点 phpEnv 界面的“重启 Apache”,不是只点“保存”)
真正关键的只有三件事:hosts 映射正确、Apache 每个 ServerName 独立且路径精准、每次改完都彻底重启 Apache。其他图形界面操作只是快捷方式,不能替代对这两份配置文件的理解和验证。



















