1.httpd.conf中找到Include conf/extra/httpd-vhosts.conf去掉注释
2. 打开目录 {Apache2 安装目录}\conf\extra\, 找到httpd-vhosts.conf文件. 模仿添加
- NameVirtualHost *:80
- <VirtualHost *:80>
- ServerAdmin webmaster@dummy-host2.example.com
- DocumentRoot "E:/mii_adb/wwwroot"
- ServerAlias www.test.com
- ErrorLog "logs/dummy-test.com-error.log"
- CustomLog "logs/dummy-test.com-access.log" common
- <Directory "E:/mii_adb/wwwroot">
- Options Indexes FollowSymLinks Includes ExecCGI
- AllowOverride all
- Order allow,deny
- Allow from all
- </Directory>
- </VirtualHost>
httpd.conf中<Directory />AllowOverride all,Allow from All不需要配置VirtualHost中的Directory,这样性能不好
Listen 要监听的端口,多个端口,要写多个Listen;否则Apache启动的时候,不会启动相应的套接字。
- Listen 82
- NameVirtualHost *:82
- <VirtualHost *:82>
- ServerAdmin webmaster@dummy-host.example.com
- DocumentRoot "E:/amcare_query/dev_src"
- ServerName localhost
- ErrorLog logs/amcare-error_log
- CustomLog logs/amcare-access_log common
- <Directory "E:/amcare_query/dev_src">
- Options Indexes FollowSymLinks Includes ExecCGI
- AllowOverride all
- Order allow,deny
- Allow from all
- </Directory>
- </VirtualHost>
E:/amcare_query/dev_src访问:http://localhost:82
4.配置windows 的host文件
- 127.0.0.1 localhost
- 127.0.0.1 www.test.com
- 127.0.0.1:82 www.amcare.com
5. 重启Apache服务.完成以上设置后,可以通过以下方式访问:www.amcare.com
注:以上代码注意一点,就是配置DocumentRoot的时候斜杠可能是这种样式:“\ ” ;请修改为这种样式:“ ∕ ” ,切记!!!
ServerAlias:服务器别名,在Apache中可以用于设置虚拟主机接收到个域名,也可以用于接收泛解析的域名。具体的设置方法如下:
一、用于设置虚拟主机接收多个域名
一个虚拟主机常常会接收多个域名解析,比如:一个虚拟主机要同时介绍sanywork.cn, 51684.com两个域名,或者是两个二级域名,如:www.sanywork.cn, www1.sanywork.cn,对于这种情况,可以在用ServerAlias轻松做到,打开apache的设置文件:httpd.conf,在该主 机的设置代码中:
- <VirtualHost 192.168.1.12>
- ServerName "sanywork.cn" #默认域名;
- ServerAlias 51684.com www1.sanywork.cn #不同域名用空格隔开;
- DocumentRoot "D:\wwwroot\sanywork.cn"
- <Directory "D:\wwwroot\sanywork.cn">
- Options Indexes FollowSymLinks Includes ExecCGI
- AllowOverride all
- Allow from all
- </Directory>
- </VirtualHost>
保存httpd.conf文件,重启apche即可生效。
虚拟主机解析的优先级:前面的解析过了,后面就不会执行了
- <VirtualHost *:80>
- ServerAdmin 75320364@qq.com
- DocumentRoot "/home/test_admin/trunk/htdocs"
- ServerName system.mall.test.com
- </VirtualHost>
- #当访问system.mall.test.com的时候下面不执行
- <VirtualHost *:80>
- ServerAdmin 75320364@qq.com
- DocumentRoot "/home/test/trunk/htdocs"
- ServerAlias *.mall.test.com
- </VirtualHost>
二、 用于接收泛域名解析
一个虚拟主机是可以接收泛域名解析的,也是通过SeverAlias选项设置,通过设置该选项不仅可以接收二级泛域名解析,也可以接收三级、四级泛域名解析,具体设置如下:
- <VirtualHost 192.168.1.12>
- ServerName "sanywork.cn" #默认域名;
- ServerAlias *.sanywork.cn #用*表示泛域名,如果要接收三级泛域名解析,可以写成这样:*.my.sanywork.cn;
- DocumentRoot "D:\wwwroot\sanywork.cn"
- <Directory "D:\wwwroot\sanywork.cn">
- Options Indexes FollowSymLinks Includes ExecCGI
- AllowOverride all
- Allow from all
- </Directory>
- </VirtualHost>