Apaceh 多虛擬主機多站點配置兩種方案
Apache中配置多主機多站點,可以通過兩種方式實現:
- 將同一個域名的不同埠對映到不同的虛擬主機,不同埠對映到不同的站點;
- 將同一個埠對映成不同的域名,不同的域名對映到不同的站點。
我們只需要修改相應的配置檔案即可。
一、準備工作
1、修改系統 hosts檔案
我是在我自己的電腦上操作的,我的系統是windows-server 2012,所以先修改系統的hosts檔案,增加兩個域名對映,以便後面備用。hosts檔案的路徑是:C:/Windows/System32/drivers/etc/hosts,在這個檔案中我們加入如下兩行程式碼:
[plain]view plain
- 127.0.0.1www.syp.com
- 127.0.0.1www.songyp.com
這兩行程式碼的意思是將www.syp.com和www.songyp.com對映到本機的ip地址上。
2、準備兩個站點
在d:/Apache目錄下分別準備兩個站點myweb1和myweb2,這兩個站點下分別有一個index.html檔案,myweb1下index.html內容是"我來自地球,我是第一個勇士",myweb2站點下的index.html內容是"我來自火星,我是第二個火娃"。
具體如下圖所示:
好了,到此,站點準備完畢
二、具體實現
我們分別講解兩種不同的實現方案
1、修改Apache的httpd.conf檔案
1)修改監聽埠
在Apache的httpd.conf中,將監聽埠修改為8080和8090,具體如下圖所示:
2)註釋預設的訪問路徑
在httpd.conf檔案中找到如下程式碼:
[plain]view plaincopy
DocumentRoot"D:/ProgramFiles(x86)/ApacheSoftwareFoundation/Apache2.2/htdocs"
將這行程式碼註釋掉,如下圖:
3)開啟引入httpd-vhosts.conf檔案的註釋
在httpd.conf檔案中找到如下程式碼:
[plain]view plain
- #Virtualhosts
- #Includeconf/extra/httpd-vhosts.conf
將#Include conf/extra/httpd-vhosts.conf這行程式碼的註釋開啟,如下圖:
2、修改Apache的httpd-vhosts.conf檔案
我們說到的方案一和方案二,只是在配置httpd-vhosts.conf檔案的時候有所不同。
方案一、
將同一個域名的不同埠對映到不同的虛擬主機,不同埠對映到不同的站點
在httpd-vhosts.conf檔案中新增如下程式碼:
[plain]view plaincopy
- #配置我們自己的虛擬主機
- <VirtualHostwww.lyz.com:8080>
- #配置訪問跟目錄
- DocumentRoot"d:/Apache/myweb1"
- #這裡配置歡迎首頁面
- DirectoryIndexindex.htmlindex.htmindex.php
- <Directory/>
- OptionsFollowSymLinks
- #不允許別人修改我們的頁面
- AllowOverrideNone
- #設定訪問許可權
- Orderallow,deny
- AllowfromAll
- </Directory>
- </VirtualHost>
- #配置我們自己的虛擬主機
- <VirtualHostwww.lyz.com:8090>
- #配置訪問跟目錄
- DocumentRoot"d:/Apache/myweb2"
- #這裡配置歡迎首頁面
- DirectoryIndexindex.htmlindex.htmindex.php
- <Directory/>
- OptionsFollowSymLinks
- #不允許別人修改我們的頁面
- AllowOverrideNone
- #設定訪問許可權
- Orderallow,deny
- AllowfromAll
- </Directory>
- </VirtualHost>
具體如下圖所示:
啟動Apache在瀏覽器中輸入:http://www.syp.com:8080顯示"我來自地球,我是第一個勇士",輸入:http://www.songyp.com:8090顯示"我來自火星,我是第二個火娃"。
方案二、
將同一個埠對映成不同的域名,不同的域名對映到不同的站點。
在http 資料夾內 httpd-vhosts.conf檔案中新增如下程式碼:
[plain]view plaincopy
- #配置我們自己的虛擬主機
- <VirtualHost*:8080>
- #配置訪問跟目錄
- DocumentRoot"d:/Apache/myweb1"
- ServerNamewww.lyz.com
- #這裡配置歡迎首頁面
- DirectoryIndexnews.htmlindex.htmlindex.htmindex.php
- <Directory/>
- OptionsFollowSymLinks
- #不允許別人修改我們的頁面
- AllowOverrideNone
- #設定訪問許可權
- Orderallow,deny
- Allowfromall
- </Directory>
- </VirtualHost>
- #配置我們自己的虛擬主機
- <VirtualHost*:8080>
- #配置訪問跟目錄
- DocumentRoot"d:/Apache/myweb2"
- ServerNamewww.liuyazhuang.com
- #這裡配置歡迎首頁面
- DirectoryIndexnews.htmlindex.htmlindex.htmindex.php
- <Directory/>
- OptionsFollowSymLinks
- #不允許別人修改我們的頁面
- AllowOverrideNone
- #設定訪問許可權
- Orderallow,deny
- Allowfromall
- </Directory>
- </VirtualHost>
具體如下圖所示:
啟動Apache在瀏覽器中輸入:http://www.syp.com:8080顯示"我來自地球,我是第一個勇士",輸入:http://www.songyp.com:8080顯示"我來自火星,我是第二個火娃"。
同理:將index.html 文件替換為相對應的專案 依舊沒問題 本人親測無誤!!!
轉載於:https://my.oschina.net/songyp/blog/778406