配置虚拟网站主机

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了配置虚拟网站主机相关的知识,希望对你有一定的参考价值。

1 问题

本例要求以案例1的结果为基础,通过httpd网站服务器实现虚拟主机的支持,完成下列任务:

1)修改 /etc/hosts 文件,临时解决DNS名称识别问题

在文件尾添加“Web服务器IP地址 tts8.tedu.cn ne.tedu.cn”内容

2)配置 httpd 服务,实现 2 个不同的网站

本机访问 http://tts8.tedu.cn/ 时,网页显示 “Hello Student”
本机访问 http://ne.tedu.cn/ 时,网页显示 “Hello Engineer
    步骤

实现此案例需要按照如下步骤进行。

步骤一:修改 /etc/hosts 文件,临时解决DNS名称识别问题

1)添加主机映射记录,指向虚拟机的正常IP地址(如 192.168.70.120)

在文件尾添加“Web服务器IP地址 tts8.tedu.cn ne.tedu.cn”内容。
[[email protected] ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.70.120 tts8.tedu.cn ne.tedu.cn
2)测试访问结果

使用ping命令检测到两个域名的访问,确保可连通且对应到正确的IP地址。
[[email protected] ~]# ping tts8.tedu.cn
PING tts8.tedu.cn (192.168.70.120) 56(84) bytes of data.
64 bytes from tts8.tedu.cn (192.168.70.120): icmp_seq=1 ttl=64 time=0.321 ms
64 bytes from tts8.tedu.cn (192.168.70.120): icmp_seq=2 ttl=64 time=0.052 ms
^C //按Ctrl+c组合键中止测试
.. ..
[[email protected] ~]# ping ne.tedu.cn
PING tts8.tedu.cn (192.168.70.120) 56(84) bytes of data.
64 bytes from tts8.tedu.cn (192.168.70.120): icmp_seq=1 ttl=64 time=0.022 ms
64 bytes from tts8.tedu.cn (192.168.70.120): icmp_seq=2 ttl=64 time=0.051 ms
^C //按Ctrl+c组合键中止测试
.. ..
[[email protected] ~]#
步骤二:配置 httpd 服务,实现 2 个不同的网站

1)为两个网站分别准备网页目录、测试网页index.html

第一个网站的内容:
[[email protected] ~]# mkdir /var/www/web1
[[email protected] ~]# vim /var/www/web1/index.html
Hello Student
第二个网站的内容:

[[email protected] ~]# mkdir  /var/www/web2
[[email protected] ~]# vim  /var/www/web2/index.html
Hello  Engineer
    2)添加新的Web配置,支持两个虚拟Web主机,分别指向不同的网页目录

新建虚拟主机配置:

[[email protected] ~]# vim  /etc/httpd/conf.d/vhosts.conf 
<VirtualHost  *:80>
    ServerName  tts8.tedu.cn
    DocumentRoot  /var/www/web1
</VirtualHost>
<VirtualHost  *:80>
    ServerName  ne.tedu.cn
    DocumentRoot  /var/www/web2
</VirtualHost>
    作好语法检查,确保没有配置错误:
        [[email protected] ~]# httpd  -t
.. ..
Syntax OK

[[email protected] ~]# systemctl restart httpd

3)从浏览器分别访问两个虚拟Web主机,对比页面结果

当访问 http://tts8.tedu.cn/ 时,网页显示 “Hello Student”,如图-3所示。
技术分享图片

当访问 http://ne.tedu.cn/ 时,网页显示 “Hello Engineer”,如图-4所示。
技术分享图片

以上是关于配置虚拟网站主机的主要内容,如果未能解决你的问题,请参考以下文章

配置虚拟主机

配置虚拟主机

APache网站服务配置访问控制和构建虚拟主机

配置虚拟网站主机

配置虚拟网站主机

Nginx虚拟主机配置详解