六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 571|回复: 0

Apache配置虚拟目录和多主机头

[复制链接]
 楼主| 发表于 2013-11-15 13:10:00 | 显示全部楼层 |阅读模式
呃,相当古老的话题了,不过网上的资料实在是太坑爹,无奈只能自己动手做个备忘了。。。
这里不提虚拟目录和主机头的区别了,不懂得童鞋去面壁思过吧
多个虚拟目录
  首先把Apache安装到D:\Program Files\Apache2.2目录下,端口号设置为8080,安装完成后默认的网站根目录为D:\Program Files\Apache2.2\htdocs,通常我们可以在htdocs下面建立个文件夹MySite,然后在浏览器输入:http://localhost:8080/MySite 这样就可以看到我们自己的站点了。然而有时我们想把站点放到其它目录下面,这时就需要配置虚拟目录了
比如我们在D盘建立如下文件夹D:\Code\WebSite,然后通过http://localhost:8080/DemoSite来访问这个站点
打开httpd.conf文件,搜索<IfModule alias_module> 节点,然后在节点内输入以下内容:
  1. #下面是虚拟目录声明格式
  2. #Alias用来定义虚拟目录及虚拟目录路径,其中虚拟目录名称用于URL访问的路径别名,可以和虚拟目录名称不同
  3. #<Directory/>节点用于定义目录的访问权限等
  4. #
  5. #Alias 虚拟目录名称 虚拟目录路径
  6. #<Directory 虚拟目录路径>
  7. #   Options Indexes FollowSymLinks
  8. #   AllowOverride All
  9. #   Order allow,deny
  10. #   Allow from all
  11. #</Directory>

  12. #下面是具体的示例,/DemoSite是目录别名 "D:/Code/WebSite"是虚拟目录的实际路径
  13. Alias /DemoSite "D:/Code/WebSite"

  14. <Directory "D:/Code/WebSite">
  15.     Options Indexes FollowSymLinks
  16.     AllowOverride All
  17.     Order allow,deny
  18.     Allow from all
  19. </Directory>
复制代码
重启Apache服务后,在浏览器输入http://localhost:8080/DemoSite就可以正常访问了
这里需要注意下目录尽量使用"/",而不是使用"\",原因就是"\"代表转义符有些情况下会导致莫名奇妙的错误,下面附上完整的<IfModule alias_module>节点以供参考
  1. <IfModule alias_module>
  2.     #
  3.     # Redirect: Allows you to tell clients about documents that used to
  4.     # exist in your server's namespace, but do not anymore. The client
  5.     # will make a new request for the document at its new location.
  6.     # Example:
  7.     # Redirect permanent /foo http://localhost/bar

  8.     #
  9.     # Alias: Maps web paths into filesystem paths and is used to
  10.     # access content that does not live under the DocumentRoot.
  11.     # Example:
  12.     # Alias /webpath /full/filesystem/path
  13.     #
  14.     # If you include a trailing / on /webpath then the server will
  15.     # require it to be present in the URL.  You will also likely
  16.     # need to provide a <Directory> section to allow access to
  17.     # the filesystem path.

  18.     #
  19.     # ScriptAlias: This controls which directories contain server scripts.
  20.     # ScriptAliases are essentially the same as Aliases, except that
  21.     # documents in the target directory are treated as applications and
  22.     # run by the server when requested rather than as documents sent to the
  23.     # client.  The same rules about trailing "/" apply to ScriptAlias
  24.     # directives as to Alias.
  25.     #
  26.     ScriptAlias /cgi-bin/ "D:/Program Files/Apache2.2/cgi-bin/"
  27.    
  28.     Alias /DemoSite "D:/Code/WebSite"

  29.     <Directory "D:/Code/WebSite">
  30.         Options Indexes FollowSymLinks
  31.         AllowOverride All
  32.         Order allow,deny
  33.         Allow from all
  34.     </Directory>
  35. </IfModule>
复制代码
多主机头绑定
(就是在一个端口上绑定多个域名,然后每个域名可以指向不同的目录进行访问,主机头是IIS里面的说法),打开httpd.conf文件,在文件最后添加如下内容
  1. #多主机头配置无需放在特定的节点下面,一般直接在配置文件底部添加即可
  2. #NameVirtualHost addr[:port] 为一个基于域名的虚拟主机指定一个IP地址(和端口)
  3. #声明主机头必须加这条指令,否者主机头配置不会生效
  4. #VirtualHost节点下面ServerName就是要绑定的域名,DocumentRoot表示此域名指向的目录
  5. #本机测试的话请在hosts中进行域名绑定如 127.0.0.1  www.mysite1.com

  6. NameVirtualHost *:8080
  7. <VirtualHost *:8080>
  8.     ServerName www.mysite1.com
  9.     DocumentRoot "D:\Program Files\Apache2.2\htdocs"
  10. </VirtualHost>

  11. <VirtualHost *:8080>
  12.     ServerName www.mysite2.com
  13.     DocumentRoot "D:\Code\MySite"
  14. </VirtualHost>
复制代码
配置好后,重启apache服务,浏览器输入www.mysite1.com:8080,就会自动定向到D:\Program Files\Apache2.2\htdocs站点了
输入www.mysite2.com:8080就会自动定向到D:\Code\MySite站点,如此就可以实现在一个服务器上同时运行多个站点
注:此文章属懒惰的肥兔原创,版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接:http://www.cnblogs.com/lzrabbit/archive/2013/03/05/2944804.html




该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表