虚拟主机<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Apache实现的虚拟主机主要有3种类型:

    一是基于IP地址的虚拟主机;

    二是基于端口的虚拟主机;

    三是基于名字的虚拟主机。 

1.基于IP地址的虚拟主机

思路:在同一台主机上配置多个IP地址,每个IP地址对应一个虚拟主机。

第一步,在一台主机上配置多个IP地址,执行如下命令。

第一步:要需你出多块网卡:

 #cd /etc/sysconfig/network-scrips/

进入这个目录下可以用cp :

#cp ifcfg-eth0 ifcfg-eth0:0 (建立ifcfg-eth0:0这个网卡)

在将ifcfg-eth0 将里面的相应的参数该了

#Ifup Ifcfg-eth0:0 开启eth0:0这个网卡

#ifdown ifcfg-eth0:0 关闭eth0:0 这个网卡

第二步进入主配置文件/etc/httpd/conf/httpd.conf

<VirtualHost 10.22.1.102:80>

    ServerAdmin [email]webmaster@tcbuu.cn[/email]

    DocumentRoot /www/iproot1

    ServerName 10.22.1.102

    ErrorLog logs/10.22.1.102-error_log (后面的两行可以不要)

    CustomLog logs/10.22.1.102-access_log common

</VirtualHost>

<VirtualHost 10.22.1.103:80>

    ServerAdmin [email]webmaster2@tcbuu.cn[/email]

    DocumentRoot /www/iproot2

    ServerName 10.22.1.103

    ErrorLog logs/10.22.1.103-error_log (这个也是)

    CustomLog logs/10.22.1.103-access_log common

</VirtualHost>

第三步,建立两个虚拟主机的文档根目录及相应测试页面。

#mkdir  –p  /www/iproot1

#mkdir  –p  /www/iproot2

#vi  /www/iproot1/index.html

内容如下:

<html>

  this is the IP_based VirtualHost 10.22.1.102!

</html>

#vi  /www/iproot2/index.html

内容如下:

<html>

  this is the IP_based VirtualHost 10.22.1.103!

</html> 

第四步,运行与测试。

#service  httpd  restart

#elinks  [url]http://10.22.1.102[/url]

说明:利用RHEL 4.0中提供的文本浏览器elinks来测试。

#elinks  [url]http://10.22.1.103[/url]

2.基于端口的虚拟主机

思路:在同一台主机上针对一个IP地址和不同的端口来建立虚拟主机,即每个端口对应一个虚拟主机。

第一步,建立新的子接口并配置IP地址为10.22.1.104

第二步,编辑Apache的主配置文件httpd.conf,增加监听的端口号8001和8002

在Section 1 中增加两行配置,分别监听8001和8002端口

Listen 80

Listen 8001

Listen 8002

第三步,编辑Apache的主配置文件httpd.conf,建立基于端口的虚拟主机配置段,内容如下:

<VirtualHost 10.22.1.104:8001>

    ServerAdmin [email]webmaster3@tcbuu.cn[/email]

    DocumentRoot /www/portroot1

    ServerName 10.22.1.104

    ErrorLog logs/10.22.1.104-8001-error_log(后两行也可以省略的)

    CustomLog logs/10.22.1.104-8001-access_log common

</VirtualHost>

<VirtualHost 10.22.1.104:8002>

    ServerAdmin [email]webmaster4@tcbuu.cn[/email]

    DocumentRoot /www/portroot2

    ServerName 10.22.1.104

    ErrorLog logs/10.22.1.104-8002-error_log

    CustomLog logs/10.22.1.104-8002-access_log common

</VirtualHost>

3.基于名字的虚拟主机

思路:在同一台主机上针对相同的IP地址和端口号建立基于名字的虚拟主机。

第一步,在DNS服务器的区域数据库文件中增加两条A记录和两条PTR记录。为了不影响前面的虚拟主机,这里再增加一个子接口,并配置为10.22.1.105。

# ifconfig  eth0:3  10.22.1.105  netmask  255.255.255.0

接下来,配置DNS以支持新的域名解析。DNS正向区域数据库中增加的记录如下:

www1.tcbuu.cn.  IN  A  10.22.1.105

www2.tcbuu.cn.  IN  A  10.22.1.105

第二步,编辑Apache的主配置文件,激活基于名字的虚拟主机,并建立两个基于名字的虚拟主机配置段。

NameVirtualHost 10.22.1.105:80

功能:针对10.22.1.105:80配置基于名字的虚拟主机。

说明:这是非常重要的一条指令,正是该指令的作用才激活了基于名字的虚拟主机的功能。

NameVirtualHost 10.22.1.105:80

<VirtualHost 10.22.1.105:80>

    ServerAdmin [email]webmaster5@tcbuu.cn[/email]

    DocumentRoot /www/nameroot1

    ServerName www1.tcbuu.cn

    ErrorLog logs/www1.tcbuu.cn-error_log

    CustomLog logs/www1.tcbuu.cn-access_log common

</VirtualHost>

<VirtualHost 10.22.1.105:80>

    ServerAdmin [email]webmaster6@tcbuu.cn[/email]

    DocumentRoot /www/nameroot2

    ServerName www2.tcbuu.cn

    ErrorLog logs/www2.tcbuu.cn-error_log

    CustomLog logs/www2.tcbuu.cn-access_log common

</VirtualHost> 

第三步,建立两个虚拟主机的文档根及主页。

#mkdir  -p  /www/nameroot1

#mkdir  -p  /www/nameroot2

#vi  /www/nameroot1/index.html

内容如下:

<html>

   welcome to name_based VirtualHost www1.tcbuu.cn.

</html>

#vi  /www/nameroot2/index.html

内容如下:

<html>

   welcome to name_based VirtualHost www2.tcbuu.cn.

</html>

第四步,运行与测试。

#service  httpd  restart

#elinks  www1.tcbuu.cn

#elinks  www2.tcbuu.cn