httpd(主程式)、httpd-manual、httpd-devel、system-config-httpd(GUI)httpd,systemctl start|stop|restart|enable httpd/etc/httpd/conf/httpd.confServerRoot "/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" commonAlias /webapp/ "/opt/webapp/"
<Directory "/opt/webapp">
Options Indexes MultiViews
AllowOverride None
Require all granted
</Directory>
瀏覽 https://server/webapp/ 映射至 /opt/webapp/。
htpasswd -c /etc/httpd/.htpasswd adminAuthType 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 |
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 Offmod_deflate GZIP 壓縮、mod_expires 快取控制event (2.4+ 高併發) / worker / prefork (相容非執行緒安全模組)apachectl configtest 驗證語法,systemctl reload httpd 平滑重載