在 WAMP (Windows Apache MySQL PHP) 環境下建立虛擬主機,需修改三個關鍵位置:Hosts 檔案、httpd-vhosts.conf、httpd.conf。本文提供標準化操作流程。
# 路徑:C:\Windows\System32\drivers\etc\hosts
127.0.0.1 dz1.test.com
# 路徑:wamp\bin\apache\Apache2.2.11\conf\extra\httpd-vhosts.conf
<VirtualHost *:80>
ServerName dz1.test.com
ServerAlias dz1.test.com
DocumentRoot "e:/wamp/www/dz1"
</VirtualHost>
# 重要:保留 localhost 虛擬主機,否則訪問 localhost 會 403
<VirtualHost *:80>
ServerName localhost
ServerAlias localhost
DocumentRoot "D:/wamp/www"
</VirtualHost>
Include conf/extra/httpd-vhosts.conf(去掉前面的 #)。<Directory "e:/wamp/www/dz1">
Options Indexes FollowSymLinks
AllowOverride all
Order Allow,Deny
Allow from all
</Directory>
httpd -k restart。https://dz1.test.com 應正常顯示站點內容。| 現象 | 原因 | 解決 |
|---|---|---|
| 403 Forbidden | 缺少 <Directory> 授權或路徑不匹配 | 核對 DocumentRoot 與 Directory 路徑大小寫、斜線方向 |
| localhost 打不開 | 未保留 localhost VirtualHost | 按步驟 2 添加 localhost 虛擬主機 |
| 修改不生效 | 未重啟 Apache 或編輯錯誤檔案 | 確認編輯的是 WAMP 實際使用的 conf 檔案 |