设为首页 收藏本站
查看: 1267|回复: 0

[经验分享] apache虚拟主机的搭建

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-12-18 08:27:54 | 显示全部楼层 |阅读模式
apache虚拟主机的搭建
虚拟主机的介绍虚拟主机

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

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

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

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


三种类型虚拟主机的搭建
(测试环境www.beyondjie.com,bbs.beyondjie.com,由于实验环境没有DNS服务器,所以需要修改hosts文件)
基于域名
1)编辑/usr/local/httpd/conf/extra/httpd-vhosts.com文件
[iyunv@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,支持虚拟主机
[iyunv@Apache_Server conf]# echo"Include conf/extra/httpd-vhosts.conf" >>httpd.conf
[iyunv@Apache_Server conf]# tail -1httpd.conf
Include conf/extra/httpd-vhosts.conf

3)创建对应的测试网页文件。重启Apache
[iyunv@Apache_Server conf]# cd/usr/local/httpd/htdocs/
[iyunv@Apache_Server htdocs]# rm -fr *
[iyunv@Apache_Server htdocs]# mkdir www
[iyunv@Apache_Server htdocs]# mkdir bbs
[iyunv@Apache_Server htdocs]# echo"<h1>www.beyondjie.com</h1>" > www/index.html
[iyunv@Apache_Server htdocs]# echo"<h1>bbs.beyondjie.com</h1>" > bbs/index.html   
[iyunv@Apache_Server htdocs]# catwww/index.html
[iyunv@Apache_Server htdocs]# catbbs/index.html   
<h1>bbs.beyondjie.com</h1>
重启apache
[iyunv@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解析,并测试
[iyunv@Client ~]# echo"192.168.254.100 www.beyondjie.com bbs.beyondjie.com" >>/etc/hosts
[iyunv@Client ~]# tail -1 /etc/hosts
[iyunv@Client ~]# elinks -dumpwww.beyondjie.com
                              www.beyondjie.com
[iyunv@Client ~]# elinks -dumpbbs.beyondjie.com
                              bbs.beyondjie.com
基于域名的虚拟主机创建成功

基于IP
1)本机两块网卡,ip如下
[iyunv@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文件
[iyunv@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文件
[iyunv@Client ~]# tail -1 /etc/hosts
192.168.254.100 www.beyondjie.com
172.16.254.29 bbs.beyondjie.com
4)重启apache服务,并测试
[iyunv@Apache_Server ~]# service httpdrestart
停止 httpd:                                              [确定]
正在启动 httpd:                                           [确定]

[iyunv@Client ~]# elinks -dump www.beyondjie.com
                              www.beyondjie.com
[iyunv@Client ~]# elinks -dumpbbs.beyondjie.com
                              bbs.beyondjie.com

基于ip的虚拟主机已经完成
基于端口
1)编辑/usr/local/httpd/conf/extra/httpd-vhosts.com文件
[iyunv@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文件
[iyunv@Client ~]# tail -1 /etc/hosts
3)编辑httpd.conf文件,加入监听的8080端口
Listen 80
Listen 8080

4)重启apache服务并测试
[iyunv@Apache_Server ~]# service httpdrestart
停止 httpd:                                              [确定]
正在启动 httpd:                                           [确定]

[iyunv@Client ~]# elinks -dumpwww.beyondjie.com:80
                              www.beyondjie.com
[iyunv@Client ~]# elinks -dumpbbs.beyondjie.com:8080
                              bbs.beyondjie.com
基于端口的虚拟主机完成
总结:
1.三种虚拟主机类型的实现主要是修改<virtualhost  >里的参数。
2.基于域名的虚拟主机需要开启NameVirtualhost参数。其他两个用#注释掉即可。
但是注意:httpd-2.4.*版本不需要此参数
3.基于ip的虚拟主机至少需要两块网卡
4.基于端口的虚拟主机需要在httpd.conf中添加监听的端口


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-38199-1-1.html 上篇帖子: apache的介绍和安装详解 下篇帖子: Apache运行时寻找ServerName 虚拟主机
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表