萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

Windows 上使用 WAMP 搭建 PHP 本地開發環境完整圖文教學

WAMP (Windows + Apache + MySQL + PHP) 一鍵安裝包是 Windows 平台最快上手的 PHP 開發環境。本文以 WampServer 3.0.6 64-bit 為例,涵蓋安裝、專案目錄、虛擬主機、局域網存取、常見錯誤排解。

1. 下載與安裝

  • 官網:wampserver.com (SourceForge 鏡像較快)。
  • 選擇 **64-bit** 版本 (含 Apache 2.4.23, PHP 5.6.25/7.0.10, MySQL 5.7.14, phpMyAdmin 4.6.4)。
  • 安裝路徑建議:D:\wamp64 (避免 C 盤空間不足、權限問題)。
  • 常見錯誤:缺少 msvcr110.dll → 先安裝 Visual C++ 2012 Redistributable (vcredist_x64.exe)。

2. 專案目錄與預設根目錄

  • 預設 Web 根目錄:D:\wamp64\www。
  • 在此建立資料夾即為專案,如 D:\wamp64\www\myproject,瀏覽器訪問 https://localhost/myproject/。

3. 修改 Apache 根目錄 (不想用 www)

  1. 編輯 D:\wamp64\bin\apache\apache2.4.23\conf\httpd.conf:
    DocumentRoot "D:/wamp64/www" → 改為你的目錄 (正斜線)。
    同步修改後方 <Directory "D:/wamp64/www"> 路徑。
  2. 編輯 D:\wamp64\bin\apache\apache2.4.23\conf\extra\httpd-vhosts.conf:
    將 DocumentRoot "D:/wamp64/www" 同步修改。
  3. **重啟所有服務** (WAMP 圖示左鍵 → Restart All Services)。

4. 測試 PHP 環境

<?php
// D:\wamp64\www\test\test.php
echo 'Hello World!';

瀏覽器開啟 https://localhost/test/test.php,顯示 Hello World! 即成功。

5. 設定虛擬主機 (自訂域名,如 www.test.local)

  1. 編輯 httpd-vhosts.conf,末尾新增:
    <VirtualHost *:80>
        ServerName www.test.local
        ServerAlias test.local
        DocumentRoot "D:/wamp64/www/test"
        <Directory "D:/wamp64/www/test">
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
            DirectoryIndex index.php index.html
        </Directory>
    </VirtualHost>
  2. 編輯 Windows Hosts 檔:
    C:\Windows\System32\drivers\etc\hosts (需管理員權限編輯)
    新增:127.0.0.1 www.test.local
  3. 重啟 Apache,瀏覽器即可用 https://www.test.local/ 存取。

6. 開啟區域網路存取 (手機實測、同事協作)

  1. 編輯 httpd.conf,找到 <Directory "${INSTALL_DIR}/www"> 區塊:
    AllowOverride all
    Require all granted (Apache 2.4 語法,舊版為 Order allow,deny / Allow from all)
  2. 同步修改 httpd-vhosts.conf 中對應 <Directory> 區塊。
  3. Windows 防火牆開放 **TCP 80** 輸入規則 (或暫時關閉防火牆測試)。
  4. 手機/同事電腦瀏覽器輸入 https://你的區網IP/ 或 https://你的區網IP/專案資料夾/。

7. 切換 PHP 版本

WAMP 圖示左鍵 → PHP → Version → 選擇 5.6.25 或 7.0.10 (需重啟 Apache)。

8. 常見問題快速排查

現象可能原因解決
WAMP 圖示持續橙色/紅色Apache/MySQL 未啟動檢查 80/3306 埠被佔用 (Skype、IIS、VMware) → netstat -ano | findstr :80 → 終止佔用進程或修改 Apache 埠
Forbidden 403Require 設定錯誤確認 httpd.conf/vhosts.conf Directory 區塊有 Require all granted
PHP 錯誤不顯示php.ini 設定開啟 display_errors=On, error_reporting=E_ALL
MySQL 連線失敗密碼/權限phpMyAdmin 根帳號預設無密碼,建議設定 root 密碼並建立專用帳號

完成上述設定,您即擁有一個功能完整、支援虛擬主機、可區網存取的 PHP 本地開發環境,可專注於程式碼撰寫而非環境折騰。

copyright © 萬盛學電腦網 all rights reserved