本地Apache绑定多域名

来源:互联网 发布:暴雪游戏平台网络错误 编辑:程序博客网 时间:2024/05/02 13:43
在本地开发的时候,经常要实现通过访问不同的xxx.xxx.com来访问本地多个不同网站而不是多个xxx.xxx.com/xxx/,在网上看了一大堆文章,都是copy的,无用的配置一大堆,而且会拦截原有域名,比如研究test,绑定www.test.com做本地测试,就访问不了test官方网站来寻找资料,十分不便,但是采取local.test.com或其他xxx.test.com的方式,则即不影响域名和配置的直观效果,又不影响test官网的访问。

说配置:

打开c:\windows\system32\drivers\etc\hosts文件,编辑内容如下:

127.0.0.1       localhost
192.168.1.101    local.test.com

第一行本来就存在,不管;如果apache服务器网站只是你一个人本机访问做测试,那么第二行前面也可以写成127.0.0.1,如果局域网也要访问,比如公司同事,则可以写成本机内网ip,可以通过本地运行cmd,敲入ipconfig命令来获得,不过最好是设置了静态ip。局域网用户需要访问local.test.com,同样需要在他电脑的c:\windows\system32\drivers\etc\hosts里面加入第二行。

打开Apache配置文件,一般在Apache安装目录下/config/httpd.conf,编辑前先做备份。

查找<Directory />,找到如下内容并修改为,不然可能会因权限问题而出现错误页面:

<Directory />
    Options FollowSymLinks ExecCGI Indexes
    AllowOverride None
   #Order deny,allow
    #Deny from all
   Order allow,deny
    Allow from all
    Satisfy all
</Directory>

接着在最后加入以下内容:

<VirtualHost *:80>
DocumentRoot F:/App/www/test/
ServerName local.test.com
# Other directives here
</VirtualHost>

其中F:/App/www/为Apache根目录,F:/App/www/test/为根目录下方test所有源码的目录;ServerName为你想要访问的域名,如果你想用www.xxx.com的方式访问,只需要同时编辑c:\windows\system32\drivers\etc\hosts里面的域名和这个域名就可以实现,但是会拦截这个域名的DNS解析。

最终配置如下:

c:\windows\system32\drivers\etc\hosts

127.0.0.1       localhost
192.168.1.101    local.test.com

 

/apache/conf/httpd.conf

<Directory />
    Options FollowSymLinks ExecCGI Indexes
    AllowOverride None
   Order allow,deny
    Allow from all
    Satisfy all
</Directory>

 

<VirtualHost *:80>
DocumentRoot F:/App/www/test/
ServerName local.test.com
# Other directives here
</VirtualHost>
原创粉丝点击