Tuesday, August 01, 2006

A couple of Apache performance tips

I had to troubleshoot an Apache installation recently. Apache 2.0 was running on several Linux boxes behind a load balancer. If you ran top on each box, the CPU was mostly idle, there was plenty of memory available, and yet Apache seemed sluggish. Here are a couple of things I did to speed things up.

1. Disable RedirectMatch directives temporarily

All the Apache servers had directives such as:

RedirectMatch /abc/xyz/data http://admin.mysite.com/abc/xyz/data

This was done so administrators who visited a special URL would be redirected to a special-purpose admin server. Since the servers were pretty much serving static pages, and they were under considerable load due to a special event, I disabled the RedirectMatch directives temporarily, for the duration of the event. Result? Apache was a lot faster.

2. Increase MaxClients and ServerLimit

This is a well-known Apache performance optimization tip. Its effect is to increase the number of httpd processes available to service the HTTP requests.

However, when I tried to increase MaxClients over 256 in the prefork.c directives and I restarted Apache, I got a message such as:

WARNING: MaxClients of 1000 exceeds ServerLimit value of 256 servers, lowering MaxClients to 256. To increase, please see the ServerLimit directive.

There is no ServerLimit entry by default in httpd.conf, so I proceeded to add one just below the MaxClients entry. I restarted httpd, and I still got the message above. The 2 entries I had in httpd.conf in the IfModule prefork.c section were:

MaxClients 1000
ServerLimit 1000

At this point I resorted to all kinds of Google searches in order to find out how to get past this issue, only to notice after a few minutes that the number of httpd processes HAD been increased to well over the default of 256!

UPDATE 03/06/09: It turns out that the new MaxClient and ServerLimit values take effect only if you stop httpd then start it back again. Just doing a restart doesn't do the trick...


So, lesson learned? Always double-check your work and, most importantly, know when to ignore warnings :-)

Now I have a procedure for tuning the number of httpd processes on a given box:

1. Start small, with the default MaxClients (150).
2. If Apache seems sluggish, start increasing both MaxClients and ServerLimit; restart httpd every time you do this.
3. Monitor the number of httpd processes; you can use something like:

ps -def | grep httpd | grep -v grep | wc -l

If the number of httpd processes becomes equal to the MaxClients limit you specified in httpd.conf, check your CPU and memory (via top or vmstat). If the system is not yet overloaded, go to step 2. If the system is overloaded, it's time to put another server in the server farm behind the load balancer.

That's it for now. There are many other Apache performance tuning tips that you can read from the official Apache documentation here.

11 comments:

Anonymous said...

If you can disable .htaccess files, you'll also eliminate a directory walk of stat() calls searching for .htaccesses along the way... a /home/user/html/a/b/c/test.html path results in...

stat(/home/.htaccess)
stat(/home/user/.htaccess)
stat(/home/user/html/.htaccess)
stat(/home/user/html/a/.htaccess)

...and so on.

Anonymous said...

You could place ServerLimit in httpd.conf, outside <Ifmodule somethong-PMP-conf> e.g, under KeepAliveTimeout

Anonymous said...

ups, I mean below KeepAliveTimeout line

lbruno said...

That pipeline can be simplified:

grep [h]ttpd

lbruno said...

I mean: instead of grepping for httpd in the process list and having to filter out grep itself, you can use a regexp:

ps -def | grep httpd | grep -v grep

That would become:

ps -def | grep [h]ttpd

Shahryar Ghazi said...

thanks for sharing this with us

TrutherD said...

On a hunch I put ServerLimit BEFORE MaxClients and it worked without a warning. :)

Anonymous said...

'ps aux | grep -c httpd' does it for me.

Cheers,
Aadil.

venkat said...

Actual fix: You need to put the Serverlimit option before the maxclient in the apache config file.

Example config:

IfModule prefork.c

StartServers 20
MinSpareServers 5
MaxSpareServers 20

ServerLimit 1024 <====------ moved this line here rather than after the
Maxclients (otherwise Apache will gives error, "WARNING: MaxClients of 1000
exceeds ServerLimit value of 256 servers, lowering MaxClients to 256. To
increase, please see the ServerLimit directive")

MaxClients 512 <====-------- changed from 256 to 512

MaxRequestsPerChild 10000

IfModule





--Venkat

irctc pnr said...

I have 7GM RAM , 8 core processor.
below settings are ok ?

StartServers 100
MinSpareServers 100
MaxSpareServers 450
ServerLimit 550
MaxClients 550
MaxRequestsPerChild 20000

jsmoriss said...

A few weeks ago I needed to tune an apache web server for a client, and although the process is straight-forward (see memory used by processes, server memory available, etc.), it's also a bit tedious. I expected to find a ready made application / script to do the number crunching, but my search didn't turn up anything, so I rolled my own. ;-) I figured you might find it useful too. You can find the weblog entry here https://surniaulula.com/2012/11/09/check-apache-httpd-mpm-config-limits/ and the script is on Google Code as well.

Enjoy!

js.

Modifying EC2 security groups via AWS Lambda functions

One task that comes up again and again is adding, removing or updating source CIDR blocks in various security groups in an EC2 infrastructur...