温馨提示×

apache如何处理centos高并发

小樊
54
2025-12-31 05:17:36
栏目: 智能运维

在 CentOS 上让 Apache 稳定承载高并发

一 架构与 MPM 选择

  • 优先选用 MPM Event(事件驱动、异步处理长连接),在 CentOS 7 上常见默认是 prefork,需切换。检查与切换示例:
    • 查看:httpd -V | grep -i mpm
    • 启用 Event(示例路径):编辑 /etc/httpd/conf.modules.d/00-mpm.conf,确保加载 LoadModule mpm_event_module modules/mod_mpm_event.so,并注释其他 MPM 模块;重启 systemctl restart httpd
  • 若因模块依赖(如某些 PHP 版本)只能使用 prefork,需按进程模型单独调参(见下文示例)。

二 Apache 关键配置

  • MPM Event 示例(放在 /etc/httpd/conf/httpd.conf/etc/httpd/conf.modules.d/00-mpm.conf<IfModule mpm_event_module> 内):
    StartServers            8
    MinSpareThreads         64
    MaxSpareThreads         256
    ThreadsPerChild         64
    ServerLimit             32
    MaxRequestWorkers      2048
    MaxConnectionsPerChild 10000
    Timeout                30
    KeepAlive              On
    MaxKeepAliveRequests   100
    KeepAliveTimeout       2
    
    计算关系:MaxRequestWorkers ≈ ServerLimit × ThreadsPerChild(上例为 32 × 64 = 2048)。
  • MPM Prefork 示例(仅在不支持线程或依赖非线程安全模块时使用):
    <IfModule mpm_prefork_module>
        StartServers          5
        MinSpareServers       5
        MaxSpareServers      10
        MaxRequestWorkers    150
        MaxConnectionsPerChild 0
    </IfModule>
    
  • 启用压缩与缓存、精简模块与日志:
    • 压缩(mod_deflate):
      <IfModule mod_deflate.c>
          AddOutputFilterByType DEFLATE text/html text/plain text/css text/javascript application/javascript application/json
      </IfModule>
      
    • 缓存(mod_expires):
      <IfModule mod_expires.c>
          ExpiresActive On
          ExpiresByType image/jpg  "access plus 1 month"
          ExpiresByType image/jpeg "access plus 1 month"
          ExpiresByType image/png  "access plus 1 month"
          ExpiresByType text/css  "access plus 1 week"
          ExpiresByType application/javascript "access plus 1 week"
      </IfModule>
      
    • 精简模块:禁用不需要的模块(如 info、status、autoindex、userdir)以降低内存与攻击面。
    • 日志轮转:使用 rotatelogs 避免大文件影响性能:
      CustomLog "|/usr/sbin/rotatelogs /var/log/httpd/access_log.%Y%m%d 86400" combined
      
    • 可选:启用 mod_status 监控(限制来源 IP):
      <Location /server-status>
          SetHandler server-status
          Require ip 192.168.1.0/24
      </Location>
      
    以上配置能显著降低带宽、提升缓存命中、减少资源占用并便于排障。

三 操作系统与内核参数

  • 文件描述符与进程限制(提升可打开连接数与进程数):
    • 编辑 /etc/security/limits.conf
      * soft nofile 65535
      * hard nofile 65535
      * soft nproc  65535
      * hard nproc  65535
      apache soft nofile 65535
      apache hard nofile 65535
      
    • 系统级:echo "fs.file-max = 1000000" >> /etc/sysctl.conf
  • 网络栈优化(/etc/sysctl.conf,执行 sysctl -p 生效):
    net.ipv4.tcp_tw_reuse = 1
    net.ipv4.tcp_tw_recycle = 0     # NAT/云环境建议关闭
    net.ipv4.tcp_fin_timeout = 30
    net.ipv4.tcp_max_tw_buckets = 20000
    net.ipv4.ip_local_port_range = 1024 65000
    net.ipv4.tcp_rmem = 4096 87380 16777216
    net.ipv4.tcp_wmem = 4096 16384 16777216
    net.core.rmem_max = 16777216
    net.core.wmem_max = 16777216
    net.core.netdev_max_backlog = 30000
    net.ipv4.tcp_max_syn_backlog = 4096
    net.core.somaxconn = 4096
    net.ipv4.tcp_syncookies = 1
    net.ipv4.tcp_max_orphans = 262144
    net.ipv4.tcp_keepalive_time = 600
    net.ipv4.tcp_keepalive_intvl = 30
    net.ipv4.tcp_keepalive_probes = 3
    net.ipv4.tcp_slow_start_after_idle = 0
    
  • I/O 与内存(SSD 场景示例):
    echo noop > /sys/block/sda/queue/scheduler
    echo 0 > /proc/sys/vm/swappiness
    
    这些调整可缓解 TIME_WAIT 堆积、提升连接队列与网络吞吐,为高并发提供底层支撑。

四 监控与压测

  • 连接与状态查看:
    • 实时连接状态:ss -tulnp | grep httpd
    • 统计 80 端口连接数:netstat -nat | grep -i "80" | wc -l
    • 统计已建立连接:netstat -na | grep ESTABLISHED | wc -l
    • 按状态汇总:netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
    • Apache 实时状态:http://your-server/server-status?auto(需启用 mod_status)
  • 压力测试与验证:
    • 基准测试:ab -n 1000 -c 100 http://your-server/index.html
    • 逐步增大并发(如 -c 500/1000/2000),观察 CPU、内存、连接队列、错误率,与 MaxRequestWorkers、内核参数联动微调。

五 架构与扩展建议

  • 静态资源交由 Nginx 或 CDN 处理,Apache 专注动态内容(如 PHP),降低后端压力、提升总体并发与缓存命中。
  • 动态层建议搭配 PHP-FPM 进程池 优化(进程/子进程数、最大请求数、慢日志),与 Apache 的并发参数协同。
  • 进一步引入 Redis/Memcached 做页面/数据缓存,减少后端数据库与后端应用的计算与 I/O 压力。

0