免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3277 | 回复: 0
打印 上一主题 下一主题

Apache配置本地测试多网站域名与虚拟主机 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-02-18 10:04 |只看该作者 |倒序浏览
Apache配置本地测试多网站域名与虚拟主机



1、修改域名访问方式:

运行:C:\WINDOWS\system32\drivers\etc

打开:hosts文件

添加域名指向。

2、修改httpd.conf.

配置生效前提,必须修改跟目录为:
  1. <Directory />
  2. Options FollowSymLinks
  3. AllowOverride None
  4. ### Order deny,allow
  5. ### Deny from all
  6. Order allow,deny
  7. Allow from all
  8. Satisfy all
  9. </Directory>
复制代码
否则会出现无权访问问题。

3、虚拟主机的配置
(1)基于IP地址的虚拟主机配置
  1. Listen 80
  2. <VirtualHost 172.20.30.40>
  3. DocumentRoot /www/example1
  4. ServerName www.example1.com
  5. </VirtualHost>
  6. <VirtualHost 172.20.30.50>
  7. DocumentRoot /www/example2
  8. ServerName www.example2.org
  9. </VirtualHost>
复制代码
(2) 基于IP和多端口的虚拟主机配置
  1. Listen 172.20.30.40:80
  2. Listen 172.20.30.40:8080
  3. Listen 172.20.30.50:80
  4. Listen 172.20.30.50:8080

  5. <VirtualHost 172.20.30.40:80>
  6. DocumentRoot /www/example1-80
  7. ServerName www.example1.com
  8. </VirtualHost>

  9. <VirtualHost 172.20.30.40:8080>
  10. DocumentRoot /www/example1-8080
  11. ServerName www.example1.com
  12. </VirtualHost>

  13. <VirtualHost 172.20.30.50:80>
  14. DocumentRoot /www/example2-80
  15. ServerName www.example1.org
  16. </VirtualHost>

  17. <VirtualHost 172.20.30.50:8080>
  18. DocumentRoot /www/example2-8080
  19. ServerName www.example2.org
  20. </VirtualHost>
复制代码
(3)单个IP地址的服务器上基于域名的虚拟主机配置:
  1. # Ensure that Apache listens on port 80
  2. Listen 80

  3. # Listen for virtual host requests on all IP addresses
  4. NameVirtualHost *:80

  5. <VirtualHost *:80>
  6. DocumentRoot /www/example1
  7. ServerName www.example1.com
  8. ServerAlias example1.com. *.example1.com
  9. # Other directives here
  10. </VirtualHost>

  11. <VirtualHost *:80>
  12. DocumentRoot /www/example2
  13. ServerName www.example2.org
  14. # Other directives here
  15. </VirtualHost>
复制代码
(4)在多个IP地址的服务器上配置基于域名的虚拟主机:
  1. Listen 80

  2. # This is the "main" server running on 172.20.30.40
  3. ServerName server.domain.com
  4. DocumentRoot /www/mainserver

  5. # This is the other address
  6. NameVirtualHost 172.20.30.50

  7. <VirtualHost 172.20.30.50>
  8. DocumentRoot /www/example1
  9. ServerName www.example1.com
  10. # Other directives here …
  11. </VirtualHost>
  12. IXDBA.NET社区论坛

  13. <VirtualHost 172.20.30.50>
  14. DocumentRoot /www/example2
  15. ServerName www.example2.org
  16. # Other directives here …
  17. </VirtualHost>
复制代码
(5)在不同的端口上运行不同的站点(基于多端口的服务器上配置基于域名的虚拟主机):
  1. Listen 80
  2. Listen 8080

  3. NameVirtualHost 172.20.30.40:80
  4. NameVirtualHost 172.20.30.40:8080

  5. <VirtualHost 172.20.30.40:80>
  6. ServerName www.example1.com
  7. DocumentRoot /www/domain-80
  8. </VirtualHost>

  9. <VirtualHost 172.20.30.40:8080>
  10. ServerName www.example1.com
  11. DocumentRoot /www/domain-8080
  12. </VirtualHost>

  13. <VirtualHost 172.20.30.40:80>
  14. ServerName www.example2.org
  15. DocumentRoot /www/otherdomain-80
  16. </VirtualHost>

  17. <VirtualHost 172.20.30.40:8080>
  18. ServerName www.example2.org
  19. DocumentRoot /www/otherdomain-8080
  20. </VirtualHost>
