1. 程式人生 > 實用技巧 >Apaceh 多虛擬主機多站點配置兩種方案

Apaceh 多虛擬主機多站點配置兩種方案

>>> hot3.png

Apache中配置多主機多站點,可以通過兩種方式實現:

  • 將同一個域名的不同埠對映到不同的虛擬主機,不同埠對映到不同的站點;
  • 將同一個埠對映成不同的域名,不同的域名對映到不同的站點。

我們只需要修改相應的配置檔案即可。

一、準備工作

1、修改系統 hosts檔案

我是在我自己的電腦上操作的,我的系統是windows-server 2012,所以先修改系統的hosts檔案,增加兩個域名對映,以便後面備用。hosts檔案的路徑是:C:/Windows/System32/drivers/etc/hosts,在這個檔案中我們加入如下兩行程式碼:

[plain]view plain

copy

  1. 127.0.0.1www.syp.com
  2. 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

copy

  1. #Virtualhosts
  2. #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

  1. #配置我們自己的虛擬主機
  2. <VirtualHostwww.lyz.com:8080>
  3. #配置訪問跟目錄
  4. DocumentRoot"d:/Apache/myweb1"
  5. #這裡配置歡迎首頁面
  6. DirectoryIndexindex.htmlindex.htmindex.php
  7. <Directory/>
  8. OptionsFollowSymLinks
  9. #不允許別人修改我們的頁面
  10. AllowOverrideNone
  11. #設定訪問許可權
  12. Orderallow,deny
  13. AllowfromAll
  14. </Directory>
  15. </VirtualHost>
  16. #配置我們自己的虛擬主機
  17. <VirtualHostwww.lyz.com:8090>
  18. #配置訪問跟目錄
  19. DocumentRoot"d:/Apache/myweb2"
  20. #這裡配置歡迎首頁面
  21. DirectoryIndexindex.htmlindex.htmindex.php
  22. <Directory/>
  23. OptionsFollowSymLinks
  24. #不允許別人修改我們的頁面
  25. AllowOverrideNone
  26. #設定訪問許可權
  27. Orderallow,deny
  28. AllowfromAll
  29. </Directory>
  30. </VirtualHost>

具體如下圖所示:

啟動Apache在瀏覽器中輸入:http://www.syp.com:8080顯示"我來自地球,我是第一個勇士",輸入:http://www.songyp.com:8090顯示"我來自火星,我是第二個火娃"。

方案二、

將同一個埠對映成不同的域名,不同的域名對映到不同的站點。

在http 資料夾內 httpd-vhosts.conf檔案中新增如下程式碼:

[plain]view plaincopy

  1. #配置我們自己的虛擬主機
  2. <VirtualHost*:8080>
  3. #配置訪問跟目錄
  4. DocumentRoot"d:/Apache/myweb1"
  5. ServerNamewww.lyz.com
  6. #這裡配置歡迎首頁面
  7. DirectoryIndexnews.htmlindex.htmlindex.htmindex.php
  8. <Directory/>
  9. OptionsFollowSymLinks
  10. #不允許別人修改我們的頁面
  11. AllowOverrideNone
  12. #設定訪問許可權
  13. Orderallow,deny
  14. Allowfromall
  15. </Directory>
  16. </VirtualHost>
  17. #配置我們自己的虛擬主機
  18. <VirtualHost*:8080>
  19. #配置訪問跟目錄
  20. DocumentRoot"d:/Apache/myweb2"
  21. ServerNamewww.liuyazhuang.com
  22. #這裡配置歡迎首頁面
  23. DirectoryIndexnews.htmlindex.htmlindex.htmindex.php
  24. <Directory/>
  25. OptionsFollowSymLinks
  26. #不允許別人修改我們的頁面
  27. AllowOverrideNone
  28. #設定訪問許可權
  29. Orderallow,deny
  30. Allowfromall
  31. </Directory>
  32. </VirtualHost>

具體如下圖所示:

啟動Apache在瀏覽器中輸入:http://www.syp.com:8080顯示"我來自地球,我是第一個勇士",輸入:http://www.songyp.com:8080顯示"我來自火星,我是第二個火娃"。

同理:將index.html 文件替換為相對應的專案 依舊沒問題 本人親測無誤!!!

轉載於:https://my.oschina.net/songyp/blog/778406