Host文件是用于DNS解析,本地的host文件优于网络上的DNS解析。找到目录C:\Windows\System32\drivers\etc下的host文件,用记事本打开在后面添加一行 127.0.0.1 www.test.com。
如何在WampServer中Apache配置虚拟主机方法
找到Apache下的httpd.conf文件,把虚拟主机开启。(如我的在:C:\wamp64\bin\apache\apache2.4.23\conf下)。
在httpd.conf文件中找到 #Include conf/extra/httpd-vhosts.conf这句话,将前面的#号去掉。
如何在WampServer中Apache配置虚拟主机方法
找到Apach下的httpd-vhosts.conf 一般是在apach安装目录下的conf目录下(如:C:\wamp64\bin\apache\apache2.4.23\conf\extra)
在httpd-vhosts.conf文件里添加如下代码
<VirtualHost *:80>
#网站根目录
DocumentRoot "F:/test"
#域名
ServerName www.test.com
#这里配置欢迎首页面
DirectoryIndex index.html index.htm index.php
<Directory />
Options FollowSymLinks
#不允许别人修改我们的页面
AllowOverride None
#设置访问权限
order allow,deny
Allow from all
</Directory>
如何在WampServer中Apache配置虚拟主机方法
重启服务器,在网站目录根目录下新建1个index.php文件(我的在F:/test里面)。在index.php里写一句echo “这是网站首页!”,用于测试。
如何在WampServer中Apache配置虚拟主机方法
在浏览器输入自己设置的域名,出现如下说明设置成功。
如何在WampServer中Apache配置虚拟主机方法
如果出现如下情况(浏览器状态码为403),就是因为权限不足引起的,再次打开httpd.conf进行添加权限
如何在WampServer中Apache配置虚拟主机方法
找到下面的代码
AllowOverride none
Require all denied
将它改为:
AllowOverride none
Require all granted
就可以了
如何在WampServer中Apache配置虚拟主机方法
END
注意事项
出现浏览器403状态一般是没有权限访问
出现浏览器404状态是没找到页面,检查文件所在路径是否正确