phpstudy多端口搭建多站点的核心是apache监听多个端口并为各端口配置独立virtualhost;需修改httpd.conf添加listen指令、编辑httpd-vhosts.conf绑定documentroot与端口、创建对应目录及测试文件,最后重启apache验证访问。

phpStudy 设置端口号搭建多站点,核心是让 Apache 同时监听多个端口,并为每个端口绑定独立的网站目录和域名。这种方式适合不想改 hosts 或避免域名冲突的场景,操作直接、调试方便。
确认 Apache 已启用多端口监听
打开 phpStudy 安装目录下的 Apache/conf/httpd.conf 文件,找到类似这一行:
Listen 80
在它下方手动添加你要用的其他端口,例如:
- Listen 8080
- Listen 8081
- Listen 8082
确保这些端口未被系统或其他软件占用(如 Skype、IIS、Docker 等)。可用命令 netstat -ano | findstr :8080 检查是否被占用。
配置每个端口对应的虚拟主机
编辑 Apache/conf/extra/httpd-vhosts.conf 文件,在末尾添加多个 <virtualhost></virtualhost> 块,例如:
-
<virtualhost></virtualhost>DocumentRoot "D:/www/site-a"ServerName site-a.local<directory> Require all granted </directory> -
<virtualhost></virtualhost>DocumentRoot "D:/www/site-b"ServerName site-b.local<directory> Require all granted </directory>
注意:ServerName 可填任意域名(如 test1.dev),但仅作标识,实际访问仍靠端口;若想用域名访问,还需配合 hosts 文件映射。
创建网站目录并放置测试文件
按配置中 DocumentRoot 的路径新建对应文件夹,比如 D:/www/site-a,并在其中放一个 index.php:
<?php echo 'Site A running on port 8080'; ?>
同理为 site-b 准备内容。确保路径无中文、空格或特殊符号,避免 Apache 权限报错。
重启服务并验证访问
在 phpStudy 主界面点击重启 Apache(不是仅重启 MySQL)。
浏览器中分别访问:
- http://127.0.0.1:8080 → 应显示 site-a 内容
- http://localhost:8081 → 应显示 site-b 内容
如果打不开,检查 Apache 日志(logs/error.log)是否有语法错误或权限拒绝提示;常见问题是 Require all granted 缺失或路径拼写错误。
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!











