温馨提示×

Apache2在Linux上如何优化性能

小樊
33
2025-12-16 20:40:45
栏目: 智能运维

Apache2 在 Linux 上的性能优化指南

一 基线检查与模块准备

  • 确认版本并保持更新:使用 apache2 -vhttpd -v 查看版本,优先使用 2.4 系列的稳定版本,及时修补安全与性能问题。
  • 启用必要模块:静态资源加速与压缩建议启用 mod_deflate、mod_expires、mod_headers;按需启用 mod_cache/mod_disk_cache 做页面/反向代理缓存;启用 mod_ssl 并优化会话复用。
  • 关闭无用模块与信息泄露:禁用不需要的模块(如 a2dismod 模块名),设置 ServerTokens ProdServerSignature Off,避免暴露版本与目录结构。
  • 基础连接与日志:将 HostnameLookups Off 减少 DNS 反向查询;按需调整 LogLevel(如 warn)降低日志开销。

二 选择并配置合适的 MPM

  • 选型建议:
    • prefork:多进程、无线程,兼容非线程安全模块(如传统 mod_php),稳健但内存占用高。
    • worker/event:多进程多线程,高并发下资源利用更优;event 在长连接、高并发场景通常更省资源(需模块支持)。
  • 典型配置示例(需结合内存与压测微调):
    • prefork(示例值,仅作起点):
      <IfModule mpm_prefork_module>
          StartServers          10
          MinSpareServers       30
          MaxSpareServers       45
          MaxRequestWorkers    1000
          MaxConnectionsPerChild 3000
      </IfModule>
      
    • event 或 worker(示例值,仅作起点):
      <IfModule mpm_event_module>
          StartServers          2
          MinSpareThreads      25
          MaxSpareThreads      75
          ThreadLimit          64
          ThreadsPerChild      25
          MaxRequestWorkers    400
          MaxConnectionsPerChild 0
      </IfModule>
      
  • 调参要点:
    • 估算并发上限:MaxRequestWorkers ≈ 内存 / 单进程/线程常驻内存;避免超过物理内存导致换页/崩溃。
    • 先定 ThreadsPerChild,再算 MaxRequestWorkers,最后用 ServerLimit/ThreadLimit 放开上限。
    • 长连接业务可适当提高 MaxKeepAliveRequestsKeepAliveTimeout,短连接业务缩短超时以释放资源。

三 关键运行时参数与静态资源优化

  • 持久连接与超时:
    KeepAlive On
    MaxKeepAliveRequests 100
    KeepAliveTimeout 5
    Timeout 5
    
  • 压缩与缓存:
    # Gzip 压缩
    <IfModule mod_deflate.c>
        AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript
    </IfModule>
    
    # 浏览器缓存
    <IfModule mod_expires.c>
        ExpiresActive On
        ExpiresByType text/css "access plus 30 days"
        ExpiresByType application/javascript "access plus 30 days"
        ExpiresByType image/jpeg "access plus 1 year"
        ExpiresByType image/png "access plus 1 year"
        ExpiresByType image/gif "access plus 1 year"
    </IfModule>
    
  • 静态文件传输:启用 sendfile(现代内核与 Apache 2.4 默认已优化),减少用户态/内核态拷贝,提高静态文件吞吐。
  • SSL/TLS(如启用 HTTPS):
    SSLEngine on
    SSLSessionCache "shmcb:/var/cache/mod_ssl/scache(512000)"
    SSLCompression off
    
  • 访问日志:避免过于复杂的日志格式与过高日志级别,生产环境常用 common 或精简格式,级别 warn

四 操作系统与网络栈优化

  • 文件描述符限制:提升进程可打开文件数,编辑 /etc/security/limits.conf
    * soft nofile 65535
    * hard nofile 65535
    * soft nproc  65535
    * hard nproc  65535
    
  • TCP 与内核参数(/etc/sysctl.conf):
    net.core.somaxconn = 65535
    net.ipv4.tcp_fin_timeout = 30
    net.ipv4.tcp_tw_reuse = 1
    net.ipv4.tcp_keepalive_time = 1200
    net.ipv4.ip_local_port_range = 10000 65000
    net.ipv4.tcp_max_syn_backlog = 8192
    net.ipv4.tcp_max_tw_buckets = 5000
    net.ipv4.tcp_syncookies = 1
    vm.swappiness = 10
    fs.file-max = 6815744
    
    应用:执行 sysctl -p
  • 存储与挂载:选择 ext4/xfs 等合适文件系统,挂载时使用 noatime 减少元数据写入。

五 验证 监控与回滚

  • 配置生效与语法检查:
    • Debian/Ubuntu:sudo a2enmod deflate expires headers ssl cachesudo systemctl restart apache2
    • RHEL/CentOS:sudo systemctl restart httpd
    • 语法检查:apache2ctl configtesthttpd -t
  • 压测与观测:使用 ab/jmeter 进行基准测试,结合 top/htop、iftop、netstat 观察 CPU、内存、连接数、带宽 等指标,逐步微调参数。
  • 变更管理:所有改动先在测试环境验证,记录配置与压测结果,保留回滚方案,变更后持续监控稳定性与性能。

0