Apache VPS Optimization – Memory Usage Tuning
I’ve recently had the need to optimize my VPS plan so it won’t consume that much memory.
The main culprit was Apache with more then a few processes using over 50MB.
How to limit processes amount
First you need to download a little script I found called apachebuddy.pl by Gustav Maskowitz.
Running this script shows you how many processes are running and how much memory the average process consumes.
We are checking the service running on port 80
The process listening on port 80 is /usr/sbin/httpd
The process running on port 80 is Apache/2.2.15 (Unix)
Apache has been running 0d 0h 14m 20s
The full path to the Apache config file is: /etc/httpd/conf/httpd.conf
Apache is using prefork model
Examining your Apache configuration…
Apache runs as apache
Your max clients setting is 8
Analyzing memory use…
Your server has 490 MB of memory
The largest apache process is using 43.35 MB of memory
The smallest apache process is using 8.91 MB of memory
The average apache process is using 31.87 MB of memory
Going by the average Apache process, Apache can potentially use 254.97 MB RAM (52.04 % of available RAM)
Going by the largest Apache process, Apache can potentially use 346.81 MB RAM (70.78 % of available RAM)
### GENERAL REPORT ###
Settings considered for this report:
Your server’s physical RAM: 490MB
Apache’s MaxClients directive: 8
Apache MPM Model: prefork
Largest Apache process (by memory): 43.35MB
Your MaxClients setting is within an acceptable range.
Max potential memory usage: 346.8 MB
Percentage of RAM allocated to Apache [ OK ]
Using this data I’ve calculated how many processes I can run.
These are the new settings I have set in my httpd.conf:
# prefork MPM # StartServers: number of server processes to start # MinSpareServers: minimum number of server processes which are kept spare # MaxSpareServers: maximum number of server processes which are kept spare # ServerLimit: maximum value for MaxClients for the lifetime of the server # MaxClients: maximum number of server processes allowed to start # MaxRequestsPerChild: maximum number of requests a server process serves StartServers 2 MinSpareServers 1 MaxSpareServers 3 ServerLimit 8 MaxClients 8 MaxRequestsPerChild 1000
You need to save at least 128MB for your operating system
Don’t forget to tweak MySQL if you are using it