首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

WAMP 環境配置 Apache 虛擬主機完整步驟

     編輯:PHP 相關知識

在 WAMP (Windows Apache MySQL PHP) 環境下建立虛擬主機,需修改三個關鍵位置:Hosts 檔案、httpd-vhosts.conf、httpd.conf。本文提供標準化操作流程。

步驟 1:修改 Hosts 檔案

# 路徑:C:\Windows\System32\drivers\etc\hosts
127.0.0.1    dz1.test.com

步驟 2:編輯 httpd-vhosts.conf

# 路徑: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>

步驟 3:編輯 httpd.conf 啟用虛擬主機並授權目錄

  1. 取消註釋:Include conf/extra/httpd-vhosts.conf(去掉前面的 #)。
  2. 添加目錄授權(路徑必須與 DocumentRoot 完全一致):
    <Directory "e:/wamp/www/dz1">
        Options Indexes FollowSymLinks
        AllowOverride all
        Order Allow,Deny
        Allow from all
    </Directory>

步驟 4:重啟 Apache 驗證

  • WAMP 面板重啟所有服務,或命令列 httpd -k restart。
  • 瀏覽器訪問 https://dz1.test.com 應正常顯示站點內容。

常見錯誤與排查

多站點管理建議

  • 每個站點獨立 VirtualHost 區塊,DocumentRoot 指向不同目錄。
  • 使用 ServerAlias 支援多域名(如 www.example.com、example.com)。
  • 生產環境建議啟用 mod_rewrite、mod_ssl、mod_expires 等模組優化效能與安全。

遵循標準化三步驟,即可在 WAMP 輕鬆管理多虛擬主機開發環境。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
現象原因解決
403 Forbidden缺少 <Directory> 授權或路徑不匹配核對 DocumentRoot 與 Directory 路徑大小寫、斜線方向
localhost 打不開未保留 localhost VirtualHost按步驟 2 添加 localhost 虛擬主機
修改不生效未重啟 Apache 或編輯錯誤檔案確認編輯的是 WAMP 實際使用的 conf 檔案