萬盛學電腦網

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

Apache 修改預設站點目錄與虛擬主機配置完整教學

修改預設 DocumentRoot

  1. 找到 `httpd.conf`(通常在 `conf/` 目錄)。
  2. 搜尋 DocumentRoot,修改路徑,例如:
    DocumentRoot "D:/MyWebSite"
  3. 同步修改對應的 `` 區塊:
    <Directory "D:/MyWebSite">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
  4. 重啟 Apache:`httpd -k restart` 或服務管理器重啟。

新增虛擬主機(多站點)

在 `httpd-vhosts.conf` 或 `httpd.conf` 末尾添加:

<VirtualHost *:80>
    ServerName site1.local
    DocumentRoot "D:/Web/site1"
    <Directory "D:/Web/site1">
        AllowOverride All
        Require all granted
    </Directory>
    ErrorLog "logs/site1-error.log"
    CustomLog "logs/site1-access.log" common
</VirtualHost>

<VirtualHost *:80>
    ServerName site2.local
    DocumentRoot "D:/Web/site2"
    <Directory "D:/Web/site2">
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

啟用虛擬主機配置

確保 `httpd.conf` 中取消註釋:

Include conf/extra/httpd-vhosts.conf

Windows Hosts 綁定

編輯 `C:\Windows\System32\drivers\etc\hosts`:

127.0.0.1  site1.local
127.0.0.1  site2.local

常見問題排查

  • 403 Forbidden:檢查 `Require all granted` 及目錄權限(ICU/IIS_USRS 讀取權限)。
  • 路徑分隔符:Windows 建議使用正斜線 `/` 或雙反斜線 `\\`。
  • 配置語法檢查:`httpd -t` 驗證配置無誤再重啟。
copyright © 萬盛學電腦網 all rights reserved