发布于 2014-12-17 12:58:29 | 381 次阅读 | 评论: 0 | 来源: PHPERZ
Apache Web服务器
Apache是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件。
本文为大家讲解的是apache虚拟主机的搭建配置,感兴趣的同学参考下.
Apache是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件。
虚拟主机是指在同一台服务器上运行多个web站点,其中的每一个站点实际上并不占用整个服务器,可以充分利用服务器的硬件资源,节省成本。
基于域名 使用不同的域名,但是对应的ip和端口都是一样的
基于IP 使用不同的域名,但是每个域名对应的ip不一样
基于端口 使用不同的域名,但是ip一样,对应的端口不一样,用户访问的时候需要在域名后面指定端口号
(测试环境www.test.com,bbs.test.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.test.com
ServerAlias web1
ErrorLog "logs/www.test.com-error_log"
CustomLog "logs/www.test.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.test.com
ErrorLog "logs/bbs.test.com-error_log"
CustomLog "logs/bbs.test.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.test.com</h1>" > www/index.html
[root@Apache_Server htdocs]# echo"<h1>bbs.test.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.test.com bbs.test.com" >>/etc/hosts
[root@Client ~]# tail -1 /etc/hosts
192.168.254.100 www.test.combbs.test.com
[root@Client ~]# elinks -dumpwww.test.com
www.test.com
[root@Client ~]# elinks -dumpbbs.test.com
bbs.test.com
基于域名的虚拟主机创建成功
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.test.com
ServerAlias web1
ErrorLog "logs/www.test.com-error_log"
CustomLog "logs/www.test.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.test.com
ErrorLog "logs/bbs.test.com-error_log"
CustomLog "logs/bbs.test.com-access_log" common
</VirtualHost>
3)测试文件上面已经创建,现在需要修改hosts文件
[root@Client ~]# tail -1 /etc/hosts
192.168.254.100 www.test.com
172.16.254.29 bbs.test.com
4)重启apache服务,并测试
[root@Apache_Server ~]# service httpdrestart
停止 httpd: [确定]
正在启动 httpd: [确定]
[root@Client ~]# elinks -dump www.test.com
www.test.com
[root@Client ~]# elinks -dumpbbs.test.com
bbs.test.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.test.com
ServerAlias web1
ErrorLog "logs/www.test.com-error_log"
CustomLog "logs/www.test.com-access_log" common
</VirtualHost>
<VirtualHost 192.168.254.100:8080>
ServerAdminJeckFY@vip.com
DocumentRoot“/usr/local/httpd-2.4.4/htdocs/bbs”
ServerNamebbs.test.com
ErrorLog“logs/bbs.test.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.test.combbs.test.com
3)编辑httpd.conf文件,加入监听的8080端口
Listen 80
Listen 8080
4)重启apache服务并测试
[root@Apache_Server ~]# service httpdrestart
停止 httpd: [确定]
正在启动 httpd: [确定]
[root@Client ~]# elinks -dumpwww.test.com:80
www.test.com
[root@Client ~]# elinks -dumpbbs.test.com:8080
bbs.test.com
基于端口的虚拟主机完成
总结:
1.三种虚拟主机类型的实现主要是修改<virtualhost >里的参数。
2.基于域名的虚拟主机需要开启NameVirtualhost参数。其他两个用#注释掉即可。
但是注意:httpd-2.4.*版本不需要此参数
3.基于ip的虚拟主机至少需要两块网卡
4.基于端口的虚拟主机需要在httpd.conf中添加监听的端口