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”內容。
[root@svr7 ~]# 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地址。
[root@svr7 ~]# 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組合鍵中止測試
.. ..
[root@svr7 ~]# 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組合鍵中止測試
.. ..
[root@svr7 ~]#
步驟二:配置 httpd 服務,實現 2 個不同的網站
1)爲兩個網站分別準備網頁目錄、測試網頁index.html
第一個網站的內容:
[root@svr7 ~]# mkdir /var/www/web1
[root@svr7 ~]# vim /var/www/web1/index.html
Hello Student
第二個網站的內容:
[root@svr7 ~]# mkdir /var/www/web2
[root@svr7 ~]# vim /var/www/web2/index.html
Hello Engineer
2)添加新的Web配置,支持兩個虛擬Web主機,分別指向不同的網頁目錄
新建虛擬主機配置:
[root@svr7 ~]# 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>
作好語法檢查,確保沒有配置錯誤:
[root@svr7 ~]# httpd -t
.. ..
Syntax OK
[root@svr7 ~]# systemctl restart httpd
3)從瀏覽器分別訪問兩個虛擬Web主機,對比頁面結果
當訪問 http://tts8.tedu.cn/ 時,網頁顯示 “Hello Student”,如圖-3所示。
當訪問 http://ne.tedu.cn/ 時,網頁顯示 “Hello Engineer”,如圖-4所示。