常用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),找到以下模塊php
#Include conf/extra/httpd-vhosts.conf 去掉前面的#,這樣就開啓了httpd-vhosts虛擬主機文件。這時候重啓wamp環境,沒法打開localhost,須要在httpd-vhosts.conf配置一下。 2,用記事本打開httpd-vhosts文件,配置好localhost虛擬主機,參照httpd-vhosts文件中實例,修改爲以下: <VirtualHost *:80> ServerAdmin webmaster@dummy-host.localhost DocumentRoot "D:\wamp\www" ServerName localhost ServerAlias localhost ErrorLog "logs/dummy-host.localhost-error.log" CustomLog "logs/dummy-host.localhost-access.log" common </VirtualHost> 修改配置以下: DocumentRoot 修改成本地wamp環境下的www目錄(如:D:\wamp\www) ServerName改成localhost 3,重啓Apache,發現localhost能夠正常打開,配置localhost比較簡單。 實例二,Apaceh配置test.biuuu.com虛擬主機步驟 1,方法同上,複製配置代碼修改以下: <VirtualHost *:80> ServerAdmin test@biuuu.com DocumentRoot E:\WebRoot\biuuu ServerName test.biuuu.com ErrorLog "logs/dummy-host2.localhost-error.log" CustomLog "logs/dummy-host2.localhost-access.log" common </VirtualHost> 2,打開host文件(C:\WINDOWS\system32\drivers\etc\hosts),增長一行代碼 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文件,找到以下語句 <Directory /> Options FollowSymLinks AllowOverride None Order deny,allow Deny from all </Directory> 複製以上代碼,並進行目錄修改,把/替換爲E:\WebRoot\biuuu,修改virtualHost代碼以下 <VirtualHost *:80> ServerAdmin test@biuuu.com DocumentRoot E:\WebRoot\biuuu ServerName test.biuuu.com ErrorLog "logs/dummy-host2.localhost-error.log" CustomLog "logs/dummy-host2.localhost-access.log" common <Directory E:\WebRoot\biuuu> Options FollowSymLinks AllowOverride None Order deny,allow Deny from all </Directory> </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 原創文章如轉載,請註明:轉載自 必優博客 http://www.biuuu.com/ 顧銀鑫 注:如發生Fatal error: Allowed memory size of 8388608 bytes exhausted錯誤 修改php.ini設置memory_limit = 12M(默認8M) 或只須要在你的程序頭部加入: ini_set("memory_limit","12M");html