Apache optimize memory: Difference between revisions

From Skytech
Jump to navigation Jump to search
(Created page with "Category:Linux = Optimize apache based on memory and usage = Found this usefull, and copied everything. All credits to author from this page: https://telvps.com/clients/kno…")
 
No edit summary
 
Line 6: Line 6:
All credits to author from this page: https://telvps.com/clients/knowledgebase/25/HOW-TO-Optimize-Apache-for-Low-Memory-Usage.html
All credits to author from this page: https://telvps.com/clients/knowledgebase/25/HOW-TO-Optimize-Apache-for-Low-Memory-Usage.html


<pre>

1. SSH into your server as root.
1. SSH into your server as root.
2. Run top.
2. Run top.
Line 73: Line 73:


Once I've set these, I save the file, exit the editor, and restart apache. I've now reconfigured apache to meet my specific needs!
Once I've set these, I save the file, exit the editor, and restart apache. I've now reconfigured apache to meet my specific needs!
</pre>

Latest revision as of 14:56, 10 April 2012


Optimize apache based on memory and usage

Found this usefull, and copied everything.

All credits to author from this page: https://telvps.com/clients/knowledgebase/25/HOW-TO-Optimize-Apache-for-Low-Memory-Usage.html

1. SSH into your server as root.
2. Run top.
3. Press shift + m.
4. Note the highest RES memory used by httpd.
5. Hit Q to exit top.
6. Execute: service httpd stop
7. Once httpd is stopped, execute: free -m
8. Note the memory listed under "used".
9. Find the guaranteed memory for your VPS plan. Support can tell you how much you have guaranteed if you cannot find it.
10. Subtract the memory USED from the memory that your plan is GUARANTEED. This will give you your base FREE MEMORY POOL.
11. Multiply the value of your FREE MEMORY POOL by 0.8 to find your average AVAILABLE APACHE POOL (this will allow you a 20% memory reserve for burst periods). 
12. Divide your AVAILABLE APACHE POOL by the highest RES memory used by httpd. This will give you the MaxClients value that should be set for your system.
13. Open httpd.conf in your favorite text editor (don't use Windows text editors as they may break httpd.conf).
14. Set the MaxClients value you've calculated.
15. Set Keepalive Off if you don't need it (your server will handle more requests per second with keepalive on, but will require more memory for apache--don't turn on keepalive if you're not leaving a 20% memory reserve).
16. If you are leaving keepalive on, set keepalivetimeout to the lowest value you can to prevent connections from hanging. If you experience high latency to your server, set keepalivetimeout to 2-5 seconds.
17. Set your Timeout to a reasonable value. Pick a time that won't cut off the transfer of your pages to your customers, but keep it low enough that you don't have dead connections that remain open for a large period of time (10-30 seconds should be a good timeout if most of your users are on highspeed, and 30-120 should be ok for dialup).
18. MaxKeepAliveRequests should be set equal to the largest number of objects you have in 1 page. If you don't know, 70-200 should be good.
19. Set your MinSpareServers equal to 10-25% of MaxClients.
20. Set your MaxSpareServers equal to 25-50% of MaxClients.
21. Set your StartServers equal to either MinSpareServers or MaxSpareServers. When apache is restarted, this is the number of servers that will start and be ready for connections immediately. High-traffic sites should set this value to MaxSpareServers and lower volume sites should use MinSpareServrs.
22. MaxRequestsPerChild should be set somewhere between 500 (if you see rapid apache child process memory use growth) to 10000 (if you have no leaks in your applications). Setting this value to 0 will result in child processes never being killed, and eventually all shared memory used by apache will become "dirty" and unshared, possibly resulting in higher overall memory use. 
23. Once you've set all values, save the file, exit, and issue: service httpd restart. 

The following is an EXAMPLE with output:

Under TOP I notice that my biggest apache process is using 5mb of memory:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
15535 root 16 0 15072 5376 3196 S 0 0.5 0:17.60 httpd


-bash-3.00# service httpd stop
/etc/init.d/httpd stop: httpd stopped
-bash-3.00# free -m
total used free shared buffers cached
Mem: 1024 131 892 0 0 0
-/+ buffers/cache: 131 892
Swap: 0 0 0
-bash-3.00#

I see I'm using 131MB of RAM without apache. I have 384MB of RAM available.

384 - 131 = 253MB Free Memory Pool
253 * 0.8 = 202.4MB Available Apache Pool
200 / 5 = 40 

Armed with this information I now set up my httpd.conf with the following settings:

Timeout 10 (If it takes more than 10 seconds to load my pages, something is wrong)

MaxClients 40 (Note: I can set this lower if I know other processes will need more memory under load)
MinSpareServers 4 (My site gets almost no traffic)

MaxSpareServers 20 (My server never uses memory, so extra apache children hanging around doesn't hurt me)

StartServers 4 (again, low traffic, so I don't need to be handling an instantaneous burst on apache restart)

Keepalive On (Since I don't use much memory, I don't need to turn this off as the extra use won't hurt me)

MaxKeepAliveRequests 100 (I don't have 100 objects on any page, but using too low a value doesn't help performance, so I'll just do 100)

KeepAliveTimeout 1 (I don't have large files/pages, so I don't expect the remote computer to take more than 1 second to reply to each keepalive request)

MaxRequestsPerChild 10000 (I like the better performance with this, and I run no PHP Apps that leak memory) 

Once I've set these, I save the file, exit the editor, and restart apache. I've now reconfigured apache to meet my specific needs!