首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

Linux Apache 服務器配置與管理實戰指南:從目錄結構到命令行操作

     編輯:PHP 綜合知識

一、Apache 兩大核心目錄

目錄標準路徑 (RHEL/CentOS/Fedora)Debian/Ubuntu 路徑用途
配置目錄/etc/httpd/conf//etc/apache2/主配置 httpd.conf、模組、虛擬主機
文檔根目錄/var/www/html//var/www/html/預設網站檔案存放處

關鍵子目錄:

  • conf.d/ 或 sites-available/ —— 模組化配置片段
  • modules/ —— 動態載入模組 .so
  • logs/ 或 /var/log/httpd/ —— 存取/錯誤日誌

二、兩種配置模式

模式優點缺點適用場景
命令行編輯 httpd.conf彈性高、可版控、自動化部署友善需熟悉指令語法、易拼錯生產環境、CI/CD、資深運維
圖形化工具 (system-config-httpd / Webmin)低門檻、視覺化、即時語法檢查依賴桌面環境、難自動化初學者、桌面版測試機

建議:生產環境**務必掌握命令行配置**,配合 Git 版控 /etc/httpd/。

三、服務管理:systemd 現代指令

# 啟動/停止/重啟/重載配置
systemctl start httpd
systemctl stop httpd
systemctl restart httpd
systemctl reload httpd      # 平滑重載,不中斷連線

# 開機自啟
systemctl enable httpd
systemctl disable httpd

# 狀態檢查
systemctl status httpd
systemctl is-enabled httpd

# 即時追蹤日誌
journalctl -u httpd -f
tail -f /var/log/httpd/error_log

舊式 SysV 指令(舊版/CentOS 6):

service httpd start|stop|restart|reload
chkconfig httpd on
chkconfig --list httpd

四、核心配置檔 httpd.conf 關鍵段落解析

# 1. 基礎設定
ServerRoot "/etc/httpd"
Listen 80
ServerName www.example.com:80
DocumentRoot "/var/www/html"

# 2. MPM 模組選擇(預載入一種)
# prefork: 穩定、相容舊模組(如 mod_php)
# event/worker: 高併發、低記憶體,需 PHP-FPM
# LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
# LoadModule mpm_event_module modules/mod_mpm_event.so

# 3. 目錄權限模板

    Options Indexes FollowSymLinks
    AllowOverride All          # 允許 .htaccess
    Require all granted        # 2.4+ 語法


# 4. 虛擬主機引入
IncludeOptional conf.d/*.conf
# 或 Debian 風格
IncludeOptional sites-enabled/*.conf

五、模組管理

# 查看已載入模組
httpd -M
apachectl -M

# 安裝常用模組 (CentOS/RHEL)
yum install mod_ssl mod_rewrite mod_proxy mod_headers mod_deflate mod_security

# 啟用/停用模組 (Debian/Ubuntu)
a2enmod rewrite ssl headers proxy deflate
a2dismod autoindex
systemctl reload apache2

六、虛擬主機配置範例

# /etc/httpd/conf.d/vhost-example.conf

    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public

    
        AllowOverride All
        Require all granted
    

    ErrorLog logs/example.com-error_log
    CustomLog logs/example.com-access_log combined

    # PHP-FPM 代理 (推薦)
    
        SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost"
    


# HTTPS 範例

    ServerName example.com
    DocumentRoot /var/www/example.com/public
    SSLEngine on
    SSLCertificateFile /etc/pki/tls/certs/example.com.crt
    SSLCertificateKeyFile /etc/pki/tls/private/example.com.key
    # ... SSL 參數優化 ...

七、安全硬化清單

  • ✅ 關閉 ServerTokens Prod、ServerSignature Off 隱藏版本資訊
  • ✅ 僅啟用必要模組,移除 autoindex, status, info 等
  • ✅ 禁用 Options Indexes 防止目錄瀏覽
  • ✅ 配置 mod_security (WAF) + OWASP CRS 規則集
  • ✅ 限制 LimitRequestBody、Timeout、MaxRequestWorkers
  • ✅ 強制 HTTPS:Header always set Strict-Transport-Security "max-age=31536000"
  • ✅ 日誌審計:LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

八、效能調優關鍵參數

參數建議值說明
MaxRequestWorkersRAM(GB) * 100~200prefork 最大進程數,防 OOM
KeepAliveOn啟用持久連線
KeepAliveTimeout2~5避免佔用 worker
EnableSendfileOn核心零拷貝傳輸靜態檔
EnableMMAPOffNFS/網路磁碟需關閉
mod_deflate啟用Gzip 壓縮 text/* application/json
mod_expires啟用靜態資源長期快取

九、除錯技巧

# 語法檢查
httpd -t
apachectl configtest

# 查看編譯參數
httpd -V

# 模擬請求測試
curl -I https://localhost/
ab -n 1000 -c 10 https://localhost/  # 壓測
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved