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

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

     編輯:PHP 相關知識

Apache 安裝後預設文件根目錄通常位於 htdocs,實際部署時常需調整至專案目錄或設定多站點虛擬主機。本文詳細說明修改預設目錄步驟、虛擬主機配置、權限設定與常見錯誤排查,適用於 Windows/Linux 環境。

一、修改預設站點目錄(單站點模式)

1. 找到主配置檔

  • Windows (WAMP/XAMPP):apache/conf/httpd.conf 或 apache2/conf/httpd.conf
  • Linux (apt/yum 安裝):/etc/apache2/httpd.conf 或 /etc/httpd/conf/httpd.conf
  • macOS 內建:/etc/apache2/httpd.conf

2. 修改 DocumentRoot 與 Directory

搜尋 DocumentRoot(約第 250 行),修改路徑:

# 原始
DocumentRoot "C:/wamp/bin/apache/Apache2.2.21/htdocs"

# 修改為你的專案目錄(使用正斜線,雙引號不可少)
DocumentRoot "D:/MyProjects/PublicWeb"

緊接著搜尋 <Directory "...">(約第 280 行),同步修改:

# 原始
<Directory "C:/wamp/bin/apache/Apache2.2.21/htdocs">

# 修改
<Directory "D:/MyProjects/PublicWeb">

3. 目錄權限指令(關鍵)

在同一 <Directory> 區塊內,確保包含:

<Directory "D:/MyProjects/PublicWeb">
    Options Indexes FollowSymLinks
    AllowOverride All          # 允許 .htaccess 覆寫(需模組 mod_rewrite)
    Require all granted        # Apache 2.4+ 語法,允許所有存取
</Directory>

Apache 2.2 舊語法: Order allow,deny / Allow from all

4. 重啟 Apache 生效

# Windows (管理員 CMD)
httpd -k restart

# Linux systemd
sudo systemctl restart apache2   # Ubuntu/Debian
sudo systemctl restart httpd     # CentOS/RHEL

二、虛擬主機配置(多站點推薦做法)

修改預設目錄僅適合單一站點。生產環境強烈建議使用 虛擬主機,隔離專案、綁定域名、獨立日誌。

1. 啟用虛擬主機模組

在 httpd.conf 確認以下行未被註解:

LoadModule vhost_alias_module modules/mod_vhost_alias.so
# 包含獨立虛擬主機配置檔(推薦)
Include conf/extra/httpd-vhosts.conf

2. 編輯 httpd-vhosts.conf

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    DocumentRoot "D:/MyProjects/Example/Public"
    <Directory "D:/MyProjects/Example/Public">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    ErrorLog "logs/example-error.log"
    CustomLog "logs/example-access.log" common
</VirtualHost>

<VirtualHost *:80>
    ServerName api.example.com
    DocumentRoot "D:/MyProjects/Api/Public"
    # ... 同樣的 Directory 設定
</VirtualHost>

3. 本機測試需修改 hosts 檔

# Windows: C:\Windows\System32\drivers\etc\hosts
# Linux/macOS: /etc/hosts
127.0.0.1   www.example.com
127.0.0.1   api.example.com

三、常見錯誤與排查

現象可能原因解決方法
403 ForbiddenDirectory 區塊缺少 Require all granted / 權限不足檢查 Directory 設定、資料夾權限 755、檔案 644、SELinux contexts
404 Not FoundDocumentRoot 路徑錯誤、拼寫大小寫(Linux 區分大小寫)用實際路徑測試 ls -la /path,確認 Apache 進程使用者可讀取
.htaccess 不生效AllowOverride None / 未載入 mod_rewrite設定 AllowOverride All、確認 LoadModule rewrite_module
重啟失敗語法錯誤、端口佔用執行 apachectl -t 或 httpd -t 檢查語法;netstat -an | grep :80 檢查端口

四、Windows 路徑與權限特別提醒

  • 路徑統一使用正斜線 / 或雙反斜線 \\,避免單反斜線被視為跳脫字元。
  • Apache 服務通常以 SYSTEM 或指定使用者執行,確保該使用者對目標資料夾有「讀取/執行」權限。
  • 若專案在網路磁碟(映射磁碟機),服務使用者可能無法存取,建議改用 UNC 路徑 //server/share 並設定服務登入身分。

五、安全加固建議

  1. 隱藏版本資訊:ServerTokens Prod、ServerSignature Off
  2. 禁用目錄瀏覽:Options -Indexes(生產環境)
  3. 限制 .htaccess 可覆寫指令:AllowOverride AuthConfig FileInfo Limit 按需開放
  4. 啟用 HTTPS:配置 SSL 憑證、強制導向、HSTS

掌握 Apache 目錄與虛擬主機配置,是佈署 PHP/靜態站點的基礎功力。建議採用「一站一虛擬主機、獨立日誌、獨立目錄」的規範結構,便於維護、擴展與故障隔離。

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