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

Apache 性能優化 25 條黃金法則:模塊、緩存、壓縮、OS 與應用層全維度調優

編輯:關於伺服器

Apache 核心層優化

  1. 移除無用模塊:LoadModule 按需保留,減少內存佔用與攻擊面
  2. 啟用 mod_disk_cache 而非 mod_mem_cache:磁盤緩存適合大文件、持久化,內存緩存易導致 OOM
  3. 扁平化配置 mod_disk_cache:避免多層目錄,提升查找效率
  4. 設置完整緩存頭:Expires、ETag、Cache-Control 三位一體
  5. 緩存目錄放獨立磁盤:避免與系統/日誌磁盤 IO 爭搶
  6. 使用管道日誌:CustomLog "|/usr/local/apache/bin/rotatelogs /var/log/access_%Y%m%d.log 86400" combined,避免頻繁寫入阻塞
  7. 日誌與緩存分盤:讀寫分離
  8. 啟用 mod_deflate (Gzip):壓縮 HTML/CSS/JS,流量減 70%+
  9. 關閉 HostnameLookups:HostnameLookups Off,避免 DNS 反查延遲
  10. 配置避免使用主機名:Allow from 192.168.1.0/24 而非域名
  11. 啟用持久連接:KeepAlive On
  12. KeepAliveTimeout 不宜過大:建議 2-5 秒,防止佔用 worker
  13. 禁用 .htaccess:AllowOverride None,改寫入主配置,避免每請求遞歸讀取
  14. 允許 FollowSymLinks:Options FollowSymLinks,關閉 SymLinksIfOwnerMatch 減少 lstat 系統調用
  15. 關閉 ExtendedStatus:ExtendedStatus Off,減少狀態統計開銷
  16. DirectoryIndex 避免通配符:明確列出 index.html index.php

作業系統層優化

  1. 調高 Swappiness:vm.swappiness=10(減少換出,保留內存給緩存)
  2. 增大寫入緩衝區:vm.dirty_ratio=40、vm.dirty_background_ratio=10
  3. 提高最大打開文件數:ulimit -n 65535,/etc/security/limits.conf 同步配置

應用層優化

  1. 靜態資源前置代理:Nginx/Varnish 反代圖片/CSS/JS,Apache 只處理動態
  2. Rails 使用 mod_passenger (Phusion Passenger):比 FastCGI 穩定高效
  3. PHP 關閉 safe_mode:已棄用,改用 open_basedir 隔離
  4. 避免 threaded MPM 搭配 mod_php:非線程安全,改用 php-fpm + event/worker MPM
  5. 刷新緩衝區預渲染:flush() 早發送頭部,瀏覽器提前解析
  6. 熱點數據本地緩存:Redis/Memcached/APCu 緩存頻繁查詢結果

驗證工具

  • ab -n 10000 -c 100 https://site/ 壓測對比
  • htop/iostat -x 1 觀察 CPU/IO
  • curl -I 檢查響應頭
Copyright © Windows教程網 All Rights Reserved