内存消耗apache2.4

时间:2014-08-11 16:38:41

标签: performance apache vps

这些天我遇到了apache的问题。我有一个拥有4GB内存的VPS,并拥有一个每天点击50,000次的网站。前几天我看到我的MySQL服务器(托管在同一个VPS上)被丢弃了,然后我看到系统已经开始交换了。当我打开文件apache2.conf时发现Prefork没有规则,所以我决定限制apache进程。

现在我看到使用此命令的apache进程的平均大小:

ps -ylC apache2 | awk '{x += $8;y += 1} END {print "Apache Memory Usage (MB): "x/1024; print "Average Proccess Size (MB): "x/((y-1)*1024)}'

结果是:

Apache Memory Usage (MB): 3748.25
Average Proccess Size (MB): 234.265

我认为这个平均尺寸被夸大了,但我不知道。

我已经完成的下一步是使用此脚本计算适当数量的MaxClients:

#!/bin/bash
echo "Calculate MaxClients by dividing biggest Apache thread by free memory"
if [ -e /etc/debian_version ]; then
 APACHE="apache2"
elif [ -e /etc/redhat-release ]; then
 APACHE="httpd"
fi
APACHEMEM=$(ps -aylC $APACHE |grep "$APACHE" |awk '{print $8'} |sort -n |tail -n 1)
APACHEMEM=$(expr $APACHEMEM / 1024)
SQLMEM=$(ps -aylC mysqld |grep "mysqld" |awk '{print $8'} |sort -n |tail -n 1)
SQLMEM=$(expr $SQLMEM / 1024)
echo "Stopping $APACHE to calculate the amount of free memory"
/etc/init.d/$APACHE stop &> /dev/null
TOTALFREEMEM=$(free -m |head -n 2 |tail -n 1 |awk '{free=($4); print free}')
TOTALMEM=$(free -m |head -n 2 |tail -n 1 |awk '{total=($2); print total}')
SWAP=$(free -m |head -n 4 |tail -n 1 |awk '{swap=($3); print swap}')
MAXCLIENTS=$(expr $TOTALFREEMEM / $APACHEMEM)
MINSPARESERVERS=$(expr $MAXCLIENTS / 4)
MAXSPARESERVERS=$(expr $MAXCLIENTS / 2)
echo "Starting $APACHE again"
/etc/init.d/$APACHE start &> /dev/null
echo "Total memory $TOTALMEM"
echo "Free memory $TOTALFREEMEM"
echo "Amount of virtual memory being used $SWAP"
echo "Largest Apache Thread size $APACHEMEM"
echo "Amount of memory taking up by MySQL $SQLMEM"
if [[ SWAP > TOTALMEM ]]; then
      ERR="Virtual memory is too high"
else
      ERR="Virtual memory is ok"
fi
echo "$ERR"
echo "Total Free Memory $TOTALFREEMEM"
echo "MaxClients should be around $MAXCLIENTS"
echo "MinSpareServers should be around $MINSPARESERVERS"
echo "MaxSpareServers should be around $MAXSPARESERVERS"

这是输出:

Calculate MaxClients by dividing biggest Apache thread by free memory
Stopping apache2 to calculate the amount of free memory
Starting apache2 again
Total memory 3954
Free memory 3603
Amount of virtual memory being used 0
Largest Apache Thread size 252
Amount of memory taking up by MySQL 106
Virtual memory is ok
Total Free Memory 3603
MaxClients should be around 14
MinSpareServers should be around 3
MaxSpareServers should be around 7

使用这些参数,apache不再交换,虽然Web已经完全无法访问,但加载主页需要1分钟。此外,CPU使用率约为85%。

这是Prefork的完整配置:

<IfModule mpm_prefork_module>
    StartServers          4
    MinSpareServers       3
    MaxSpareServers       7
    MaxClients            15
    MaxRequestsPerChild   500
</IfModule>

任何人都知道如何才能让它正常运行?谢谢大家!

0 个答案:

没有答案