什麼是虛擬主機?html
一個 http 服務要配置多個站點,就須要用到虛擬主機。linux
什麼狀況會用到虛擬主機?
apache
部署多個站點,每一個站點但願用不一樣的域名和站點目錄,或者是不一樣的端口,不一樣的 IP。vim
虛擬主機的分類瀏覽器
1)基於域名app
2)基於端口ide
3)基於 IPspa
由於搭建基於端口,IP的虛擬主機的狀況較少,因此本文只介紹的是搭建基於域名的虛擬主機3d
虛擬主機的規劃htm
配置三個基於域名的虛擬主機,域名及站點目錄以下
www.yuci.com /var/html/www
blog.yuci.com /var/html/blog
bbs.yuci.com /var/html/bbs
當前環境介紹
CentOS6.7x86_64,分別關閉了 iptables,selinux
uname -r
在搭建虛擬主機前,系統中須要有安裝好的 Apache 環境
Apache環境搭建:http://aby028.blog.51cto.com/5371905/1896181
一鍵建立三個站點目錄,並分別在其目錄裏建立首頁文件
mkdir -p /var/html/{www,blog,bbs}
touch /var/html/{www,blog,bbs}/index.html
經過 tree 命令查看是否建立成功
tree /var/html
若是報錯須要 yum 安裝一下:yum -y install tree
經過 for 循環分別將對應的域名寫入三個目錄中的 index.html 文件
for name in www blog bbs;do echo "http://$name.yuci.com" > /var/html/$name/index.html;done
還能夠經過 for 循環使用 cat 命令檢查
for name in www blog bbs;do cat /var/html/$name/index.html;done
編輯虛擬主機的配置文件
vim /application/apache/conf/extra/httpd-vhosts.conf
編輯 apache 的主配置文件
vim /application/apache/conf/httpd.conf
在 httpd.conf 文件的最後加入如下字段
<Directory "/var/html">
AllowOverride None
Order allow,deny
Allow from all
</Directory>
檢車語法沒有報錯後,平滑重啓
/application/apache/bin/apachectl -t
/application/apache/bin/apachectl graceful
編輯本機的 hosts 文件
hosts 文件路徑 C:\Windows\System32\drivers\etc
也能夠經過 cmd 執行 drivers
編輯內容以下:地址在前,域名在後
當本機分別 ping 三個域名,檢測是否能夠 ping 通
ping www.yuci.com
ping blog.yuci.com
ping bbs.yuci.com
經過瀏覽器分別訪問三個域名,都可以正常訪問並正確看到內容