最近想在Apache2.4.3上挂载多站点,可是怎么都不成功。我使用的是同一个IP、不同名字的方法(也叫Name-Based方法,另外一种方法叫IP-Based)。也就说本机IP都是127.0.0.1,但是访问localhost、www.a.com、www.b.com时,所指向的网站目录不同。
最终要实现的指向是:
127.0.0.1 ==> D:/xampp/htdocs/xampp
localhost ==> D:/xampp/htdocs/xampp
www.a.com ==> D:/xampp/htdocs/a
www.b.com ==> D:/xampp/htdocs/b
(有人可能会问,能不能指向“xampp/htdocs”以外的目录,比如G:/website/a。这个应该得修改httpd.conf中的DocumentRoot。可修改后,浏览127.0.0.1和localhost就不会指向xampp/htdocs/xampp了。如果不修改原有的DocumentRoot,只仅仅添加Dirctory,我试了网上各种添加方法,都没成功。可能是版本原因。所以在文章标题处特地注明了我所使用的版本。所以,为了效果好、方便,还是把站点都放在xampp/htdocs下吧。)
1.修改C:\Windows\System32\drivers\etc下的hosts文件:
# Copyright (c) 1993-2009 Microsoft Corp. # # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. # # This file contains the mappings of IP addresses to host names. Each # entry should be kept on an individual line. The IP address should # be placed in the first column followed by the corresponding host name. # The IP address and the host name should be separated by at least one # space. # # Additionally, comments (such as these) may be inserted on individual # lines or following the machine name denoted by a '#' symbol. # # For example: # # 102.54.94.97 rhino.acme.com # source server # 38.25.63.10 x.acme.com # x client host # localhost name resolution is handled within DNS itself. 127.0.0.1 localhost 127.0.0.1 www.a.com 127.0.0.1 www.b.com # ::1 localhost
2.在D:\xampp\htdocs中新建目录/a/、/a/logs/、/b/、/b/logs/
# # Virtual Hosts # # If you want to maintain multiple domains/hostnames on your # machine you can setup VirtualHost containers for them. Most configurations # use only name-based virtual hosts so the server doesn't need to worry about # IP addresses. This is indicated by the asterisks in the directives below. # # Please see the documentation at # <URL:http://httpd.apache.org/docs/2.2/vhosts/> # for further details before you try to setup virtual hosts. # # You may use the command line option '-S' to verify your virtual host # configuration. # # Use name-based virtual hosting. # ##NameVirtualHost *:80 # # VirtualHost example: # Almost any Apache directive may go into a VirtualHost container. # The first VirtualHost section is used for all requests that do not # match a ServerName or ServerAlias in any <VirtualHost> block. # ##<VirtualHost *:80> ##ServerAdmin [email protected] ##DocumentRoot "D:/xampp/htdocs/dummy-host.localhost" ##ServerName dummy-host.localhost ##ServerAlias www.dummy-host.localhost ##ErrorLog "logs/dummy-host.localhost-error.log" ##CustomLog "logs/dummy-host.localhost-access.log" combined ##</VirtualHost> ##<VirtualHost *:80> ##ServerAdmin [email protected] ##DocumentRoot "D:/xampp/htdocs/dummy-host2.localhost" ##ServerName dummy-host2.localhost ##ServerAlias www.dummy-host2.localhost ##ErrorLog "logs/dummy-host2.localhost-error.log" ##CustomLog "logs/dummy-host2.localhost-access.log" combined ##</VirtualHost> # 挂载多站点 #配置回原localhost,不然原localhost的访问会出错 <virtualhost *:80> ServerName localhost DocumentRoot "D:/xampp/htdocs" </virtualhost> #添加站点1 <virtualhost *:80> ServerName www.a.com DocumentRoot "D:/xampp/htdocs/a/" ErrorLog "D:/xampp/htdocs/a/logs/www.a.com.log" CustomLog "D:/xampp/htdocs/a/logs/www.a.com.log" combined </virtualhost> #添加站点2 <virtualhost *:80> ServerName www.b.com DocumentRoot "D:/xampp/htdocs/b/" ErrorLog "D:/xampp/htdocs/b/logs/www.b.com.log" CustomLog "D:/xampp/htdocs/b/logs/www.b.com.log" combined </virtualhost>Apache2.4.x可不用NameVirtualHost *:80,官网也不推荐使用。使用了在error.log中反而会出现警告“AH00548: NameVirtualHost has no effect and will be removed in the next release D:/xampp/apache/conf/extra/httpd-vhosts.conf:46”。
4.保存文件,重启Apache即可。