apache虚拟主机的搭建

时间:2022-09-01 21:00:21

apache虚拟主机的搭建

虚拟主机的介绍

虚拟主机

虚拟主机是指在同一台服务器上运行多个web站点,其中的每一个站点实际上并不占用整个服务器,可以充分利用服务器的硬件资源,节省成本。

类型

  • 基于域名   使用不同的域名,但是对应的ip和端口都是一样的

  • 基于IP     使用不同的域名,但是每个域名对应的ip不一样

  • 基于端口  使用不同的域名,但是ip一样,对应的端口不一样,用户访问的时候需要在域名后面指定端口号

三种类型虚拟主机的搭建

(测试环境www.beyondjie.com,bbs.beyondjie.com,由于实验环境没有DNS服务器,所以需要修改hosts文件)

基于域名

1)编辑/usr/local/httpd/conf/extra/httpd-vhosts.com文件

[root@Apache_Server conf]# catextra/httpd-vhosts.conf

NameVirtualHost 192.168.254.100:80

<VirtualHost 192.168.254.100:80>

   ServerAdmin JeckFY@vip.com

   DocumentRoot "/usr/local/httpd-2.4.4/htdocs/www"

   ServerName www.beyondjie.com

   ServerAlias web1

   ErrorLog "logs/www.beyondjie.com-error_log"

   CustomLog "logs/www.beyondjie.com-access_log" common

</VirtualHost>

 

<VirtualHost 192.168.254.100:80>

   ServerAdmin JeckFY@vip.com

   DocumentRoot "/usr/local/httpd-2.4.4/htdocs/bbs"

   ServerName bbs.beyondjie.com

   ErrorLog "logs/bbs.beyondjie.com-error_log"

   CustomLog "logs/bbs.beyondjie.com-access_log" common

</VirtualHost>

2)编辑httpd.conf,支持虚拟主机

[root@Apache_Server conf]# echo"Include conf/extra/httpd-vhosts.conf" >>httpd.conf

[root@Apache_Server conf]# tail -1httpd.conf

Include conf/extra/httpd-vhosts.conf

 

3)创建对应的测试网页文件。重启Apache

[root@Apache_Server conf]# cd/usr/local/httpd/htdocs/

[root@Apache_Server htdocs]# rm -fr *

[root@Apache_Server htdocs]# mkdir www

[root@Apache_Server htdocs]# mkdir bbs

[root@Apache_Server htdocs]# echo"<h1>www.beyondjie.com</h1>" > www/index.html

[root@Apache_Server htdocs]# echo"<h1>bbs.beyondjie.com</h1>" > bbs/index.html  

[root@Apache_Server htdocs]# catwww/index.html

<h1>www.beyondjie.com</h1>

[root@Apache_Server htdocs]# catbbs/index.html   

<h1>bbs.beyondjie.com</h1>

重启apache

[root@Apache_Server conf]# service httpdrestart

停止 httpd:                                              [确定]

正在启动 httpd:AH00548:NameVirtualHost has no effect and will be removed in the next release/usr/local/httpd-2.4.4/conf/extra/httpd-vhosts.conf:1

                                                          [确定]

重启提示NameVirtualHost has no effect and will be removed 意思是httpd-2.4.4版本不用使用NameVirtualHost。所以删掉即可(httpd-2.2.*需要)

在客户机上配置hosts解析,并测试

[root@Client ~]# echo"192.168.254.100 www.beyondjie.com bbs.beyondjie.com" >>/etc/hosts

[root@Client ~]# tail -1 /etc/hosts

192.168.254.100 www.beyondjie.combbs.beyondjie.com

[root@Client ~]# elinks -dumpwww.beyondjie.com

                              www.beyondjie.com

[root@Client ~]# elinks -dumpbbs.beyondjie.com

                              bbs.beyondjie.com

基于域名的虚拟主机创建成功

 

基于IP

1)本机两块网卡,ip如下

[root@Apache_Server ~]# ifconfig

eth0     Link encap:Ethernet  HWaddr 00:0C:29:F2:1A:88 

         inet addr:172.16.254.29  Bcast:172.16.254.255  Mask:255.255.255.0

         inet6 addr: fe80::20c:29ff:fef2:1a88/64 Scope:Link

         UP BROADCAST RUNNING MULTICAST MTU:1500  Metric:1

         RX packets:17 errors:0 dropped:0 overruns:0 frame:0

         TX packets:13 errors:0 dropped:0 overruns:0 carrier:0

         collisions:0 txqueuelen:1000

         RX bytes:1637 (1.5 KiB)  TXbytes:1146 (1.1 KiB)

 

