Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

Linux 架設 WWW 伺服器實戰 (二):三大核心配置檔深度解析與運行模式選擇

編輯:關於伺服器

Apache 三大核心配置檔架構

Apache 1.3 (Red Hat 5.2 預設) 採用三檔分離架構,位於 /etc/httpd/conf/:

  • httpd.conf:核心引擎配置 (MPM、模組載入、網路監聽、日誌、虛擬主機)
  • srm.conf:資源映射配置 (文檔根目錄、索引、別名、腳本映射、MIME 類型)
  • access.conf:存取控制配置 (目錄級權限、認證授權、功能限制)

現代演進:Apache 2.0+ 已合併至單一 httpd.conf,透過 Include 引入模組化配置,但理解三檔架構有助於掌握核心概念。

srm.conf:資源映射與內容協商核心指令

指令功能預設值 (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
ScriptAliasCGI 腳本目錄 URL 映射/cgi-bin/ → /home/httpd/cgi-bin/映射目錄下所有檔案視為可執行腳本,非靜態內容
AddType副檔名 → MIME 類型映射內建數百條如 AddType application/x-httpd-php .php 啟用 PHP 解析
AddHandler副檔名 → 處理器映射-如 AddHandler cgi-script .cgi .pl 指定 CGI 處理器

access.conf:目錄級存取控制與認證授權

採用 <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/DenyIP/網段/域名存取控制from all、from 192.168.1.0/24、from .example.com
Require認證授權規則valid-user (所有合法用戶)、user alice bob、group admins

運行模式深度對比與配置

1. 兩大模式本質差異

  • Standalone (獨立守護進程):Apache 主進程常駐監聽,預先 fork 子進程/執行緒池,請求直接分派處理。高效能、低延遲,生產環境標準。
  • inetd (超級伺服器代管):inetd 監聽埠口,請求到達時 fork Apache 子進程處理,處理完畢退出。零常駐記憶體、配置即時生效,但每請求有進程建立開銷。

2. httpd.conf 中模式切換

ServerType standalone   ; 預設值,生產環境推薦
; ServerType inetd      ; 開發測試可啟用

3. 啟用 inetd 模式額外配置步驟

  1. 修改 /etc/services:註冊 http 8080/tcp (避開 80 埠)
  2. 修改 /etc/inetd.conf:新增 http stream tcp nowait www /usr/sbin/httpd httpd -f /etc/httpd/conf/httpd.conf
  3. 重啟 inetd:/etc/rc.d/init.d/inetd restart
  4. 驗證:telnet localhost 8080 或瀏覽器訪問 https://server:8080/

為何保留 inetd 模式?開發測試的利器

雖然效能遜色,但 inetd 模式**每請求重讀配置檔**的特性,使其成為開發階段的最佳伴侶:修改 httpd.conf/srm.conf/access.conf 後無需重啟服務,下一請求即生效。配合 Tail -f /var/log/httpd/error_log 即時觀察錯誤,極大提升調試效率。

Copyright © Windows教程網 All Rights Reserved