Home > Programming, Servers & Scripts > Hosting, Servers & Security

Getting The Most From Your Apache Server With MaxClients (11)


03-08-2013 05:24 PM #1 caurmen (Administrator)
Getting The Most From Your Apache Server With MaxClients

If you're paying a good sum of cash for your server - VPS, dedicated, cloud, whatever - you want to get the most for your money.

Particularly if you're running an Apache server and you've never changed its configuration, the chances are it's not running anywhere near its maximum potential. In the past, I've been able to avoid several thousand dollars' worth of upgrades to key servers just by spending an hour tweaking some of the numbers in Apache's basic configuration settings.

Here's a quick tutorial on how to optimise your settings - particularly if you're finding that your server is becoming slow or even crashing, this stuff could save you upgrade fees or the cost of lost conversions. It's a bit of a pain, but it's not hard to do!

You'll need to have SSH access to your server's command line for this tutorial, and you'll also need to know the basics of Linux command line usage. Don't worry - you don't need super-sysadmin knowledge. If you can edit text files and change directories, you'll be fine.


How much performance can you get out of this?

For reference, I've been tuning a new Apache VPS using the approach in this tutorial whilst I was writing it.

After tuning, my 1Gb VPS running prefork Apache was able to handle 250 simultaneous clients, no timeouts and 7,200 hits in a minute without slowing below 40ms per request. That's on a live landing page.



I also tested it with 500 simultaneous users - that's the equivalent of about 180,000 clicks a day - and whilst the loading time spiked to a maximum of 400ms, the server handled the load fine.

Apache wouldn't be my first choice of web server software, but if you're using it, you can still get a lot of performance - and money - out of it.


Step One: Determine The Size Of Apache's Ass

Apache has a big butt. I can't lie. It does indeed look fat in that. In short, it eats memory like it's extra-heavy chocolate cake.

One of the most common ways that Apache will slow down is if it runs out of memory on the server. In that case, it'll still make new copies of itself in memory, but it'll start using hard disk space as "spare" memory, slowing it down by 20x or more.

To avoid that, we need to figure out how much memory we have available, and how much memory Apache needs.

SSH into your web server - whilst it's running Apache, Mysql, and everything else you use - and run

Code:
ps -ylC apache2 | awk '{x += $8;y += 1} END {print "Apache Memory Usage (MB): "x/1024; print "Average Process Size (MB): "x/((y-1)*1024)}'
That'll give you two numbers: overall memory usage ("Apache Memory Usage"), and how much memory each Apache process (copy of Apache, basically) is using, on average.

Now that you've got that number, you need to know how much memory everything else needs.

Run

Code:
free -m
You'll get a line that looks like this:



Don't Panic! You're nowhere near as short of memory as it appears. It's just Linux's confusing way of displaying memory. The line you want right now is this one:



That number shows how much memory is actually in use.

To figure out how much of that isn't Apache, subtract the Apache Memory Usage that you got above from the amount of memory in use on your server.

Take that number, and double it, just to be on the safe side. Let's call this Maximum Non-Apache Memory

Now, do this calculation:

(Total Server Memory - Maximum Non-Apache Memory) / Average Process Size (from above)

The number you get here - round down - is the number of copies of Apache you can safely run without your server making like a Slow Loris and becoming incredibly cute. Er, I mean, incredibly slow.


Step 2: Don't Worry, This Is The Easy Bit.

After all that calculation, you're probably pretty worried about what I'm going to ask you to do now.

Fear not, the hard bit's over.

First, we need to know what version of Apache you're running. Run the following command:

Code:
aptitude search apache2-mpm-
You'll get output like this:



Look for the version with "i" next to it. That's the one you're running. It'll almost certainly either be "apache-mpm-prefork" or "apache-mpm-worker"

Now:



You're done! Well, almost.

It's possible that something has gone wrong with your setup. To test this possibility, start a Blitz.io load test on your server, then run

top

Once Top is running, watch the numbers at the top of the screen - they'll look like this:



