因为需要测试ajax的跨域操作,所以在本地搭建一个虚拟服务器,也碰到一些问题,不过最终还是解决了,如果你也想搭建虚拟服务器,可以按以下步骤进行操作。



httpd-vhosts.conf

找到httpd-vhosts.conf,我的文件在F:\Software\wamp\bin\apache\apache2.2.22\conf\extra下,作为一个coder分工总是令人蛋疼地明确,理所当然,你也可以直接在httpd.conf中设置,不过我建议你不要这么做。

将里面的内容用”#”全部注释掉,然后加入以下内容

1
2
3
4
5
6
7
8
9
10
11
12
<VirtualHost 127.0.0.2:8080>
    DocumentRoot F:/Software/wamp/virtual
   ServerName virtual

 DirectoryIndex index.html
<Directory "F:/Software/wamp/virtual">
    Options Indexes FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>
</VirtualHost>

因为这里端口我用了8080,如果你是80的话,你就要写成127.0.0.2:80,DocumentRoot是你虚拟主机的根目录,这里我就不多说了。

httpd-conf

我一开始以为只要在httpd-vhosts.conf改了就可以了,没想到主配置文件没有包含,找到Include conf/extra/httpd-vhosts.conf,将前面的”#”去掉。



hosts

最后一步了,如果你玩过代理翻墙,那么你就可能会知道(不排除你用自由门)需要在hosts文件中修改IP指向那个你需要翻墙的网址,这里咱们就要设置一下将IP指向咱们的虚拟目录。添加以下内容

127.0.0.2 virtual

You don’t have permission to access / on this server.

这里提一个小插曲,你可以会被服务器拒绝说没有权限,在httpd-conf的文件中找到

1
2
3
4
5
6
<Directory />
    Options FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all 
</Directory>

改成

1
2
3
4
<Directory />
    Options Indexes FollowSymLinks
    AllowOverride None
</Directory>


OK!!!重启一下开始新的旅程吧