Linux项目实战之基于IP的虚拟主机


说明:


服务器上有多块网卡,每块网卡对应一个IP地址,每个地址绑定一个域名。


补充:


多网卡实现方法:(1)多个物理网卡(2)多个虚拟网卡,例如eth0:0  eth0:1 ……


实验环境:


       服务器安装了Apache httpd服务(有测试网页,站点位置/var/www/html/index.html一台主机、一个物理网卡eth0(在此基础上再创建eth0:0  eth0:1两个虚拟网卡)win 7客户端


实验目标:


通过客户端访问同一主机上的不同域名不同IP


www.tarena.com192.168.10.10


www.baidu.com192.168.10.11

www.google.com192.168.10.12


步骤:


1)创建虚拟网卡


             [root@localhost ~]# cd /etc/sysconfig/network-scripts/


[root@localhost network-scripts]# cp ifg-eth0 ifg-eth0:0


[root@localhost network-scripts]# cp ifg-eth0 ifg-eth0:1


[root@localhost network-scripts]# vim ifg-eth0:0


[root@localhost network-scripts]# vim ifg-eth0:1


[root@localhost network-scripts]# service network restart


网卡内容:


eth0:


             DEVICE=eth0


BOOTPROTO=static


IPADDR=192.168.1.254


NETMASK=255.255.255.0


HWADDR=00:0C:29:E5:7C:DF


ONBOOT=yes



eth0:0


DEVICE=eth0:0


BOOTPROTO=static


IPADDR=192.168.1.253


NETMASK=255.255.255.0


HWADDR=00:0C:29:E5:7C:DF


ONBOOT=yes



eth0:1


DEVICE=eth0:1


BOOTPROTO=static


IPADDR=192.168.1.252


NETMASK=255.255.255.0


HWADDR=00:0C:29:E5:7C:DF


ONBOOT=yes


(2)修改主配置文件


[root@localhost ~]#vim  /etc/httpd/conf.d/virt.conf //在配置目录文件中创建并编辑virt.conf虚拟主机配置文件


virt.conf虚拟主机配置文件内容(可以参考主配置文件/etc/httpd/conf/httpd中的模板)


<VirtualHost 192.168.10.10:80>


  DocumentRoot /var/www/ht

   ServerName www.tarena.com

  ErrorLog logs/tarena.com-error_log


CustomLog logs/tarena.com-access_log common


</VirtualHost>




<VirtualHost 192.168.10.11:80>


       DocumentRoot /data/web/baidu


       ServerName www.baidu.com


       ErrorLog logs/baidu.com-error_log


       CustomLog logs/baidu.com-access_log common


</VirtualHost>




<VirtualHost 192.168.10.12:80>


       DocumentRoot /data/web/google


       ServerName www.google.com


       ErrorLog logs/google.com-error_log


       CustomLog logs/google.com-access_log common


</VirtualHost>


(3)启动服务


[root@localhost ~]# service httpd restart



(4)客户端测试(注意更改hosts文件)


在地址栏输入:http://www.tarena.com


Linux项目实战之基于IP的虚拟主机_虚拟主机

Linux项目实战之基于IP的虚拟主机_服务器_02


在地址栏输入:http://www.baidu.com
Linux项目实战之基于IP的虚拟主机_Linux_03Linux项目实战之基于IP的虚拟主机_IP地址_04




在地址栏输入:http://www.google.com



Linux项目实战之基于IP的虚拟主机_Linux_05Linux项目实战之基于IP的虚拟主机_客户端_06







附:


各站点日志文件:


Linux项目实战之基于IP的虚拟主机_IP地址_07








虚拟主机配置文件内容模板:


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

Linux项目实战之基于IP的虚拟主机_服务器_08