(含phpstudy)Windows下配置虚拟域名总结

时间:2022-10-23 08:13:08

(含phpstudy)Windows下配置虚拟域名总结

大家好,我是Yangrl,今天正好配虚拟域名。本以为几分钟搞定,结果又掉坑里了!
不说废fa ↓
环境:集成环境phstudy
操作系统: windows 10
1):修改hosts文件,添加虚拟域名->C:\Windows\System32\drivers\etc\hosts(含phpstudy)Windows下配置虚拟域名总结
在hosts文件最下面添加你的地址和对应的虚拟域名,比如:127.0.0.1 test.com

2):找到配置文件 httpd.conf
位置在你安phpstudy的盘符比如 F:\phpStudy\PHPTutorial\Apache\conf
(含phpstudy)Windows下配置虚拟域名总结
打开 httpd.conf 文件,ctrl+f 搜索 httpd-vhost ,就会找到 Include conf/extra/httpd-vhosts.conf,删掉前面的注释“#”,然后再搜索 servername 找到下图,注释(在前面加上#)后面会用到↓
(含phpstudy)Windows下配置虚拟域名总结

3):第一个坑,很多教程都叫去同级目录 extra 目录中找 httpd-vhosts.conf 修改,但在phpstudy中,就是同级目录中的vhosts.conf文件
(含phpstudy)Windows下配置虚拟域名总结
(含phpstudy)Windows下配置虚拟域名总结

continue->修改vhosts文件(原内容全部删掉),复制修改为自己的信息即可(我写的注释记得删掉)

#1
<VirtualHost *:80>
DocumentRoot "F:\phpStudy\PHPTutorial\WWW"
ServerName localhost
ServerAlias
<Directory "F:\phpStudy\PHPTutorial\WWW">
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>

#2
<VirtualHost *:80>
DocumentRoot "F:\phpStudy\PHPTutorial\WWW"
ServerName 127.0.0.1
ServerAlias
<Directory "F:\phpStudy\PHPTutorial\WWW">
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>

#3
<VirtualHost *:80>
DocumentRoot "F:\phpStudy\PHPTutorial\WWW\hopes\tp5\public" //目标目录
ServerName community.hope.com //hosts文件中添加的虚拟域名
</VirtualHost>
<Directory "F:\phpStudy\PHPTutorial\WWW\hopes\tp5\public"> //目标目录
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>

(PS:为什么要这么写?因为先前我们去掉httpd-conf中的 #Include conf/extra/httpd-vhosts.conf 的#之后,虽然设置的虚拟域名可以访问了,但是原来通过localhost和127.0.0.1的方式访问却会404,因为想继续以这两种形式进行访问,所以才会有之前我们注释servername的步骤,然后#1,#2也就是把这两种形式也设置成了虚拟站点,得以继续访问。不要忘了在hosts文件中也添加127.0.0.1 localhost 哦!我是之前就有的。那这是我踩坑后得出的结论,每个人可能遇到情况都不太一样,如果您有更好更简便的方式,或者有不同的意见请在下面评论区分享出来,我会非常开心的O(∩_∩)O)
重启Apache,成功!

4):还可以直接通过phpstudy中的站点域名管理快速配置,我的有问题,所以跳过。
(含phpstudy)Windows下配置虚拟域名总结

END.

只要不杀到他床上去,他宁可闭着眼假寐。