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

Apache HTTP Server 完整配置與管理實戰指南

編輯:關於伺服器

安裝與服務管理

  • 套件:httpd(主程式)、httpd-manual、httpd-devel、system-config-httpd(GUI)
  • 服務名:httpd,systemctl start|stop|restart|enable httpd
  • 主配置檔:/etc/httpd/conf/httpd.conf

核心配置段落詳解

第一區:全域環境

  • ServerRoot "/etc/httpd":程式、配置、日誌相對路徑基準
  • Listen 80:監聽所有介面 80 埠;Listen 192.168.1.10:8080 指定 IP/埠
  • ServerAdmin root@localhost:錯誤頁面顯示的管理員信箱
  • ServerName www.example.com:80:自我識別主機名(需 DNS 可解析),避免啟動警告

第二區:主伺服器配置

  • DocumentRoot "/var/www/html":網站根目錄絕對路徑
  • DirectoryIndex index.html index.php:預設首頁檔案優先順序
  • <Directory "/var/www/html"> 目錄權限控制:
    • Options Indexes FollowSymLinks:允許目錄列表、符號連結
    • AllowOverride None:禁止 .htaccess 覆蓋(效能與安全)
    • Require all granted (2.4+) / Order allow,deny; Allow from all (2.2):存取授權

日誌配置

  • ErrorLog logs/error_log:錯誤日誌
  • CustomLog logs/access_log combined:存取日誌(combined 格式含 Referer、User-Agent)
  • 格式定義:LogFormat "%h %l %u %t \"%r\" %>s %b" common

虛擬目錄(Alias)

Alias /webapp/ "/opt/webapp/"
<Directory "/opt/webapp">
    Options Indexes MultiViews
    AllowOverride None
    Require all granted
</Directory>

瀏覽 https://server/webapp/ 映射至 /opt/webapp/。

使用者驗證(基本認證)

  1. 建立密碼檔:htpasswd -c /etc/httpd/.htpasswd admin
  2. 目錄配置加入:
    AuthType Basic
    AuthName "Restricted Area"
    AuthUserFile /etc/httpd/.htpasswd
    Require user admin
    

虛擬主機三種模式

模式適用場景關鍵指令
IP-based每站點獨立 IP/SSL 憑證<VirtualHost 192.168.1.10>
Port-based同 IP 不同埠,測試/內部用<VirtualHost *:8080>
Name-based共用 IP,多域名(最常用)NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.a.com

Name-based 範例

NameVirtualHost *:80
<VirtualHost *:80>
    ServerName www.a.com
    DocumentRoot "/www/a"
    ErrorLog logs/a/error_log
    CustomLog logs/a/access_log combined
</VirtualHost>
<VirtualHost *:80>
    ServerName www.b.com
    DocumentRoot "/www/b"
    ErrorLog logs/b/error_log
    CustomLog logs/b/access_log combined
</VirtualHost>

第一個 VirtualHost 為預設主機,處理未匹配 Host 標頭的請求。

效能與安全建議

  • 生產環境關閉 Indexes、ServerTokens Prod、ServerSignature Off
  • 啟用 mod_deflate GZIP 壓縮、mod_expires 快取控制
  • MPM 選擇:event (2.4+ 高併發) / worker / prefork (相容非執行緒安全模組)
  • 定期 apachectl configtest 驗證語法,systemctl reload httpd 平滑重載
Copyright © Windows教程網 All Rights Reserved