配置虛擬網站主機

1.1 問題

本例要求以案例1的結果爲基礎,經過httpd網站服務器實現虛擬主機的支持,完成下列任務:html

1)修改 /etc/hosts 文件,臨時解決DNS名稱識別問題web

在文件尾添加「Web服務器IP地址 tts8.tedu.cn ne.tedu.cn」內容

2)配置 httpd 服務,實現 2 個不一樣的網站vim

本機訪問 http://tts8.tedu.cn/ 時,網頁顯示 「Hello Student」
本機訪問 http://ne.tedu.cn/ 時,網頁顯示 「Hello Engineer」

1.2 步驟

實現此案例須要按照以下步驟進行。瀏覽器

步驟一:修改 /etc/hosts 文件,臨時解決DNS名稱識別問題

1)添加主機映射記錄,指向虛擬機的正常IP地址(如 192.168.70.120)服務器

在文件尾添加「Web服務器IP地址 tts8.tedu.cn ne.tedu.cn」內容。dom

[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)測試訪問結果ide

使用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網站

第一個網站的內容:rest

[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  //設置虛擬主機dns域名
    DocumentRoot  /var/www/web1  //配置虛擬主機網站路徑
</VirtualHost>
<VirtualHost  *:80>
    ServerName  ne.tedu.cn //設置虛擬主機dns域名
    DocumentRoot  /var/www/web2 //配置虛擬主機網站路徑
</VirtualHost>

做好語法檢查,確保沒有配置錯誤:

[root@svr7 ~]# httpd  -t  // 測試配置文件httpd語法是否正確
.. ..
Syntax OK    //顯示語法沒有問題

重啓httpd服務:

[root@svr7 ~]# systemctl  restart  httpd

3)從瀏覽器分別訪問兩個虛擬Web主機,對比頁面結果

當訪問 http://tts8.tedu.cn/ 時,網頁顯示 「Hello Student」,如圖-3所示。
配置虛擬網站主機
當訪問 http://ne.tedu.cn/ 時,網頁顯示 「Hello Engineer」,如圖-4所示。
配置虛擬網站主機

相關文章
相關標籤/搜索