复制代码
(6)基于域名和基于IP的混合虚拟主机的配置:
  1. Listen 80

  2. NameVirtualHost 172.20.30.40

  3. <VirtualHost 172.20.30.40>
  4. DocumentRoot /www/example1
  5. ServerName www.example1.com
  6. </VirtualHost>

  7. <VirtualHost 172.20.30.40>
  8. DocumentRoot /www/example2
  9. ServerName www.example2.org
  10. </VirtualHost>

  11. <VirtualHost 172.20.30.40>
  12. DocumentRoot /www/example3
  13. ServerName www.example3.net
  14. </VirtualHost>
复制代码
==========================================================================

简单的说,打开httpd.conf 在最后加入如下内容:
  1. <VirtualHost 127.0.0.2:80>
  2.     DocumentRoot d:/AppServ/www2
  3.     ServerName 127.0.0.2:80
  4. </VirtualHost>


  5. <Directory "d:/AppServ/www2">
  6.     Options Indexes FollowSymLinks Multiviews
  7.     AllowOverride All
  8.     Order Allow,Deny
  9.     Allow from all
  10. </Directory>
复制代码
"d:/AppServ/www2" 为你的站点存放目录:重启apache2以后,你的虚拟主机就配置好了,以后就可以通过127.0.0.2,和127.0.0.3进入不同的站点了。

下面为详细说明分析:

在我们安装APACHE的时候一般默认的apache的配置是只有一个网站,这样切换起来很不方便。其实这个问题很好解决,就是把本机的apache配置成为虚拟服务器。但是,网上大多数教程的是教用 apache如何配置基于域名的虚拟主机的,而在本机调试网站的时候,一般都是用本地ip(127.0.0.1 或 localhost)直接访问,没有用到域名。所以得把apache配置成为基于ip地址的虚拟主机。

首先,我们都知道,所有以127打头的ip地址都应该指向本机,并不只有127.0.0.1,这点大家可以试试。
这样一来,也就是说本机有足够多的ip地址供你来开设虚拟主机了。

废话少说,进入正式的配置工作,下面是apache的httpd.conf里相关配置部分( httpd.conf 位于 Apache2.2\conf ):
1、Listen部分,必须直接指定端口,不指定ip地址,配置应写为:
Listen 80
2、不用像基于域名的虚拟主机那样写“NameVirtualHost”。

3、虚拟主机配置段:在httpd.conf 最后加上
  1. <VirtualHost 127.0.0.2:80>
  2.     DocumentRoot d:/AppServ/www2
  3.     ServerName 127.0.0.2:80
  4. </VirtualHost>

  5. <VirtualHost 127.0.0.3:80>
  6.     DocumentRoot d:/AppServ/www3
  7.     ServerName 127.0.0.3:80
  8. </VirtualHost>...
复制代码
4、然后相应的配置好各个目录属性,下面是一个目录属性的典型配置:
  1. <Directory "d:/AppServ/www2">
  2.     Options Indexes FollowSymLinks Multiviews
  3.     AllowOverride All
  4.     Order Allow,Deny
  5.     Allow from all
  6. </Directory>

  7. <Directory "d:/AppServ/www3">
  8.     Options Indexes FollowSymLinks Multiviews
  9.     AllowOverride All
  10.     Order Allow,Deny
  11.     Allow from all
  12. </Directory>
复制代码
重启apache2以后,你的虚拟主机就配置好了,以后就可以通过127.0.0.1和127.0.0.2,127.0.0.3进入不同的站点了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP