PHP中級篇 Apache配置httpd-vhosts虛擬主機總結及注意事項


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

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

實例一,Apaceh配置localhost虛擬主機步驟
1,用記事本打開apache目錄下httpd.conf文件(如:D:\wamp\bin\apache\apache2.2.8\httpd.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 webmaster@dummy-host.localhost

  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 test@biuuu.com

  3. DocumentRoot E:\ProjectRoot\

  4. ServerName test.sallency.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.sallency.com

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

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

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

  1. <Directory />

  2. Options FollowSymLinks

  3. AllowOverride All

  4. Order deny,allow

  5. Deny from all

  6. </Directory>

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

  1. <VirtualHost *:80>

  2. ServerAdmin test@biuuu.com

  3. DocumentRoot E:\ProjectRoot\

  4. ServerName test.sallency.com

  5. ErrorLog "logs/dummy-host2.localhost-error.log"

  6. CustomLog "logs/dummy-host2.localhost-access.log" common

  7.  

  8. <Directory E:\ProjectRoot\>

  9. Options FollowSymLinks

  10. AllowOverride All

  11. Order deny,allow

  12. Deny from all

  13. </Directory>

  14.  

  15. </VirtualHost>

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

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

注意事項
1,目錄路徑,如E:\ProjectRoot\

2,訪問權限,如上Deny from all修改為allow from all

3,host文件,配置虛擬域名host指向
4,httpd文件,打開Include conf/extra/httpd-vhosts.conf模塊
5,httpd-vhosts文件,配置虛擬主機

6,還有可能是linux 的 selinux 防火牆導致這個原因,切記切記

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

參考資料:

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

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

顧銀鑫 注:如發生Fatal error: Allowed memory size of 8388608 bytes exhausted錯誤

修改php.ini設置memory_limit = 12M(默認8M)

或只需要在你的程序頭部加入: ini_set("memory_limit","12M");

原文:http://blog.163.com/lgh_2002/blog/static/44017526201182514650248/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM