Linux 系統常見 Web 伺服器:CERN、NCSA、Apache 三種。Apache 以跨平台、配置簡明、最大兼容性成為首選。特色功能:整合代理伺服器、Perl 腳本、會話追蹤、日誌定製、虛擬主機、HTTP 認證等。配合 Linux 強大內核,構建高性能 Web 平台。
# 下載範例:apache_1.3.x.tar.gz
tar xvzf apache_1.3.x.tar.gz
# 解壓即完成安裝
# 安裝 RPM
rpm -ivh apache_1.3.x.rpm
# 進入源碼目錄配置編譯
cd /usr/src/apache-1.3.x
./configure
make
# 複製執行檔與配置檔
cp httpd /etc/httpd/bin/
cp httpd.conf access.conf srm.conf mime.types /etc/httpd/conf/
大多數 Linux 發行版預裝 Apache,若未安裝可從安裝光碟或 Apache 官網 獲取(提供源碼與二進制包兩種版本)。
啟動 Web 服務僅需將 Linux 接入互聯網,主頁放置於 /home/httpd。為提升效能需深度配置四大核心檔案:
| 指令 | 說明 | 範例 |
|---|---|---|
| ServerType | 運行模式:standalone(獨立守護進程,高效能,預設) inetd(較安全,RedHat 預設) | ServerType standalone |
| Port | 主進程監聽埠 | Port 80 |
| ErrorLog | 錯誤日誌路徑 | ErrorLog /var/httpd/error.log |
| ServerRoot | 配置與日誌儲存根目錄 | ServerRoot /etc/httpd |
| ServerAdmin | 管理員郵箱 | ServerAdmin admin@example.com |
inetd 模式額外配置:
# /etc/inetd.conf 新增:
httpd stream tcp nowait httpd /etc/httpd/bin/httpd -f /etc/httpd/conf/httpd.conf
# /etc/services 新增:
httpd 80/tcp httpd
| 指令 | 說明 | 範例 |
|---|---|---|
| DocumentRoot | 主文檔根目錄 | DocumentRoot /home/httpd/html |
| DirectoryIndex | 預設首頁檔名(支援多備選) | DirectoryIndex index.html index.htm |
# 重啟 Apache 使配置生效
/etc/rc.d/init.d/httpd restart
# 或使用系統服務腳本
service httpd restart
透過 IP/網段允許/拒絕存取。但現今多數用戶使用動態 IP,實用性有限。
適應動態 IP 環境,安全性更高。具體實現涉及 htpasswd 建立密碼檔、.htaccess 或 httpd.conf 定義保護領域、AuthType、AuthUserFile、Require valid-user 等指令。詳細配置超出本文範圍,請參閱 Apache 官方文檔進階學習。
生產環境建議: