1、使用不同的端口,使用不方便,仅限于组织内部使用

          808188

2、使用不同的IP(端口都是80),需要租赁多个ip地址,费用不划算

3、使用不同的主机名称(domain),比较常用的方式。

  实现方式:在http的请求报文中,request报文中,有一项叫请求的host表示的不是ip地址而是域名,所以第三种方式就是这么实现的



虚拟主机配置:

首先先禁用掉“主”服务器配置

[root@Wiker ~]# grep"Section" /etc/httpd/conf/httpd.conf

### Section 1: GlobalEnvironment

### Section 2: 'Main'server configuration

### Section 3:Virtual Hosts

[root@Wiker ~]# vim/etc/httpd/conf/httpd.conf

#DocumentRoot "/var/www/html" #注释掉此选项即可

首先进行不同端口的虚拟主机的配置

[root@Wiker ~]# vim/etc/httpd/conf/httpd.conf

Listen 80   #设置监听不同的虚拟主机需要使用的端口

Listen 8080

Listen 8088

<Virtualhost *:80 >  #三个不同端口的主机

   ServerName www.80.com

   DocumentRoot /web/vhosts/www.80.com

</Virtualhost>


<Virtualhost*:8080 >

   ServerName www.8080.com

   DocumentRoot /web/vhosts/www.8080.com

</Virtualhost>

<Virtualhost*:8088 >

   ServerName www.8088.com

   DocumentRoot /web/vhosts/www.8088.com

</Virtualhost>

[root@Wiker ~]# mkdir-pv /web/vhosts/www.{80,8080,8088}.com

mkdir: createddirectory `/web'

mkdir: createddirectory `/web/vhosts'

mkdir: createddirectory `/web/vhosts/www.80.com'

mkdir: createddirectory `/web/vhosts/www.8080.com'

mkdir: createddirectory `/web/vhosts/www.8088.com'

[root@Wiker ~]# echo"80" > /web/vhosts/www.80.com/index.html

[root@Wiker ~]# echo"8080" > /web/vhosts/www.8080.com/index.html

[root@Wiker ~]# echo"8088" > /web/vhosts/www.8088.com/index.html

[root@Wiker ~]# httpd-t

Syntax OK

[root@Wiker ~]# setenforce 0 #先关掉selinux,要不会报错

[root@Wiker ~]#service httpd restart

Stopping httpd:                                           [  OK  ]

Starting httpd:                                           [  OK  ]

你就会发现三个端口都处于监听状态

[root@Wiker ~]#netstat -tlnp | grep httpd

tcp        0     0 :::8080                    :::*                        LISTEN      2947/httpd          

tcp        0     0 :::80                      :::*                        LISTEN      2947/httpd          

tcp        0     0 :::8088                    :::*                        LISTEN      2947/httpd

然后打开网页测试

wKiom1MDf6bTs3jPAAFeOm3tFuQ289.jpg

使用三个域名的任何一个加上三个不同的端口都可以访问这三个网站,因为这个虚拟主机使用的是不同的端口号来区分,不是不同的域名。


使用不同的ip地址配置虚拟主机

首先给主机附加一个ip地址,临时性的就可以

[root@Wiker ~]#ifconfig br0:1 192.168.0.2/24

[root@Wiker ~]#ifconfig br0:3 192.168.0.3/24

[root@Wiker ~]# vim/etc/httpd/conf/httpd.conf

<Virtualhost192.168.0.1:80 >

   ServerName www.80.com

   DocumentRoot /web/vhosts/www.80.com

</Virtualhost>


<Virtualhost192.168.0.2:80 >

   ServerName www.8080.com

   DocumentRoot /web/vhosts/www.8080.com

</Virtualhost>

<Virtualhost192.168.0.3:80 >

   ServerName www.8088.com

   DocumentRoot /web/vhosts/www.8088.com

</Virtualhost>

[root@Wiker ~]# httpd-t

Syntax OK

[root@Wiker ~]#service httpd restart

Stopping httpd:                                           [  OK  ]

Starting httpd:                                           [  OK  ]

使用使用浏览器进行测试

wKioL1MDf52DGBxQAAEMYNH5WX0999.jpg


端口和ip地址也可以结合使用

[root@Wiker ~]# vim/etc/httpd/conf/httpd.conf

<Virtualhost192.168.0.1:80 >

   ServerName www.80.com

   DocumentRoot /web/vhosts/www.80.com

</Virtualhost>


<Virtualhost192.168.0.2:80 >

   ServerName www.8080.com

   DocumentRoot /web/vhosts/www.8080.com

</Virtualhost>

<Virtualhost192.168.0.1:8080 >

   ServerName www.8088.com

   DocumentRoot /web/vhosts/www.8088.com

</Virtualhost>

[root@Wiker ~]#service httpd restart

Stopping httpd:                                           [  OK  ]

Starting httpd:                                           [  OK  ]

使用浏览器进行测试

wKiom1MDf9LhQgSYAAEZ6S9FdNQ616.jpg



基于主机名的虚拟主机

[root@Wiker ~]# httpd-v

Server version:Apache/2.2.15 (Unix)

Server built:   Oct  62011 11:06:33

[root@Wiker ~]# vim /etc/httpd/conf/httpd.conf

NameVirtualHost 192.168.0.1:80  #apache2.2.xx版本需要开启此选项,而且要和下面的保持一致;2.4.x版本就不需要此项设置了

<Virtualhost 192.168.0.1:80 >  #这三个虚拟主机如果输入ip地址来访问的话,访问的都是第一个,使用域名可以访问三个内容

   ServerName www.80.com

   DocumentRoot /web/vhosts/www.80.com

</Virtualhost>


<Virtualhost 192.168.0.1:80 >

   ServerName www.8080.com

   DocumentRoot /web/vhosts/www.8080.com

</Virtualhost>

<Virtualhost192.168.0.1:80 >

   ServerName www.8088.com

   DocumentRoot /web/vhosts/www.8088.com

</Virtualhost>

[root@Wiker ~]#service httpd restart

Stopping httpd:                                           [  OK  ]

Starting httpd:                                           [  OK  ]

使用浏览器进行测试

wKioL1MDf8DxfPsWAAEcJZ1hkMw583.jpg


虚拟主机的别名

我们在本地hosts添加一条记录

192.168.0.1www.80.com

192.168.0.1www.8080.com

192.168.0.1www.8088.com

192.168.0.1bbs.8080.com

想让bbs.8080.com显示的和www.8080.com一样的内容,但是我们浏览器访问的时候发现是

wKioL1MDf93jKeC2AABqoZjxYBQ445.jpg

因为bbs.8080.com自动匹配httpd.conf配置里面的第一个虚拟主机了,如果想实现此要求可以进行如下设置,设置虚拟主机的别名

[root@Wiker ~]# vim/etc/httpd/conf/httpd.conf

<Virtualhost192.168.0.1:80 >

   ServerName www.80.com

   DocumentRoot /web/vhosts/www.80.com

</Virtualhost>


<Virtualhost192.168.0.1:80 >

ServerAlias bbs.8080.com

   ServerName www.8080.com

   DocumentRoot /web/vhosts/www.8080.com

</Virtualhost>

<Virtualhost192.168.0.1:80 >

   ServerName www.8088.com

   DocumentRoot /web/vhosts/www.8088.com

[root@Wiker ~]#service httpd restart

Stopping httpd:                                           [  OK  ]

Starting httpd:                                           [  OK  ]

然后现在访问就可以实现上面的要求了

wKiom1MDf_LzQguEAABlvMMyhFQ098.jpg

默认虚拟主机定义

[root@Wiker ~]# vim/etc/httpd/conf/httpd.con

<Virtualhost192.168.0.1:80 >

   ServerName default

   DocumentRoot /web/vhosts/default

   ErrorDocument 404 /site_list.html

</Virtualhost>

或者

[root@Wiker ~]# vim/etc/httpd/conf/httpd.conf

<Virtualhost_default_:80 >

   DocumentRoot /web/vhosts/default

</Virtualhost>



生成一批虚拟主机:

如果想生成一批虚拟主机,比如使用的名字是www.a.com,就存放在/web/vhosts/www.a.com/目录下www.b.com就存放在/web/vhosts/www.b.com/目录下

VirtualDocumentRoot  /web/vhosts/%0/

VirtualScriptAlias/www/vhosts/%0/cgi-bin



将虚拟主机的配置信息放置于数据库中(第三方模块)

mod_vhost_dbi

下载地址:http://www.outoforder.cc/projects/httpd/mod_vhost_dbi/

需要先安装libdbi库文件

Libdbi

libdbi-drivers

libdbi-dbd-mysql