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

Windows Apache 配置虛擬主機完整教程:多域名多項目共存

     編輯:PHP 相關知識

Windows Apache 配置虛擬主機完整教程:多域名多項目共存

在 Windows 開發環境配置虛擬主機,可實現多專案獨立域名訪問 (如 `project1.local`、`api.local`),避免端口衝突與路徑汙染。本文以 Apache 2.4 為例,步驟清晰可直接落地。

第一步:啟用虛擬主機配置文件

  1. 編輯 `httpd.conf` (通常在 `Apache24/conf/`):
# 找到並取消註釋 (去掉 #)
Include conf/extra/httpd-vhosts.conf

第二步:編寫虛擬主機規則

  1. 編輯 `httpd-vhosts.conf` (在 `conf/extra/`),新增:
<VirtualHost *:80>
    ServerName www.mydemo.com
    DocumentRoot "D:/mydemo"
    <Directory "D:/mydemo">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Require all granted
    </Directory>
    # 可選:錯誤/存取日誌分離
    ErrorLog "logs/mydemo-error.log"
    CustomLog "logs/mydemo-access.log" common
</VirtualHost>

關鍵指令說明

  • `ServerName`:匹配的域名
  • `DocumentRoot`:網站根目錄 (使用正斜槓或雙反斜槓)
  • `AllowOverride All`:允許 `.htaccess` 覆蓋 (Laravel/TP/ThinkPHP 必須)
  • `Require all granted`:Apache 2.4 授權語法 (舊版為 `Order allow,deny / Allow from all`)

第三步:配置本地 hosts 解析

  1. 以**管理員身份**開啟記事本,編輯 `C:\Windows\System32\drivers\etc\hosts`:
127.0.0.1   www.mydemo.com
127.0.0.1   api.mydemo.com
::1         www.mydemo.com  # IPv6 兼容

第四步:建立目錄與測試檔案

mkdir D:\mydemo
echo "<?php phpinfo(); ?>" > D:\mydemo\index.php

第五步:重啟 Apache 驗證

  1. 命令列 (管理員) 執行:`httpd -k restart`
  2. 或使用 `ApacheMonitor.exe` 圖形介面重啟
  3. 瀏覽器訪問 `https://www.mydemo.com` 應顯示 phpinfo 頁面

常見問題排查

現象原因解決
403 Forbidden目錄權限不足檢查 `` 允許存取、Windows 資料夾權限給 Users 讀取
404 Not FoundDocumentRoot 路徑錯誤確認路徑存在、斜槓方向、大小寫
域名跳轉到預設站點ServerName 不匹配核對 hosts、ServerName 拼寫、清除瀏覽器快取
.htaccess 不生效AllowOverride None改為 `AllowOverride All` 並重啟
端口 80 被佔用System/IIS/Skype 佔用`netstat -ano | findstr :80` 找 PID 終止,或改 Apache 端口

進階:多站點統一管理技巧

1. 通配子域名 (泛域名)

<VirtualHost *:80>
    ServerName mydemo.com
    ServerAlias *.mydemo.com
    VirtualDocumentRoot "D:/www/mydemo/%1"  # 需 mod_vhost_alias
</VirtualHost>

新增資料夾 `D:/www/mydemo/api` 即可自動對應 `api.mydemo.com`。

2. 專案獨立 PHP 版本 (配合 PHP-FPM/多 PHP 版本)

<VirtualHost *:80>
    ServerName legacy.local
    DocumentRoot "D:/legacy"
    # 指定 PHP 5.6 模組
    <FilesMatch \.php$>
        SetHandler "proxy:fcgi://127.0.0.1:9056"
    </FilesMatch>
</VirtualHost>

<VirtualHost *:80>
    ServerName modern.local
    DocumentRoot "D:/modern"
    # 指定 PHP 8.2 模組
    <FilesMatch \.php$>
        SetHandler "proxy:fcgi://127.0.0.1:9082"
    </FilesMatch>
</VirtualHost>

3. SSL 自簽憑證 (本地 HTTPS 測試)

<VirtualHost *:443>
    ServerName secure.local
    DocumentRoot "D:/secure"
    SSLEngine on
    SSLCertificateFile "conf/ssl/secure.local.crt"
    SSLCertificateKeyFile "conf/ssl/secure.local.key"
</VirtualHost>

生成自簽憑證:`openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout secure.local.key -out secure.local.crt`

開發環境最佳實踐清單

  • ✅ 使用 `.local` / `.test` / `.dev` 後綴 (避免與公網域名衝突)
  • ✅ 專案根目錄統一規範:`D:/www/{project}/public`
  • ✅ 日誌分離:`ErrorLog` / `CustomLog` 便於排查
  • ✅ `AllowOverride All` 支援框架路由重寫
  • ✅ 定期備份 `httpd-vhosts.conf` 與 `hosts` 文件

提示:Windows 修改 hosts 需管理員權限,建議使用 HostsFileEditor 等圖形工具簡化操作。生產環境請使用 Nginx 反向代理 + 域名解析,勿直接用 Apache 虛擬主機對外服務。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved