/etc/services 為系統服務名稱與埠號/協定的映射資料庫,每行格式:
服務名稱 埠號/協定 別名
關鍵規則:
/etc/services 並設定為 8080 埠 (避免與 standalone 衝突)範例:
http 80/tcp www www-http ; standalone 模式標準 HTTP
http 8080/tcp www-proxy ; inetd 模式替代埠
當 Apache 以 inetd 模式運行時,每個 HTTP 請求由 inetd 接收後 fork 新進程處理。配置行格式:
服務名稱 socket類型 協定 wait/nowait 執行使用者 程式路徑 程式參數
Apache 典型配置:
http stream tcp nowait www /usr/sbin/httpd httpd -f /etc/httpd/conf/httpd.conf
stream:TCP 流式 socketnowait:inetd 不等待子進程結束即繼續接受新連線 (適用多執行緒/多進程服務)www:以低權限使用者 www 執行 (安全原則)Apache 發行版預置 htdocs/index.html (含 "It worked!" 標語)。確認檔案存在:
ls -l /home/httpd/html/index.html ; Red Hat 預設路徑
# /sbin/httpd -f /etc/httpd/conf/httpd.conf
⚠ 必須以 root 身份執行:
User/Group 指令降權至配置指定使用者 (如 www/www)無需手動啟動 httpd。系統啟動時 inetd 已監聽 8080 埠,收到請求自動 fork httpd 子進程。需確保 inetd 服務運行:
/etc/rc.d/init.d/inetd status
/etc/rc.d/init.d/inetd restart
| 錯誤訊息 | 根本原因 | 解決方案 |
|---|---|---|
Httpd: Could not open document config file ...fopen: No such file or directory | 配置檔路徑錯誤。Apache 啟動時不在當前目錄尋找,需絕對路徑 | 使用 -f /完整/路徑/httpd.conf 指定絕對路徑 |
Httpd: bad user name ...Httpd: bad group name ... | httpd.conf 中 User/Group 指定的使用者/群組在系統中不存在 | 建立對應系統帳號:groupadd www; useradd -g www -s /sbin/nologin www |
| 維度 | Standalone (預設/推薦) | inetd |
|---|---|---|
| 效能 | 常駐進程,零啟動延遲,高併發優勢明顯 | 每請求 fork 新進程,開銷大,高併發下明顯劣化 |
| 配置熱重載 | 修改配置需手動 apachectl restart 或發送 SIGHUP | 每請求重讀配置,修改即時生效,適合開發測試 |
| 資源佔用 | 常駐記憶體 | 無請求時零佔用 |
| 適用場景 | 生產環境、高流量站點 | 低頻訪問、開發測試、嵌入式設備 |
結論:生產環境**強烈建議 Standalone 模式**;僅在頻繁調整配置的開發階段考慮 inetd 模式。