phpEnv是一款集成了PHP、MySQL、Apache、Nginx等多种开发组件的本地环境搭建工具,很多开发者在开发多个项目时,需要通过不同的域名来区分各个站点,这就需要配置phpEnv的虚拟站点功能。下面分别介绍Apache和Nginx两种服务器下的多域名配置方法。
一、配置前的准备工作
在正式配置虚拟站点之前,需要先完成两项基础工作:
- 创建各个站点对应的项目目录,比如在phpEnv安装目录下的
www文件夹中,分别新建site1和site2两个文件夹,作为两个不同域名的站点根目录。 - 准备好要绑定的域名,本地测试可以使用自定义的非公网域名,比如
test1.local和test2.local。
二、Apache服务器下配置多域名
phpEnv默认如果使用的是Apache服务器,可以按照以下步骤配置虚拟主机:
1. 修改Apache虚拟主机配置文件
打开phpEnv安装目录,找到Apache的虚拟主机配置文件,路径通常为phpEnvapacheconfextrahttpd-vhosts.conf,在文件末尾添加如下配置:
# 第一个虚拟站点配置
<VirtualHost *:80>
# 绑定的域名
ServerName test1.local
# 可选的别名
ServerAlias www.test1.local
# 站点根目录
DocumentRoot "D:/phpEnv/www/site1"
# 目录权限配置
<Directory "D:/phpEnv/www/site1">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
# 第二个虚拟站点配置
<VirtualHost *:80>
ServerName test2.local
ServerAlias www.test2.local
DocumentRoot "D:/phpEnv/www/site2"
<Directory "D:/phpEnv/www/site2">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
2. 重启Apache服务
修改完配置文件后,打开phpEnv控制面板,点击Apache对应的重启按钮,让配置生效。
三、Nginx服务器下配置多域名
如果phpEnv使用的是Nginx服务器,配置逻辑和Apache略有不同:
1. 修改Nginx站点配置文件
找到Nginx的配置文件目录,路径通常为phpEnvnginxconfvhost,新建两个配置文件,比如test1.local.conf和test2.local.conf,分别写入如下内容:
# test1.local.conf 内容
server {
# 监听端口
listen 80;
# 绑定的域名
server_name test1.local www.test1.local;
# 站点根目录
root "D:/phpEnv/www/site1";
# 默认首页
index index.php index.html index.htm;
# PHP解析配置
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
第二个配置文件test2.local.conf只需要把域名和根目录替换为对应的值即可:
server {
listen 80;
server_name test2.local www.test2.local;
root "D:/phpEnv/www/site2";
index index.php index.html index.htm;
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
2. 重启Nginx服务
保存配置文件后,在phpEnv控制面板中重启Nginx服务,让配置生效。
四、配置本地hosts文件映射
不管是Apache还是Nginx,都需要修改系统的hosts文件,把自定义域名指向本地回环地址。hosts文件路径为C:WindowsSystem32driversetchosts,在文件末尾添加如下内容:
127.0.0.1 test1.local 127.0.0.1 www.test1.local 127.0.0.1 test2.local 127.0.0.1 www.test2.local
保存hosts文件后,需要刷新DNS缓存,打开命令提示符执行ipconfig /flushdns命令即可。
五、验证配置是否生效
分别在site1和site2目录下新建index.php文件,写入不同的测试内容:
<?php // site1目录下的index.php echo "这是test1.local站点"; ?>
<?php // site2目录下的index.php echo "这是test2.local站点"; ?>
打开浏览器分别访问http://test1.local和http://test2.local,如果分别显示对应的内容,说明多域名配置成功。
六、常见问题排查
- 如果访问域名提示无法连接,首先检查phpEnv对应的Web服务是否正常运行,然后检查hosts文件是否修改正确,是否有语法错误。
- 如果访问域名显示403 forbidden,检查站点目录的权限是否配置正确,确保Web服务器有读取目录的权限。
- 如果PHP文件无法解析,检查PHP服务是否正常运行,以及虚拟主机配置中的PHP解析规则是否正确。
phpEnv多域名配置虚拟站点Apache_virtual_hostNginx_server_block修改时间:2026-06-23 22:24:46