实验要求:在一台主机上搭建2个网站服务

           www.aaa.com    

           www.bbb.com  

           实现基于域名的虚拟主机

           实现基于IP的虚拟主机

           实现基于端口的虚拟主机

一.创建实验环境

1.实现通过域名访问站点需要DNS的解析,可修改DNS数据库文件记录将2个站点IP都指向本机,

也可通过修改/etc/hosts文件来简单实现

# vim /etc/hosts

www.aaa.com       192.168.100.10

www.bbb.com       192.168.100.10


2.创建网站存放目录及测试页

# mkdir -p /data/web/{aaa,bbb}

# echo "<h1>www.aaa.com</h1>" /data/web/aaa/index.html

# echo "<h1>www.bbb.com</h1>" /data/web/bbb/index.html


3.安装软件包

# yum -y install httpd


二.搭建基于域名虚拟主机

1.修改主配置文件

# cd /etc/httpd/conf.d/

# vim vhost.conf

NameVirtualHost 192.168.100.10:80

<VirtualHost 192.168.100.10:80>

   DocumentRoot /data/web/aaa

   ServerName www.aaa.com

   ErrorLog logs/www.aaa.com-error_log

   CustomLog logs/www.aaa.com-access_log common

</VirtualHost>

<VirtualHost 192.168.100.10:80>

   DocumentRoot /data/web/bbb

   ServerName www.bbb.com

   ErrorLog logs/www.bbb.com-error_log

   CustomLog logs/www.bbb.com-access_log common

</VirtualHost>


2.启动服务

# service httpd start

# chkconfig httpd on


3.浏览器测试

http://www.aaa.com

http://www.bbb.com


三.搭建基于IP的虚拟主机

1.修改主配置文件

# ifconfig eth0:0 192.168.100.20                 //设置另一个IP      

# vim /etc/httpd/conf.d/vhost.conf

#NameVirtualHost *:80                        //不需要这行

<VirtualHost 192.168.10.10:80>

   DocumentRoot /data/web/aaa

   ServerName 192.168.100.10

   ErrorLog logs/192.168.100.10-error_log

   CustomLog logs/192.168.100.10-access_log common

</VirtualHost>

<VirtualHost 192.168.100.20:80>

   DocumentRoot /data/web/bbb

   ServerName 192.168.100.20

   ErrorLog logs/192.168.100.20-error_log

   CustomLog logs/192.168.100.20-access_log common

</VirtualHost>


2.启动服务

# service httpd restart

# chkconfig httpd on


3.浏览器测试

http://192.168.100.10

http://192.168.100.20


四.搭建基于端口的虚拟主机

1.修改主配置文件

# vim /etc/httpd/conf/httpd.conf

...

134 Listen 80

135 Listen 81

# vim /etc/httpd/conf.d/vhost.conf

#NameVirtualHost *:80

<VirtualHost 192.168.100.10:80>

   DocumentRoot /data/web/aaa

   ErrorLog logs/192.168.100.10:80-error_log

   CustomLog logs/192.168.100.10:80-access_log common

</VirtualHost>

<VirtualHost 192.168.100.10:81>

   DocumentRoot /data/web/bbb

   ErrorLog logs/192.168.100.10:81-error_log

   CustomLog logs/192.168.100.10:81-access_log common

</VirtualHost>


2.启动服务

# service httpd restart

# chkconfig httpd on


3.浏览器测试

http://192.168.100.10:80

http://192.168.100.10:81