PHPStudy配置虚拟主机需三步:一、修改hosts文件绑定域名到127.0.0.1;二、在httpd-vhosts.conf中添加VirtualHost块并设置DocumentRoot与ServerName;三、取消httpd.conf中vhosts引用注释后重启Apache服务。
在 PHPStudy 中配置虚拟主机,主要是为了让本地能同时运行多个网站(比如 site1.test、site2.test),每个站点对应独立的域名和项目目录。关键在于三步联动:修改 hosts 文件绑定域名、配置 Apache/Nginx 的虚拟主机规则、重启服务生效。
让浏览器把自定义域名指向本机(127.0.0.1),这是访问的前提。
he 虚拟主机(推荐新手用 Apache)PHPStudy 默认用 Apache,配置文件路径为:PHPServer\Apache\conf\extra\httpd-vhosts.conf
ServerAdmin webmaster@localhost DocumentRoot "D:/www/site1" # 你的第一个站点根目录 ServerName site1.test Options Indexes FollowSymLinks AllowOverride All Require all granted
ServerAdmin webmaster@localhost DocumentRoot "D:/www/site2" # 第二个站点目录 ServerName site2.test Options Indexes FollowSymLinks AllowOverride All Require all granted
光写配置不生效,还要确认 Apache 已加载 vhosts 文件。
Nginx 配置位置不同,且语法更简洁:
server {
listen 80;
server_name site1.test;
root "D:/www/site1";
location / {
index index.php index.html error/index.html;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
}
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}