Apache服務器實現基於名稱的虛擬主機服務

實驗環境:LinuxAS4;雙網卡,IP分別爲:192.10.30.3/24 | 192.10.10.3/24 對應的兩個域名:tgh.com和gary.com;主機名稱分別爲www.tgh.com; www.gary.com;
(一)安裝Apache服務器相關軟件包
[root@localhost /]#rpm -qa |grep httpd
httpd-suexec-2.0.52-22.ent
httpd-manual-2.0.52-22.ent
system-config-httpd-1.3.1-1
httpd-2.0.52-22.ent
[root@localhost /]#rpm -qa |grep apr
apr-util-0.9.4-21
apr-0.9.4-24.5
 
如果已經有上面安裝包後,且你已經做好DNS,那麼在重啓httpd(service httpd restart)後你就可以直接在firefox(瀏覽器)中輸入你的主機名稱了,打開的就是Apache的測試頁面,例如: http://www.tgh.com  或 http://www.gary.com
(二)用Apache實現基於名字的虛擬主機服務
在/etc/httpd/conf下找到Apache主配置文件httpd.conf
[root@localhost /]#grep -v "^#" /etc/httpd/conf/httpd.conf    #查看httpd.conf中除"#"開頭的信息行
[root@localhost conf]#vi httpd.conf         
i:在末行模式下找到需要修改的位置    :/ServerAdmin
ServerAdmin 是用來設置Web管理員的E-mail地址的,當系統連接出現錯誤時會及時通知Web管理員.
ServerAdmin [email protected]  --> ServerAdmin root@tgh.com      #修改成Web管理員的郵箱
ii:在末行模式下找到需要修改的位置  :/ServerName
ServerName 是用來配置Web服務器的Internet主機名和端口以標識網站
ServerName www.example.com:80 --> ServerName www.tgh.com:80  #該句默認是註釋語句,須將前面的"#"刪除.若沒有DNS,此處得用IP地址+端口
在httpd.conf文件的末尾加上以下內容:
NameVirtualHost 192.10.30.3:80
<VirtualHost 192.10.30.3:80>
ServerAdmin [email protected]
DocumentRoot /var/www/html/www.tgh.com  #需創建www.tgh.com目錄;用於存放網頁文件,路徑可以自己指定
ServerName ww.tgh.com
Errorlog /var/log/httpd/www.tgh.com/error.log #需創建www.tgh.com目錄,用於存放錯誤日誌文件error.log
</virtualHost>
 
[root@localhost www.tgh.com] #touch index.html       在/var/www/html/www.tgh.com下創建index.html文件,用於訪問
[root@localhost www.tgh.com] #echo welcome to my website ! >index.html     #在index.html中寫入內容
[root@localhost www.tgh.com] #touch error.log            #在/var/log/httpd/www.tgh.com目錄下創建一個用於保存錯誤的日誌文件
 
 
重啓httpd就可以測試了!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章