Apache 1.3 (Red Hat 5.2 預設) 採用三檔分離架構,位於 /etc/httpd/conf/:
現代演進:Apache 2.0+ 已合併至單一 httpd.conf,透過 Include 引入模組化配置,但理解三檔架構有助於掌握核心概念。
| 指令 | 功能 | 預設值 (Red Hat 5.2) | 說明 |
|---|---|---|---|
DocumentRoot | 網站文檔根目錄 (絕對路徑) | /home/httpd/html | 所有相對路徑以此為基準;目錄必須存在 |
UserDir | 使用者個人網站目錄名 | public_html | 啟用後 ~username/ 對應 /home/username/public_html/;需配合 mod_userdir |
DirectoryIndex | 目錄預設首頁檔案列表 | index.html | 支援多檔案降序嘗試:index.html index.php index.cgi |
ScriptAlias | CGI 腳本目錄 URL 映射 | /cgi-bin/ → /home/httpd/cgi-bin/ | 映射目錄下所有檔案視為可執行腳本,非靜態內容 |
AddType | 副檔名 → MIME 類型映射 | 內建數百條 | 如 AddType application/x-httpd-php .php 啟用 PHP 解析 |
AddHandler | 副檔名 → 處理器映射 | - | 如 AddHandler cgi-script .cgi .pl 指定 CGI 處理器 |
採用 <Directory> 容器封裝目錄專屬規則:
<Directory /home/httpd/html/private>
Options Indexes FollowSymLinks ; 允許目錄列表、跟隨符號連結
AllowOverride AuthConfig ; 允許 .htaccess 覆蓋認證設定
Order allow,deny
Allow from all ; 預設允許所有
# 進階:基於 IP 限制
# Deny from 192.168.1.100
# 進階:基本認證 (需建立 .htpasswd)
# AuthType Basic
# AuthName "Restricted Area"
# AuthUserFile /etc/httpd/conf/.htpasswd
# Require valid-user
</Directory>
| 指令 | 功能 | 常用值 |
|---|---|---|
Options | 目錄啟用功能 | Indexes (列表)、FollowSymLinks (符號連結)、ExecCGI (執行 CGI)、Includes (SSI)、None (禁用所有) |
AllowOverride | .htaccess 允許覆蓋範圍 | All、AuthConfig、Options、FileInfo、Limit、None |
Order | 允許/拒絕規則處理順序 | allow,deny (先允許後拒絕)、deny,allow (先拒絕後允許) |
Allow/Deny | IP/網段/域名存取控制 | from all、from 192.168.1.0/24、from .example.com |
Require | 認證授權規則 | valid-user (所有合法用戶)、user alice bob、group admins |
ServerType standalone ; 預設值,生產環境推薦
; ServerType inetd ; 開發測試可啟用
/etc/services:註冊 http 8080/tcp (避開 80 埠)/etc/inetd.conf:新增 http stream tcp nowait www /usr/sbin/httpd httpd -f /etc/httpd/conf/httpd.conf/etc/rc.d/init.d/inetd restarttelnet localhost 8080 或瀏覽器訪問 https://server:8080/雖然效能遜色,但 inetd 模式**每請求重讀配置檔**的特性,使其成為開發階段的最佳伴侶:修改 httpd.conf/srm.conf/access.conf 後無需重啟服務,下一請求即生效。配合 Tail -f /var/log/httpd/error_log 即時觀察錯誤,極大提升調試效率。