1. 程式人生 > >centos6上虛擬主機的實現

centos6上虛擬主機的實現

虛擬主機的實現

實現虛擬主機有三種方式:基於IP的實現、基於端口的實現、基於FQDN的實現

一、基於IP的實現

1.先創建三個站點:

mkdir /app/site1

mkdir /app/site2

mkdir /app/site3

2.在站點裏面輸入內容:

echo /app/site1/index.html > site1/index.html

echo /app/site2/index.html > site2/index.html

echo /app/site3/index.html > site3/index.html

3.增加三個IP地址:

ip add a 192.168.93.10/24 dev eth1

ip add a 192.168.93.20/24 dev eth1

ip add a 192.168.93.30/24 dev eth1

4.編輯配置文件:

vim /etc/httpd/conf.d/test.conf

技術分享

5.啟動httpd服務:

service httpd start

6.測試:(統一說明下,測試都是在centos7上做的,包括以下兩個實驗。)

curl http://192.168.93.10

curl http://192.168.93.20

curl http://192.168.93.30

技術分享

註:建立虛擬主機時,主站點可以關閉。每個站點占用一個

IP地址,通常使用IP地址訪問站點的用戶很少;或是配合DNS服務器上使用:

如:vim /etc/hosts

技術分享

實現通過名字訪問站點:如圖所示

技術分享

二、基於端口的實現

1.編輯配置文件,添加三個端口:

vim /etc/httpd/conf.d/test.conf

技術分享

2.重啟網絡服務:

service network restart

3.重啟httpd服務:

service httpd restart

(註:這裏可能會出現httpd服務起不來,查看是不是因為selinux的設置,如果是,進入配置文件修改設置,設置完重啟。或者通過命令臨時修改:setenforce 0

技術分享


技術分享

4.測試:

技術分享

三、基於FQDN的實現

1.在hosts文件中添加站點:

vim /etc/hosts

技術分享

2.編輯配置文件:

vim /etc/httpd/conf.d/test.conf

技術分享

3.啟動服務:service httpd restart(或使用命令!ser)

4.測試:

技術分享

centos7與centos6大致相同,只是必須對目錄授權,且不需要NameVirutalHost指令。

註:一般虛擬機不要與main主機混用;因此,要使用虛擬主機,一般先禁用main主機。禁用方法:註釋中心主機的DocumentRoot指令即可

以上就是虛擬主機的實現了,歡迎多多指教。



本文出自 “Mylog” 博客,請務必保留此出處http://600600880.blog.51cto.com/13303875/1977037

centos6上虛擬主機的實現