apache 基本vhost配置

經常使用Apache虛擬主機進行開發和測試,但每次需要配置虛擬主機時都習慣性的ctrl+c和ctrl+v,這次由於重裝系統,需要配置一個新的PHP開發環境虛擬主機,於是總結一下Apaceh配置httpd-vhosts虛擬主機使用方法和步驟,便於查找和使用。

開發環境:WAMP
網址:http://www.wampserver.com/en/

實例一,Apaceh配置localhost虛擬主機步驟
1,用記事本打開apache目錄下httpd文件(如:D:\wamp\bin\apache\apache2.2.8\conf),找到如下模塊

  1. # Virtual hosts
  2. #Include conf/extra/httpd-vhosts.conf

去掉前面的#,這樣就開啓了httpd-vhosts虛擬主機文件。這時候重啓wamp環境,無法打開localhost,需要在httpd-vhosts.conf配置一下。

2,用記事本打開httpd-vhosts文件,配置好localhost虛擬主機,參照httpd-vhosts文件中實例,修改成如下:

  1. <VirtualHost *:80>
  2. ServerAdmin [email protected]
  3. DocumentRoot "D:\wamp\www"
  4. ServerName localhost
  5. ServerAlias localhost
  6. ErrorLog "logs/dummy-host.localhost-error.log"
  7. CustomLog "logs/dummy-host.localhost-access.log" common
  8. </VirtualHost>

修改配置如下:
DocumentRoot 修改爲本地wamp環境下的www目錄(如:D:\wamp\www)
ServerName改爲localhost

3,重啓Apache,發現localhost可以正常打開,配置localhost比較簡單。

實例二,Apaceh配置test.biuuu.com虛擬主機步驟

1,方法同上,複製配置代碼修改如下:

  1. <VirtualHost *:80>
  2. ServerAdmin [email protected]
  3. DocumentRoot E:\WebRoot\biuuu
  4. ServerName test.biuuu.com
  5. ErrorLog "logs/dummy-host2.localhost-error.log"
  6. CustomLog "logs/dummy-host2.localhost-access.log" common
  7. </VirtualHost>

2,打開host文件(C:\WINDOWS\system32\drivers\etc\hosts),增加一行代碼

  1. 127.0.0.1       test.biuuu.com

3,在瀏覽器中打開test.biuuu.com,發現如下錯誤403 Forbidden錯誤
Forbidden
You don't have permission to access / on this server.

分析:這主要是目錄訪問權限沒有設置,需要設置對目錄的訪問權!

4,打開httpd文件,找到如下語句

  1. <Directory />
  2. Options FollowSymLinks
  3. AllowOverride None
  4. Order deny,allow
  5. Deny from all
  6. </Directory>

複製以上代碼,並進行目錄修改,把/替換爲E:\WebRoot\biuuu,修改virtualHost代碼如下

  1. <VirtualHost *:80>
  2. ServerAdmin [email protected]
  3. DocumentRoot E:\WebRoot\biuuu
  4. ServerName test.biuuu.com
  5. ErrorLog "logs/dummy-host2.localhost-error.log"
  6. CustomLog "logs/dummy-host2.localhost-access.log" common
  7. <Directory E:\WebRoot\biuuu>
  8. Options FollowSymLinks
  9. AllowOverride None
  10. Order deny,allow
  11. Deny from all
  12. </Directory>
  13. </VirtualHost>

在瀏覽器中測試發現還是打不開,提示如上403 Forbidden錯誤,修改其中的Deny from all爲allow from all

5,重啓Apache,虛擬主機配置成功!

注意事項
1,目錄路徑,如E:\WebRoot\biuuu
2,訪問權限,如上Deny from all修改爲allow from all
3,host文件,配置虛擬域名host指向
4,httpd文件,打開Include conf/extra/httpd-vhosts.conf模塊
5,httpd-vhosts文件,配置虛擬主機

使用Apaceh配置httpd-vhosts虛擬主機對於開發人員來說比較簡單,但卻非常重要,僅供參考!

參考資料:

http://httpd.apache.org/docs/2.2/vhosts/

http://httpd.apache.org/docs/2.0/vhosts/examples.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章