1GB VPS - Apache Worker MPM - FCGID - 最大并发连接数 - RAM CAP

use*_*874 9 memory mpm-worker mod-fcgid apache-2.2

我花了一两个星期研究和设置我的服务器来运行带有 Worker MPM 和 FCID 的 Apache。我正在尝试优化它以允许尽可能多的并发连接。在 Worker MPM 上找到好的信息一直是一场噩梦。

服务器 - 具有 1GB RAM 的 VPS(关闭 Apache 仅使用大约 150MB 的 RAM)我希望 Apache 具有大约 750MB 的内存使用上限 - 这样我的服务器就永远不会耗尽 RAM。

我已经运行服务器大约 2 年没有任何问题 - 但我们最近开始流式传输 MP3,这需要更多的并发连接。服务器也发生了一些轻微的 DDOS 攻击 - 所以我将设置减少了很多以防止服务器内存不足 - 我还添加了一些防火墙规则来限制速率。

我现在的设置看起来运行良好 - 但我遇到了一些 Segmentation fault 错误

[Sat Mar 23 03:19:50 2013] [notice] child pid 28351 exit signal Segmentation fault (11)
[Sat Mar 23 03:56:20 2013] [notice] child pid 29740 exit signal Segmentation fault (11)
*** glibc detected *** /usr/sbin/httpd.worker: malloc(): memory corruption: 0xb83abdd8 ***
Run Code Online (Sandbox Code Playgroud)

还有一些内存不足的错误

Out of memory during array extend.
Run Code Online (Sandbox Code Playgroud)

这是我目前的设置,我真的很感激一些建议。

阿帕奇设置:

Timeout 30
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 2
#####################
# Spawn 2 child processes, spawning 25 threads for each child process.
# So, a pool of 50 threads is left up and sleeping, ready to serve incoming requests.
# If more requests will come in, apache will spawn new child processes, each one spawning 25 threads,
# enlarging the thread pool until the total number of threads become 50. In that case, apache begin
# to cleanly drop processes, trying to reach 25 threads.
# New processes and its threads are spawned in case of a large spike of requests, until 200 parallel
# client requests are reached, then apache will no longer accept new incoming connections.
# When the load calm down, and requests come back under 200 parallel connections, apache will continue
# to accept connections. After 25, 000 requests served by a child, q. 1000 per thread, the process
# get closed by the father to ensure no memory leak is fired.
<IfModule worker.c>
ServerLimit      16
StartServers         2
MaxClients       400
MinSpareThreads   25
MaxSpareThreads  50 
ThreadsPerChild    25
MaxRequestsPerChild  1000
ThreadLimit          64 
ThreadStackSize      1048576
</IfModule>
#####################
Run Code Online (Sandbox Code Playgroud)

然后在 fcgid.conf 中进行一些设置

FcgidMinProcessesPerClass 0 
FcgidMaxProcessesPerClass 8 
FcgidMaxProcesses  25
FcgidIdleTimeout 60 
FcgidProcessLifeTime 120 
FcgidIdleScanInterval 30
Run Code Online (Sandbox Code Playgroud)

按照我的要求输出 /etc/my.cnf

[mysqld]
数据目录=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
用户=mysql

#skip-innodb

连接超时 = 10
最大连接数 = 300
符号链接=0
innodb_file_per_table = 1
myisam_sort_buffer_size = 8M
read_rnd_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
sort_buffer_size = 512K
表缓存 = 32
max_allowed_pa​​cket = 1M
key_buffer = 16k
查询缓存类型 = 1
查询缓存大小 = 32M
线程缓存大小 = 16
net_buffer_length = 2K
线程堆栈 = 256K
等待超时 = 300

慢查询日志

#log-slow-queries=/var/log/mysql/slow-queries.log
slow_query_log=/var/log/mysql/slow-queries.log
long_query_time = 1

[mysqld_safe]
日志错误=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

和 PHP memory_limit = 64M

Mic*_*and 0

这些设置都与平衡有关,即您可以将它们设置为多高,而不会冒内存不足和服务器崩溃的风险,或者让您的进程被 vps 父进程杀死,这可能就是您收到 SegFaults 的原因。

通常,当我优化服务器时,我会运行 mysql adjustment-primer.sh 脚本来了解 MySQL 最多可以使用多少内存:

https://launchpad.net/mysql-tuning-primer

然后,对于 prefork,我将 MaxClients 乘以 php memory_limit,以了解 Apache+PHP 最多可以使用多少内存。这些都是粗略的估计,但是一旦你做了很多次,你就会有一定的感觉。

我尝试将这两个值的总和保持在服务器的最大内存附近,如果您的 VPS 没有交换分区,我肯定会尝试将其保持在低于最大内存的水平,原因如下:

1)服务器上的其他进程将使用内存

2) 服务器上的某些 php 脚本可能会使用 ini_set 来更改自身的内存限制。

如果您可以提供 /etc/my.cnf 和 php memory_limit 我也许可以为您提供一些好的设置。


编辑:我只是想提一下,我知道您使用的是worker而不是prefork,相同的概念适用,但worker必须处理线程而不仅仅是MaxClients,所以prefork是一个更好的例子。在获得所需信息后,我必须查看设置,以便为您提供好的建议