问题
We are on 16GB AWS instance and I am finding it to be really slow. When I ran
ps -aux | grep apache
I can see about 60+ apache processes.
When I ran
watch -n 1 "echo -n 'Apache Processes: ' && ps -C apache2 --no-headers | wc -l && free -m"
It is showing almost all memory being used by apache.
When I ran
curl -L https://raw.githubusercontent.com/richardforth/apache2buddy/master/apache2buddy.pl | perl
to see how to optimize Apache, it suggested me to increase number of MaxRequestWorkers so I made it 550. I also changed MaxConnectionsPerChild from 0 (unlimited) to 10000.
Here is my /etc/apache2/mods-enabled/mpm_prefork.conf file
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 550
MaxConnectionsPerChild 10000
</IfModule>
Can you tell me how can we optimize apache memory usage so it don't bring down the whole site down ?
回答1:
I had a similar problem with an instance in EC2 and here's what I did and would suggest:
If you are using prefork, make sure that the module is loaded by typing these two commands
apache2 -l
andsudo apache2 -M
If you can see the prefork module loaded in the results of either of these two commands then up to the next step. Otherwise, make sure to load it first or else you would be changing the configurations for nothing.Run this command to find the average memory each apache2 process is using
ps aux | grep 'apache2' | awk '{print $6/1024;}' | awk '{avg += ($1 - avg) / NR;} END {print avg " MB";}'
Call that valuex
Restart your apache server by using
sudo service apache2 restart
and take a note of how much free memory you have. What I did was subtract an extra200MB-500MB
cushion from that free memory to be used later. Call that valuey
Divide the value of free memory
y
over the amount of memory used per processx
and that would be the value ofMaxRequestWorkers = y/x
As for the value of
MaxConnectionsPerChild
then you can tweak it till you get the right configuration. It you make it too big, then the process will keep using more and more memory before being killed. If you make it too small, then the processes will die too quickly and that will present an overhead on your system. I usually keep it somewhere between4000
and10000
.Some of these steps have been taken from the accepted answer in the following link: StackExchange: httpd memory usage where one solution also suggested disabling some of the modules if you don't need them.
I would suggest you do steps 1-5 first and see if that solves your problem!
Good luck!
来源:https://stackoverflow.com/questions/41015166/apache-using-all-16-gb-memory-how-to-limit-its-processes-and-memory-usage