eth1     Link encap:Ethernet  HWaddr00:0C:29:F2:1A:92 

         inet addr:192.168.254.100  Bcast:192.168.254.255  Mask:255.255.255.0

         inet6 addr: fe80::20c:29ff:fef2:1a92/64 Scope:Link

         UP BROADCAST RUNNING MULTICAST MTU:1500  Metric:1

         RX packets:33 errors:0 dropped:0 overruns:0 frame:0

         TX packets:36 errors:0 dropped:0 overruns:0 carrier:0

         collisions:0 txqueuelen:1000

         RX bytes:3865 (3.7 KiB)  TXbytes:4945 (4.8 KiB)

2)编辑/usr/local/httpd/conf/extra/httpd-vhosts.com文件

[root@Apache_Server ~]# cat /usr/local/httpd/conf/extra/httpd-vhosts.conf   

<VirtualHost 192.168.254.100:80>

   ServerAdmin JeckFY@vip.com

   DocumentRoot "/usr/local/httpd-2.4.4/htdocs/www"

   ServerName www.beyondjie.com

   ServerAlias web1

   ErrorLog "logs/www.beyondjie.com-error_log"

   CustomLog "logs/www.beyondjie.com-access_log" common

</VirtualHost>

 

<VirtualHost 172.16.254.29:80>

   ServerAdmin JeckFY@vip.com

   DocumentRoot "/usr/local/httpd-2.4.4/htdocs/bbs"

   ServerName bbs.beyondjie.com

   ErrorLog "logs/bbs.beyondjie.com-error_log"

   CustomLog "logs/bbs.beyondjie.com-access_log" common

</VirtualHost>

 

3)测试文件上面已经创建,现在需要修改hosts文件

[root@Client ~]# tail -1 /etc/hosts

192.168.254.100 www.beyondjie.com

172.16.254.29 bbs.beyondjie.com

4)重启apache服务,并测试

[root@Apache_Server ~]# service httpdrestart

停止 httpd:                                              [确定]

正在启动 httpd:                                           [确定]

 

[root@Client ~]# elinks -dump www.beyondjie.com

                              www.beyondjie.com

[root@Client ~]# elinks -dumpbbs.beyondjie.com

                              bbs.beyondjie.com

 

基于ip的虚拟主机已经完成

基于端口

1)编辑/usr/local/httpd/conf/extra/httpd-vhosts.com文件

[root@Apache_Server ~]# cat /usr/local/httpd/conf/extra/httpd-vhosts.conf   

<VirtualHost 192.168.254.100:80>

   ServerAdmin JeckFY@vip.com

   DocumentRoot "/usr/local/httpd-2.4.4/htdocs/www"

   ServerName www.beyondjie.com

   ServerAlias web1

   ErrorLog "logs/www.beyondjie.com-error_log"

   CustomLog "logs/www.beyondjie.com-access_log" common

</VirtualHost>

 

<VirtualHost 192.168.254.100:8080>

ServerAdminJeckFY@vip.com

DocumentRoot“/usr/local/httpd-2.4.4/htdocs/bbs”

ServerNamebbs.beyondjie.com

ErrorLog“logs/bbs.beyondjie.com-error_log”

CustomLog“logs/bbs.beyondjie.com-access_log” common

</VirtualHost>

2)测试文件上面已经创建,现在需要修改hosts文件

[root@Client ~]# tail -1 /etc/hosts

192.168.254.100 www.beyondjie.combbs.beyondjie.com

3)编辑httpd.conf文件,加入监听的8080端口

Listen 80

Listen 8080

 

4)重启apache服务并测试

[root@Apache_Server ~]# service httpdrestart

停止 httpd:                                              [确定]

正在启动 httpd:                                           [确定]

 

[root@Client ~]# elinks -dumpwww.beyondjie.com:80

                              www.beyondjie.com

[root@Client ~]# elinks -dumpbbs.beyondjie.com:8080

                              bbs.beyondjie.com

基于端口的虚拟主机完成

总结:

1.三种虚拟主机类型的实现主要是修改<virtualhost  >里的参数。

2.基于域名的虚拟主机需要开启NameVirtualhost参数。其他两个用#注释掉即可。

但是注意:httpd-2.4.*版本不需要此参数

3.基于ip的虚拟主机至少需要两块网卡

4.基于端口的虚拟主机需要在httpd.conf中添加监听的端口


本文出自 “Study-Everyday” 博客,请务必保留此出处http://studys.blog.51cto.com/9736817/1591141