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

了解一下根目录和虚拟目录的关系:
安装过apache之后打开httpd.conf文件会发现DocumentRoot默认设置的应该是apache安装目录中的htdocs文件夹。然后你的网页就可以放大这个htdocs文件夹中测试,例你在里面放了一个1.PHP,可以输入http://127.0.0.1/1.php进行测试。这个DocumentRoot后面的路径就是apache的根目录。有时候我们不想将自己的网站放到这里,例如我想要放到F:/MyPHP中来运行,那么就可以修改DocumentRoot为F:/MyPHP,然后将那个1.php放入F:/MyPHP文件夹,同样使用可以正常访问。

虚拟目录是什么?顾名思义只是一个虚拟的目录,和真实目录是有差别的。先来看看使用真实目录访问,我们在刚才的F:/MyPHP中建立一个文件夹Test,然后在里面放入2.php,这个时候就可以通过http://127.0.0.1/Test/2.php访问。但是有些时候你可能建立的文件夹和想要输入的访问地址有一个映射关系,而不是直接输入Test文件夹名,这样做的原因有很多其中就有一个是安全问题,因为那样别人就会知道你的根目录的文件夹。具体点,你在Test文件夹中放置了2.php,但是想要通过http://127.0.0.1/cmj/2.php访问2.php而不是通过http://127.0.0.1/Test/2.php来访问怎么办呢?这个时候我们就需要虚拟目录了,很明显没有cmj这个文件夹,但是又能够像访问一个正式的目录一样来访问,就需要一种映射关系。

多个虚拟目录

  首先把Apache安装到D:Program FilesApache2.2目录下,端口号设置为8080,安装完成后默认的网站根目录为D:Program FilesApache2.2htdocs,通常我们可以在htdocs下面建立个文件夹MySite,然后在浏览器输入:http://localhost:8080/MySite 这样就可以看到我们自己的站点了。然而有时我们想把站点放到其它目录下面,这时就需要配置虚拟目录了
比如我们在D盘建立如下文件夹D:CodeWebSite,然后通过http://localhost:8080/DemoSite来访问这个站点

打开httpd.conf文件,搜索<IfModule alias_module> 节点,然后在节点内输入以下内容:

  #下面是虚拟目录声明格式  #Alias用来定义虚拟目录及虚拟目录路径,其中虚拟目录名称用于URL访问的路径别名,可以和虚拟目录名称不同  #<Directory/>节点用于定义目录的访问权限等  #  #Alias 虚拟目录名称 虚拟目录路径  #<Directory 虚拟目录路径>  #  Options Indexes FollowSymLinks  #  AllowOverride All  #  Order allow,deny  #  Allow from all  #</Directory>    #下面是具体的示例,/DemoSite是目录别名 "D:/Code/WebSite"是虚拟目录的实际路径  Alias /DemoSite "D:/Code/WebSite"    <Directory "D:/Code/WebSite">    Options Indexes FollowSymLinks    AllowOverride All    Order allow,deny    Allow from all  </Directory>

重启Apache服务后,在浏览器输入http://localhost:8080/DemoSite就可以正常访问了
这里需要注意下目录尽量使用"/",而不是使用"",原因就是""代表转义符有些情况下会导致莫名奇妙的错误,下面附上完整的<IfModule alias_module>节点以供参考

  <IfModule alias_module>    #    # Redirect: Allows you to tell clients about documents that used to     # exist in your server's namespace, but do not anymore. The client     # will make a new request for the document at its new location.    # Example:    # Redirect permanent /foo http://localhost/bar      #    # Alias: Maps web paths into filesystem paths and is used to    # access content that does not live under the DocumentRoot.    # Example:    # Alias /webpath /full/filesystem/path    #    # If you include a trailing / on /webpath then the server will    # require it to be present in the URL. You will also likely    # need to provide a <Directory> section to allow access to    # the filesystem path.      #    # ScriptAlias: This controls which directories contain server scripts.     # ScriptAliases are essentially the same as Aliases, except that    # documents in the target directory are treated as applications and    # run by the server when requested rather than as documents sent to the    # client. The same rules about trailing "/" apply to ScriptAlias    # directives as to Alias.    #    ScriptAlias /cgi-bin/ "D:/Program Files/Apache2.2/cgi-bin/"        Alias /DemoSite "D:/Code/WebSite"      <Directory "D:/Code/WebSite">      Options Indexes FollowSymLinks      AllowOverride All      Order allow,deny      Allow from all    </Directory>  </IfModule>

多主机头绑定
(就是在一个端口上绑定多个域名,然后每个域名可以指向不同的目录进行访问,主机头是IIS里面的说法),打开httpd.conf文件,在文件最后添加如下内容

  #多主机头配置无需放在特定的节点下面,一般直接在配置文件底部添加即可  #NameVirtualHost addr[:port] 为一个基于域名的虚拟主机指定一个IP地址(和端口)  #声明主机头必须加这条指令,否者主机头配置不会生效  #VirtualHost节点下面ServerName就是要绑定的域名,DocumentRoot表示此域名指向的目录  #本机测试的话请在hosts中进行域名绑定如 127.0.0.1 www.mysite1.com    NameVirtualHost *:8080  <VirtualHost *:8080>    ServerName www.mysite1.com    DocumentRoot "D:Program FilesApache2.2htdocs"  </VirtualHost>    <VirtualHost *:8080>    ServerName www.mysite2.com    DocumentRoot "D:CodeMySite"  </VirtualHost>

配置好后,重启apache服务,浏览器输入www.mysite1.com:8080,就会自动定向到D:Program FilesApache2.2htdocs站点了

输入就会自动定向到D:CodeMySite站点,如此就可以实现在一个服务器上同时运行多个站点

参与评论