Watch the fourth line down, Mem. If "free" ever drops to 0, something's gone wrong. In that case, stop the blitz.io test, edit your httpd.conf again, reduce the number you put in by a fifth or so, restart Apache, and try again.

If you're feeling brave, you can also do the opposite - if you've still got free memory, go back, increase the numbers a bit, and run the test again.

Otherwise, congratulations, you've done the most important optimisation of Apache, and you're now getting close to the maximum performance from your web server.

I hope that was helpful! If you have any questions, if you're confused, if you have any suggestions for improving this tutorial, do let me know!


03-08-2013 05:47 PM #2 Oded Abbou (Member)

I'm definitely confused.. but i'll go through this thread 10 times until i'll get this sorted. the "small" things which makes you more money need to get full attention.

Is it relevant to litespeed servers..?

Good stuff, caurmen!


03-08-2013 05:54 PM #3 caurmen (Administrator)

Thanks!

This one's Apache only. Litespeed has a more complex performance architecture and also ships with more sensible defaults, so this tutorial's aimed at the low-hanging performance fruit of Apache servers.

However, elements of this tutorial - checking the memory usage of your web server and using Top - can apply to other web servers too.

What parts confused you? I'm more than happy to clarify and explain further.


03-08-2013 08:27 PM #4 sandyone (Member)

Thank you Dr Sheldon Cooper!


02-02-2014 05:52 PM #5 kash50 (Member)

Tying to optimize my server. When I execute the following code:

Code:
ps -ylC apache2 | awk '{x += $8;y += 1} END {print "Apache Memory Usage (MB): "x/1024; print "Average Process Size (MB): "x/((y-1)*1024)}'
I receive the following messages:

Code:
Apache Memory Usage (MB): 0
awk: (FILENAME=- FNR=1) fatal: division by zero attempted
Not sure what I am doing wrong.


02-02-2014 06:45 PM #6 kyleirwin (Member)

Quote Originally Posted by kash50 View Post
Tying to optimize my server. When I execute the following code:

Code:
ps -ylC apache2 | awk '{x += $8;y += 1} END {print "Apache Memory Usage (MB): "x/1024; print "Average Process Size (MB): "x/((y-1)*1024)}'
I receive the following messages:

Code:
Apache Memory Usage (MB): 0
awk: (FILENAME=- FNR=1) fatal: division by zero attempted
Not sure what I am doing wrong.
You're not doing anything "wrong", you're likely on a different OS than the tutorial was written for.

The Apache process is apache2 on Debian based linux distributions, and httpd on Red Hat based distros.

Try ps -ylC httpd | awk '{x += $8;y += 1} END {print "Apache Memory Usage (MB): "x/1024; print "Average Process Size (MB): "x/((y-1)*1024)}' instead.


02-02-2014 07:39 PM #7 kash50 (Member)

Thanks that works out great.

Another question is it correct to set MinSpareServers, MaxSpareServers to the same value?


02-02-2014 10:31 PM #8 BeyondHosting-Tyler (Member)

Apache has terrible performance even when max clients are turned up. It can serve static content ok but any thing dynamic (ie php... everything server side scripted) is terrible!

Just the other day I was helping migrate a customer that had a 32 core server with apache maxed out and a load average of 3500!

I installed litespeed 1 cpu and lit it up, load dropped to 2 and everything began loading instantly.

Apache is good for basic web applications and sites, but its not a solution if you want to make money.


02-03-2014 05:05 PM #9 caurmen (Administrator)

@kash50 -Yep, that's probably the best option. They're unlikely to have a huge effect on your performance most of the time.


02-27-2014 06:50 PM #10 panicore (Member)

Just a small question Caurmen, didn't want to create a new thread for it. Are you running APC on your server in combination with memcache?


02-28-2014 12:09 PM #11 caurmen (Administrator)

@panicore - Depends on which server, but usually yes. I haven't done a full benchmark with and without APC, but using some kind of opcode cache is good practise in general.


Home > Programming, Servers & Scripts > Hosting, Servers & Security