需通过修改hosts文件映射域名、配置web服务器虚拟主机(apache的virtualhost或nginx的server块)并验证http_host一致性来实现多php项目独立域名访问。

如果您在本地运行 PHP 项目,希望为不同项目分配独立域名(例如 site1.test、site2.test)并使其指向各自独立的目录,则必须通过系统 hosts 文件映射、Web 服务器虚拟主机配置协同完成。以下是具体操作步骤:
一、修改系统 hosts 文件添加本地域名映射
该步骤使操作系统将自定义域名强制解析为 127.0.0.1,从而将浏览器请求导向本地 Web 服务。此为所有多域名访问的基础前提,缺少则后续配置无效。
1、以管理员身份运行记事本(Windows)或终端中使用 sudo nano(macOS/Linux)打开 hosts 文件。
2、Windows 路径为 C:\Windows\System32\drivers\etc\hosts;macOS/Linux 路径为 /etc/hosts。
3、在文件末尾新增如下行(每行一个域名,IP 与域名间用空格分隔,不可用 Tab):127.0.0.1 site1.test 127.0.0.1 site2.test
4、保存文件;若提示权限拒绝,请确认编辑器已以管理员/root 权限运行。
5、刷新 DNS 缓存:Windows 执行 ipconfig /flushdns;macOS 执行 sudo dscacheutil -flushcache;Linux 执行 sudo systemd-resolve --flush-caches。
二、Apache 环境下配置多个 VirtualHost
Apache 通过 VirtualHost 指令区分不同域名的请求,并将其指向对应项目目录。需确保 httpd-vhosts.conf 已启用且监听 80 端口,DocumentRoot 必须指向项目的 public/ 目录(尤其对 ThinkPHP 等框架),否则将触发 403/404/500 错误。
1、打开 Apache 配置目录中的 httpd-vhosts.conf(通常位于 phpEnv\Apache\conf\extra\ 或 XAMPP\apache\conf\extra\ 下)。
2、确认 Apache 主配置 httpd.conf 中已取消注释以下两行:Include conf/extra/httpd-vhosts.conf 与 Listen 80。
3、在 httpd-vhosts.conf 文件末尾添加两个独立 VirtualHost 块(路径请按实际项目位置修改):
ServerName site1.test
DocumentRoot "D:/phpEnv/www/site1/public"
Require all granted
AllowOverride All
ServerName site2.test
DocumentRoot "D:/phpEnv/www/site2/public"
Require all granted
AllowOverride All
4、保存文件后,通过 phpEnv 控制面板或命令行执行 httpd -t 验证语法正确性,再重启 Apache 服务。
三、Nginx 环境下配置多个 server 块
Nginx 使用 server 块按 server_name 匹配请求,并将流量路由至指定 root 目录。每个 server 块必须显式声明 fastcgi_param SCRIPT_FILENAME,否则 PHP 脚本无法正确定位,导致空白页或 502 错误。
1、打开 Nginx 主配置文件 nginx.conf(通常位于 nginx/conf/nginx.conf)。
2、确认 http 块内包含:include sites-enabled/*.conf;(若无,可手动添加并创建 sites-enabled 目录)。
3、在 sites-enabled 目录下新建两个文件:site1.conf 和 site2.conf。
4、site1.conf 内容如下:
server {
listen 80;
server_name site1.test;
root D:/phpEnv/www/site1/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
}
}
5、site2.conf 内容如下:
server {
listen 80;
server_name site2.test;
root D:/phpEnv/www/site2/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
}
}
6、保存全部文件后,执行 nginx -t 验证配置,再执行 nginx -s reload 重载服务。
四、验证域名与路径一致性
Web 服务器配置生效后,PHP 层必须使用 $_SERVER['HTTP_HOST'] 获取原始 Host 头值进行逻辑判断,不可依赖 $_SERVER['SERVER_NAME'](该值来自服务器配置,无法反映真实请求域名)。若三者(hosts 映射域名、ServerName/server_name 值、PHP 中读取的 HTTP_HOST)不完全一致(包括大小写与子域结构),将导致 404 或 502 错误。
1、在 site1.test 对应项目的 public/index.php 开头添加:var_dump($_SERVER['HTTP_HOST']); die();
2、在浏览器中访问 http://site1.test,确认输出为 string(10) "site1.test"。
3、同理测试 site2.test,确认输出为 string(10) "site2.test"。
4、若任一输出不符,请回溯检查 hosts 行拼写、VirtualHost/Server 块中 ServerName/server_name 字符串、以及浏览器地址栏输入是否含端口或协议前缀。
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!











