温馨提示×

如何在Ubuntu上调整Apache内存使用

小樊
34
2025-12-05 03:26:59
栏目: 智能运维

在 Ubuntu 上调整 Apache 内存使用的实用步骤

一 基线评估与监控

  • 查看系统内存与负载:使用命令 free -htop/htop 观察可用内存、Swap 使用与 Apache 进程占用情况。
  • 评估单个 Apache 进程内存:运行 ps -U www-data -u www-data u(Ubuntu 上 Apache 常运行于 www-data 用户),记录 RSS(常驻内存)。由于 RSS 包含共享库,估算“每个进程独占内存”可用近似:RSS × 0.5~0.7 作为保守值。
  • 做一次基线压测:用 ab(ApacheBench)在测试环境执行如 ab -n 1000 -c 100 -k http://yoursite/ 的压测,记录 Requests per secondTime per request 与内存变化,为后续调参提供依据。

二 选择合适 MPM 并调整核心并发参数

  • 查看当前 MPM:执行 apachectl -M | grep mpm,Ubuntu 常见为 event(默认)、workerprefork
  • 选择原则:
    • prefork:每个进程单线程,适合需线程安全或 mod_php 的场景,内存占用更高。
    • worker/event:多进程多线程,更省内存、并发更高;event 对长连接/KeepAlive 空闲连接处理更高效,但不兼容非线程安全模块。
  • 计算 MaxRequestWorkers 的上限:
    • 公式:MaxRequestWorkers ≤ 可用内存 / 每进程独占内存
    • 示例:若可用内存 2 GB,单进程独占内存估算 40 MB,则上限约为 50
  • 配置示例(编辑对应 MPM 段,路径通常为 /etc/apache2/mods-available/mpm_*.conf):
    • prefork(示例)
      <IfModule mpm_prefork_module>
          StartServers            5
          MinSpareServers         5
          MaxSpareServers        10
          MaxRequestWorkers     150   # 依据上式计算
          MaxConnectionsPerChild  1000 # 定期回收,缓解内存碎片/泄漏
      </IfModule>
      
    • event(示例)
      <IfModule mpm_event_module>
          StartServers             2
          MinSpareThreads         25
          MaxSpareThreads         75
          ThreadLimit             64
          ThreadsPerChild         25
          MaxRequestWorkers     150   # 依据上式计算
          MaxConnectionsPerChild  1000
      </IfModule>
      
  • 修改后检查语法并重启:
    sudo apachectl configtest
    sudo systemctl restart apache2
    

三 连接与会话优化

  • 启用并收敛 KeepAlive:
    KeepAlive On
    MaxKeepAliveRequests 100
    KeepAliveTimeout 2
    
    适度降低 KeepAliveTimeout 可在相同内存下支持更多连接;过高会保持过多空闲连接占用内存。
  • 启用压缩与缓存以降负载:
    • 压缩:启用 mod_deflate 减少传输体积。
    • 缓存:启用 mod_cachemod_expires,对静态资源设置长期过期,减少后端与带宽压力。
    # 启用模块
    sudo a2enmod deflate expires cache cache_disk
    
    # 示例:磁盘缓存与过期头
    <IfModule mod_cache.c>
        CacheDefaultExpire 86400
        CacheEnable disk  /
        CacheRoot /var/cache/apache2/mod_cache_disk
        CacheDirLevels 2
        CacheDirLength 1
    </IfModule>
    
    <IfModule mod_expires.c>
        ExpiresActive On
        ExpiresByType text/css  "access plus 1 month"
        ExpiresByType application/javascript "access plus 1 month"
        ExpiresByType image/png  "access plus 1 month"
    </IfModule>
    
    修改后执行 sudo apachectl configtest && sudo systemctl restart apache2

四 模块精简与系统层面优化

  • 精简模块:禁用不需要的模块以减少常驻内存,例如 a2dismod statusa2dismod autoindex(按需保留)。
  • 限制恶意或异常请求:启用 mod_ratelimitmod_security 缓解突发流量与爬虫影响。
  • 内核与虚拟内存:
    • 适度降低 vm.swappiness(如 10~30)以减少换页,仅在确有需要时才提高。
    • 谨慎调整 vm.overcommit_memory 与透明大页(THP),变更前应在测试环境验证。
  • 必要时增加 Swap 空间,作为极端峰值的缓冲(不替代合理并发配置)。

五 验证与回滚

  • 每次调参后进行压测与监控:重复 ab 测试,观察 MaxRequestWorkers 下的 Requests per secondTime per request、内存占用与错误率,确保既满足并发又不过载。
  • 逐步迭代:一次只调整一个关键参数,记录基线—变更—结果,便于回滚与对比。
  • 配置检查与回滚:每次修改后先 apachectl configtest,确认无误再 systemctl restart apache2;异常则恢复到上一版配置。

0