Apache 的 MaxClients 與 MaxRequestsPerChild 之後

MaxClients 大致上是,用

ps  -ylC httpd --sort:rss

找出其中一個 apache 程序佔用的 memory 量 (KB)

MaxClients = All memory / one apache memory

再來用系統全部的 memory 除以一個 apache 程序佔用的 memory ,就大約是我可以用多少 MaxClients

不過也不一定要用這算出來的值,自己適情況調大

以下找一些別人寫好的 script 可以參考用

參考 : http://techietent.blogspot.tw/2013/05/script-to-calculate-maxclients-in-apache.html

#!/bin/bash
tome=$(free -m | grep -i mem | awk '{print $2}')
htps=$(ps -aylC httpd |grep "httpd" |awk '{print $8'} |sort -n |tail -n 1)
mysme=$(ps aux | grep 'mysql' | awk '{print $6}' |sort -n |tail -n 1)
rafa=1024
nmysme=$(expr $mysme / $rafa)
nhtps=$(expr $htps / $rafa)
echo -e "\nTotal Memory = $tome"
echo -e "Largest httpd Process = $nhtps"
echo -e "Mysql Memory = $nmysme"
maxc=`expr $tome - $nmysme`
maxcl=`expr $maxc / $nhtps`
echo -e "\nSo, The MaxClients = $maxcl"
echo -e "(we can use nearest round of value from $maxcl)"

輸出結果

Total Memory = 490
Largest httpd Process = 6
Mysql Memory = 43

So, The MaxClients = 74
(we can use nearest round of value from 74)

參考 : http://blog.strictly-software.com/2013/07/apache-performance-tuning-bash-script.html

這一個會停用 apache

#!/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 httpd to calculate the amount of free memory
Starting httpd again
Total memory 490
Free memory 95
Amount of virtual memory being used 542
Largest Apache Thread size 23
Amount of memory taking up by MySQL 42
Virtual memory is ok
Total Free Memory 95
MaxClients should be around 4
MinSpareServers should be around 1
MaxSpareServers should be around 2
最後修改日期: 2014 年 07 月 25 日

作者

留言

撰寫回覆或留言

發佈留言必須填寫的電子郵件地址不會公開。