Apache的虚拟主机是一种允许在同一台机器上配置多个不同站点的web服务器环境的,就是iis一样可以创建多站点了,但是apache需要在编辑状态操作,不能像windows
iis直接点击几下就好了,下面我来给各位介绍配置方法。

1、修改域名访问方式:

最平常的大概有3种方法。

运行:C:WINDOWSsystem32driversetc

第一种:单IP不同端口

打开:hosts文件

第二种:多IP同端口(独立IP的虚拟空间)

添加域名指向。

第三种:域名绑定根目录的方式(共享IP的虚拟空间)

2、修改httpd.conf.

Apache的核心配置文件名是”httpd.conf”,其所存放的路径在Apache目录下的conf文件夹下。修改它只需要使用记事本(建议使用其他编辑器,带行数的那种,方便修改),生效的话只需要保存httpd.conf,重启apache即可。

配置生效前提,必须修改跟目录为:

下面多站点支持的话,修改httpd.conf的第187~264行(不同的httpd.conf可能有差异),也就是在ServerAdmin和ServerName那里,大部分是注释。下面是主要修改的地方。

<Directory />
Options FollowSymLinks
AllowOverride None
### Order deny,allow
### Deny from all
Order allow,deny
Allow from all
Satisfy all
</Directory>

注意:如果是服务器请备份httpd.conf后再修改文件。

否则会出现无权访问问题。

 代码如下

3、虚拟主机的配置
(1)基于IP地址的虚拟主机配置
Listen 80
<VirtualHost
172.20.30.40>
DocumentRoot /www/example1
ServerName www.example1.com
</VirtualHost>
<VirtualHost
172.20.30.50>
DocumentRoot /www/example2
ServerName www.example2.org
</VirtualHost>

复制代码

(2) 基于IP和多端口的虚拟主机配置
Listen 172.20.30.40:80
Listen 172.20.30.40:8080
Listen 172.20.30.50:80
Listen 172.20.30.50:8080

# ‘Main’ server configuration
#
# The directives in this section set up the values used by the ‘main’
# server, which responds to any
requests that aren’t handled
by a
# <VirtualHost> definition.  These values also provide defaults
for
# any <VirtualHost> containers you may define later in the
file.
#
# All of these directives may appear inside <VirtualHost>
containers,
# in which case these default settings will be overridden for the
# virtual host being defined.
#

<VirtualHost
172.20.30.40:80>
DocumentRoot /www/example1-80
ServerName www.example1.com
</VirtualHost>

#
# ServerAdmin: Your address, where problems with the server should be
# e-mailed.  This address appears on some server-generated pages,
such
# as error documents.  e.g. admin@your-domain.com
#
ServerAdmin admin@example.com

<VirtualHost
172.20.30.40:8080>
DocumentRoot /www/example1-8080
ServerName www.example1.com
</VirtualHost>

#
# ServerName gives the name and port that the server uses to identify
itself.
# This can often be determined automatically, but we recommend you
specify
# it explicitly to prevent problems during startup.
#
# If your host doesn’t have a registered DNS name, enter its IP address
here.
#
ServerName www.example.com:80

<VirtualHost
172.20.30.50:80>
DocumentRoot /www/example2-80
ServerName www.example1.org
</VirtualHost>

#
# Deny access to the entirety of your server’s filesystem. You must
# explicitly permit access to web content directories in other
# <Directory> blocks below.
#
<Directory />
    AllowOverride All
    Require all denied
</Directory>

<VirtualHost
172.20.30.50:8080>
DocumentRoot /www/example2-8080
ServerName www.example2.org
</VirtualHost>

#
# Note that from this point forward you must specifically allow
# particular features to be enabled – so if something’s not working
as
# you might expect, make sure that you have specifically enabled it
# below.
#

(3)单个IP地址的服务器上基于域名的虚拟主机配置:
# Ensure that Apache listens on port 80
Listen 80

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory,
but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot “g:/www”
<Directory “g:/www”>
    #
    # Possible values for the Options directive are “None”, “All”,
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI
MultiViews
    #
    # Note that “MultiViews” must be named *explicitly* — “Options
All”
    # doesn’t give it to you.
    #
    # The Options directive is both complicated and important.  Please
see
    #
    # for more information.
    #
    Options Indexes FollowSymLinks

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

    #
    # AllowOverride controls what directives may be placed in .htaccess
files.
    # It can be “All”, “None”, or any combination of the
keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride All

<VirtualHost
*:80>
DocumentRoot /www/example1
ServerName www.example1.com
ServerAlias example1.com. *.example1.com
# Other directives here
</VirtualHost>

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

<VirtualHost
*:80>
DocumentRoot /www/example2
ServerName www.example2.org
# Other directives here
</VirtualHost>

第一种一般是测试环境,毕竟加了端口,如何绑定域名,访问的时候域名后面也需加端口。

(4)在多个IP地址的服务器上配置基于域名的虚拟主机:
Listen 80

例子分别通过80和8080访问不同的根目录。

# This is the “main” server running on 172.20.30.40
ServerName server.domain.com
DocumentRoot /www/mainserver

大概在50几行有个Listen 80,在下面添加8080端口。

