waiwai64 发表于 2015-8-3 00:10:36

[Windows]Apache虚拟主机设置

  在网上查了很多资料配置Apache的虚拟主机,但都是讲得一知半解,没有系统、完整的配置过程。
  经自己研究后,得到了完整版的配置方法。
  以下设置方法是基于WindowsXP的设置方法
  调试:在本机进行调试
  
  1、安装Apache
  省略
  2、配置Apache
  打开Apache的配置文件httpd.conf,位置:在Apache安装路径下的conf下
  找到以下内容,指定Apache的端口号,这里设置为:80



Listen 80

  继续:
  设置Web站点的路径,这里指定为以下:





  以上的设置只是Apache的常规设置,如果不设置虚拟主机的话,使用以上设置就可以完成Web服务的配置了。
  3、配置Apache虚拟主机
  继续以上步骤:
  将Include前的#去掉



# Virtual hosts
Include conf/extra/httpd-vhosts.conf
  保存修改后的httpd.conf
  4、配置httpd-vhosts.conf
  打开httpd-vhosts.conf文件,将该文件内容指定为:



#
# 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
#
# 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 anyblock.
#

    ServerAdmin webmaster@dummy-host.test
    DocumentRoot "D:/WorkSpace/ApacheWebs2"
    ServerName www.test1.com
    ServerAlias www.dummy-host.test
    ErrorLog "logs/dummy-host.test-error.log"
    CustomLog "logs/dummy-host.test-access.log" common


      Options Indexes MultiViews
      AllowOverride None
      Order allow,deny
      Allow from all


    ServerAdmin webmaster@dummy-host2.test
    DocumentRoot "D:/WorkSpace/ApacheWebs3"
    ServerName www.test2.com
    ErrorLog "logs/dummy-host2.test-error.log"
    CustomLog "logs/dummy-host2.test-access.log" common


      Options Indexes MultiViews
      AllowOverride None
      Order allow,deny
      Allow from all

  上面的配置文件中设置了两个虚拟主机,对应的服务器名及Web站点目录名分别为:
  ServerName:www.test1.com     ->   D:/WorkSpace/ApacheWebs2
  ServerName:www.test2.com     ->   D:/WorkSpace/ApacheWebs3
  
的标签使用来设置目录的访问权限的,如果不设置的话,会出现403的错误。
  如果不希望站点显示目录的话,可以将中Options的Indexes前加上[-],即:




      Options -Indexes MultiViews
      AllowOverride None
      Order allow,deny
      Allow from all

  
以上配置结束后,保存文件.
  并重新启动Apache服务即可.
  如果是本地调试的话,还需要修改一下Hosts文件,否则的话。。。,你自己试试就知道了
  打开c:\windows\system2\drivers\etc\Hosts文件
  加入以下代码:



127.0.0.1      www.test1.com
127.0.0.1      www.test2.com
  
打开IE,分别输入:
  http://www.test1.com
  http://www.test2.com
  就可以看到效果了。
  
  更多更详细的配置方法,请参考:
  http://lamp.linux.gov.cn/Apache/ApacheMenu/vhosts/examples.html
  
  原创,转载请注明来源。
页: [1]
查看完整版本: [Windows]Apache虚拟主机设置