调整 Apache2 prefork MaxClients ServerLimit

Use*_*r-N 23 hosting mpm-prefork shared-hosting segmentation-fault apache-2.2

我有一台 128 GB Ram 的机器,它使用 Apache2 作为 Web 服务器(在这台机器上没有数据库服务器,数据库机器是一台 64 GB Ram 机器,可以处理 2000 个最大连接)。我使用监控工具看到目前大约有 44 个忙碌的工作人员和 12 个空闲的工作人员,我的 prefork 模块的最佳理论值是多少?

我有时会在高负载时间加载网站时出现空白页面,并在我的 apache 错误日志中出现此错误:

【注意】child pid 13595 exit signal Segmentation fault (11)

如何解决这个问题呢?

我的 Apache2 Prefork 模块配置:

StartServers          3
MinSpareServers       3
MaxSpareServers       5
ServerLimit           3200
MaxClients            3100
MaxRequestsPerChild   0
Run Code Online (Sandbox Code Playgroud)

在 www 机器上免费 -h

总计:128 G 空闲空间:97GB(运行 apache2)共享 0b 缓冲区 1.9G 缓存 23G

Apache2 和其他程序使用的 Ram:

Private  +   Shared  =  RAM used    Program

 96.0 KiB +  61.0 KiB = 157.0 KiB   sh
176.0 KiB +  26.0 KiB = 202.0 KiB   atd
176.0 KiB +  35.5 KiB = 211.5 KiB   acpid
208.0 KiB +  19.5 KiB = 227.5 KiB   mdadm
204.0 KiB +  30.0 KiB = 234.0 KiB   init
248.0 KiB +  62.0 KiB = 310.0 KiB   sendmail
376.0 KiB +  36.0 KiB = 412.0 KiB   dbus-daemon
388.0 KiB + 285.5 KiB = 673.5 KiB   cron (2)
820.0 KiB +  42.0 KiB = 862.0 KiB   gam_server
920.0 KiB + 108.0 KiB =   1.0 MiB   ntpd
968.0 KiB + 243.0 KiB =   1.2 MiB   getty (6)
  1.3 MiB + 351.5 KiB =   1.6 MiB   udevd (3)
  1.5 MiB + 343.0 KiB =   1.8 MiB   sendmail-msp
  2.0 MiB + 910.0 KiB =   2.9 MiB   plugin-localresources2
  3.4 MiB +  50.0 KiB =   3.4 MiB   rsyslogd
  3.6 MiB +  68.5 KiB =   3.7 MiB   bash
  1.9 MiB +   2.1 MiB =   4.0 MiB   sendmail-mta (4)
  3.8 MiB + 556.0 KiB =   4.3 MiB   sshd (2)
  3.7 MiB +   1.2 MiB =   4.8 MiB   plugin-apache2
  5.1 MiB +   1.2 MiB =   6.3 MiB   agent-service
  7.0 MiB + 654.0 KiB =   7.6 MiB   fail2ban-server
  9.6 MiB +   2.6 MiB =  12.2 MiB   proftpd (8)
 59.2 MiB +  70.0 KiB =  59.3 MiB   miniserv.pl
 96.8 MiB +   3.6 MiB = 100.4 MiB   php5-cgi (2)
196.4 MiB +  35.9 MiB = 232.3 MiB   apache2 (40)
---------------------------------
                     tot 450.0 MiB
Run Code Online (Sandbox Code Playgroud)

Hrv*_*jar 70

Apache prefork 设置,根据apache 性能调整指南

引用:

The single biggest hardware issue affecting webserver performance is RAM.
A webserver should never ever have to swap, as swapping increases the latency
of each request beyond a point that users consider "fast enough". 
This causes users to hit stop and reload, further increasing the load.
You can, and should, control the MaxClients setting so that your server does
not spawn so many children it starts swapping. This procedure for doing this
is simple: determine the size of your average Apache process, by looking at
your process list via a tool such as top, and divide this into your total 
available memory, leaving some room for other processes.
Run Code Online (Sandbox Code Playgroud)

你应该根据你的输入来设置它:

  • 总内存:128 GB
  • -10% 内存用于除 apache 之外的所有内容:115 GB
  • 现在我们需要弄清楚有多少单个 apache 进程正在使用。

要计算这一点,您可以使用以下脚本:

pgrep apache2 | xargs -n1 -I{} cat /proc/{}/smaps | \
  awk '{if ($0 ~ /stack/) {pids+=1} else if ($0 ~/^Shared_/) 
    {shared+=$2} else if ($0 ~ /^Pss:/) {priv+=$2}} END {
      printf "%.2f MB\n",(priv+shared/(pids*pids))/1024}'
Run Code Online (Sandbox Code Playgroud)

这是对单个 apache 进程使用内存的最佳估计,同时尝试按比例划分每个活动 apache 进程的共享使用量并将其添加到Pss(按比例设置大小)之上

最后你用这个数字除以 115 GB,你得到MaxClients/ServerLimit. 从这里您可以相对计算其他数字,例如

  • StartServers 30% 的 MaxClients
  • MinSpareServers 5% 的 MaxClients
  • MaxSpareServers 10% 的 MaxClients
  • ServerLimit == 最大客户数
  • MaxConnectionsPerChild 10000(作为解决内存泄漏应用程序可能问题的保守替代方案)

  • 我希望有比我更多代表点的人会给你投票这个答案,非常感谢! (2认同)
  • 你的计算脚本给了我 842.13 MB。这比我想要的(CentOS 6.7 上的 apache 2.2)高出一个数量级。 (2认同)
  • @QuinnComendant 邪恶的技巧,但是如果 prefork 模块可用(确实如此),IfModule 将为 true,并且实际上两个选项都可用,worker 和 MPM...我的系统上有相同的服务器,worker 的内存使用量约为 850 MB。prefork 不可能为 1 个进程使用 800 MB 内存。您更改了脚本中将进程名称 apache2 引用为 httpd 的行,对吗? (2认同)