# This is the other address
NameVirtualHost
172.20.30.50

 代码如下

<VirtualHost
172.20.30.50>
DocumentRoot /www/example1
ServerName www.example1.com
# Other directives here …
</VirtualHost>
IXDBA.NET社区论坛

复制代码

<VirtualHost
172.20.30.50>
DocumentRoot /www/example2
ServerName www.example2.org
# Other directives here …
</VirtualHost>

Listen 80
Listen 8080<VirtualHost *:80>
    ServerAdmin admin@myxzy.com
    ServerName localhost:80
    DocumentRoot “g:/www1”
     <Directory “g:/www1”>
     Options  Indexes FollowSymLinks
     AllowOverride All
     Require all granted
   </Directory>  
</VirtualHost>
<VirtualHost *:8080>
    ServerAdmin admin@myxzy.com
    ServerName localhost:8080 
    DocumentRoot “g:/www2”
   <Directory “g:/www2”>
     Options Indexes FollowSymLinks
     AllowOverride All
     Require all granted
   </Directory>      
</VirtualHost>

(5)在不同的端口上运行不同的站点(基于多端口的服务器上配置基于域名的虚拟主机):
Listen 80
Listen 8080

第二种多IP同端口。

NameVirtualHost
172.20.30.40:80
NameVirtualHost
172.20.30.40:8080

IP地址1:192.168.2.2

<VirtualHost
172.20.30.40:80>
ServerName www.example1.com
DocumentRoot /www/domain-80
</VirtualHost>

IP地址2:192.168.1.68

<VirtualHost
172.20.30.40:8080>
ServerName www.example1.com
DocumentRoot /www/domain-8080
</VirtualHost>

端口同是80端口。

<VirtualHost
172.20.30.40:80>
ServerName www.example2.org
DocumentRoot /www/otherdomain-80
</VirtualHost>

 代码如下

<VirtualHost
172.20.30.40:8080>
ServerName www.example2.org
DocumentRoot /www/otherdomain-8080
</VirtualHost>

复制代码

(6)基于域名和基于IP的混合虚拟主机的配置:
Listen 80

<VirtualHost 192.168.1.68:80>
    ServerAdmin admin@myxzy.com
    ServerName localhost:80
    DocumentRoot “g:/www1”
     <Directory “g:/www1”>
     Options FollowSymLinks
     AllowOverride All
     Require all granted
   </Directory>  
</VirtualHost>
<VirtualHost 192.168.2.2:80>
    ServerAdmin admin@myxzy.com
    ServerName localhost:80
    DocumentRoot “g:/www2”
   <Directory “g:/www2”>
     Options FollowSymLinks
     AllowOverride All
     Require all granted
   </Directory>      
</VirtualHost>

NameVirtualHost
172.20.30.40

第三种同IP不同域名和根目录(域名的话修改本地host演示)。

<VirtualHost
172.20.30.40>
DocumentRoot /www/example1
ServerName www.example1.com
</VirtualHost>

 代码如下

<VirtualHost
172.20.30.40>
DocumentRoot /www/example2
ServerName www.example2.org
</VirtualHost>

复制代码

<VirtualHost
172.20.30.40>
DocumentRoot /www/example3
ServerName www.example3.net
</VirtualHost>

<VirtualHost 192.168.2.2:80>
    ServerAdmin admin@myxzy.com
    ServerName www.111cn.net
    DocumentRoot “g:/www1”
     <Directory “g:/www1”>
     Options FollowSymLinks
     AllowOverride All
     Require all granted
   </Directory>  
</VirtualHost>
<VirtualHost 192.168.2.2:80>
    ServerAdmin admin@myxzy.com
    ServerName www.111cn.net
    DocumentRoot “g:/www2”
   <Directory “g:/www2”>
     Options FollowSymLinks
     AllowOverride All
     Require all granted
   </Directory>      
</VirtualHost>

==========================================================================

简单的说,打开httpd.conf 在最后加入如下内容:

<VirtualHost
127.0.0.2:80>
    DocumentRoot d:/AppServ/www2
    ServerName 127.0.0.2:80
</VirtualHost>

<Directory “d:/AppServ/www2”>
    Options Indexes FollowSymLinks Multiviews
    AllowOverride All
    Order Allow,Deny
    Allow from all
</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.2conf ):
1、Listen部分,必须直接指定端口,不指定ip地址,配置应写为:
Listen 80
2、不用像基于域名的虚拟主机那样写“NameVirtualHost”。

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

<VirtualHost
127.0.0.3:80>
    DocumentRoot d:/AppServ/www3
    ServerName 127.0.0.3:80
</VirtualHost>…

4、然后相应的配置好各个目录属性,下面是一个目录属性的典型配置:
<Directory “d:/AppServ/www2”>
    Options Indexes FollowSymLinks Multiviews
    AllowOverride All
    Order Allow,Deny
    Allow from all
</Directory>

<Directory “d:/AppServ/www3”>
    Options Indexes FollowSymLinks Multiviews
    AllowOverride All
    Order Allow,Deny
    Allow from all
</Directory>

重启apache2以后,你的虚拟主机就配置好了,以后就可以通过127.0.0.1和127.0.0.2,127.0.0.3进入不同的站点了。