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

Linux 架設 WWW 伺服器實戰 (三):服務註冊、inetd 模式與首次啟動調試

編輯:關於伺服器

系統服務註冊:/etc/services 配置

/etc/services 為系統服務名稱與埠號/協定的映射資料庫,每行格式:

服務名稱  埠號/協定  別名

關鍵規則:

  • TCP/UDP 前 1024 埠為**保留埠**,僅允許 root 或系統進程綁定
  • Apache 預設獨立模式 (standalone) 使用 80 埠
  • 若採用 inetd 超級伺服器模式,需註冊於 /etc/services 並設定為 8080 埠 (避免與 standalone 衝突)

範例:

http        80/tcp      www www-http   ; standalone 模式標準 HTTP
http        8080/tcp    www-proxy      ; inetd 模式替代埠

inetd 超級伺服器模式配置:/etc/inetd.conf

當 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 流式 socket
  • nowait:inetd 不等待子進程結束即繼續接受新連線 (適用多執行緒/多進程服務)
  • www:以低權限使用者 www 執行 (安全原則)

首次啟動與驗證流程

1. 先決條件:DocumentRoot 須含 index.html

Apache 發行版預置 htdocs/index.html (含 "It worked!" 標語)。確認檔案存在:

ls -l /home/httpd/html/index.html   ; Red Hat 預設路徑

2. 手動啟動 Standalone 模式 (生產環境標準)

# /sbin/httpd -f /etc/httpd/conf/httpd.conf

⚠ 必須以 root 身份執行:

  • 綁定 80 埠 (≤1024) 需 root 權限
  • 啟動後透過 User/Group 指令降權至配置指定使用者 (如 www/www)

3. inetd 模式啟動

無需手動啟動 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 vs inetd

維度Standalone (預設/推薦)inetd
效能常駐進程,零啟動延遲,高併發優勢明顯每請求 fork 新進程,開銷大,高併發下明顯劣化
配置熱重載修改配置需手動 apachectl restart 或發送 SIGHUP每請求重讀配置,修改即時生效,適合開發測試
資源佔用常駐記憶體無請求時零佔用
適用場景生產環境、高流量站點低頻訪問、開發測試、嵌入式設備

結論:生產環境**強烈建議 Standalone 模式**;僅在頻繁調整配置的開發階段考慮 inetd 模式。

Copyright © Windows教程網 All Rights Reserved