search
date June 8th, 2008  written by E.Hallander  categories Apache, Linux, Security

Figured I might as well start with the pedal to the metal with this whole concept. This is part one of what will (hopefully) be a comprehensive guide to how to maximize performance on your Apache based web server. Not just httpd though, but also general server memory optimization as well as tips and tricks to make mySQL run as smooth as possible. Enjoy!

Apache tuning:

One of Apaches biggest problem is memory imprint on the server as well as (at least in my opinion) a few “out of the box” mis-configurations. Let’s start with processes and modules. Apache has these following modules enabled by default in /etc/httpd/conf.d/ :

  • manual.conf: This configuration file allows the manual to be accessed through your server (not crucial)
  • perl.conf: Allows Apache web server to directly execute Perl code (crucial if you use Perl)
  • php.conf: PHP4/5 Module (crucial if you use PHP)
  • proxy_ajp.conf: Adds support for proxying to a back-end server such as Tomcat (not crucial)
  • python.conf: Allows Apache handlers to be written in Python (crucial)
  • squid.conf: Access to squid cache manager (crucial)
  • ssl.conf: Apache SSL server configuration (crucial)
  • webalizer.conf: Webalizer stats configuration (not crucial)
  • welcome.conf: Enables the default “Welcome” page if there is no default index present (not crucial)

So, all of these are loaded by default and they all take their toll on memory imprint. Just go ahead and disable the ones you feel aren’t used in your setup;
With Debian/Ubuntu:

a2dismod webalizer

And to enable again:

a2enmod webalizer

With centOS/Fedora/Red Hat/Slackware you can just rename the modules you don’t need :

cd /etc/httpd/conf.d/
mv webalizer.conf webalizer.nothx

Worth noting that for any change you need to do a restart of the httpd service to activate it. But we’re not done yet ;)

httpd configuration

There’s a lot of settings in the httpd.conf that makes a huge difference in how your server behaves. This section will include a massive block of text, but bear with me. It’ll be worth it in the end.

Start by backing up your current configuration (Don’t skip this step! :D):

cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.bkup

Edit the file (click here if you need a quick brush-up on how to use the linux editor Vi)

vi /etc/httpd/conf/httpd.conf

Find this line:

# Timeout: The number of seconds before receives and sends time out.
Timeout 120

This is the amount of time the server will wait before failing a request. 120 is a bit unreasonable, so change it to something like 20
Next, look up your current settings for these three connected variables;

KeepAlive
KeepAliveTimeout
MaxKeepAliveRequests

KeepAlive is default off, which is normally not that great. KeepAlives lets users send multiple requests on an already open connection. This is especially great for sites that either have a lot of images or a lot of sequential database calls. The flipside is that a connection will be kept busy doing nothing waiting for more requests on the already open connection. The default KeepAliveTimeout of 15 seconds attempts to minimize this effect, but 15 seconds is quite unreasonable. MaxKeepAliveRequests is basically the amount of requests a user can do on a connection before it needs to be renewned. Change these to:

KeepAlive On
KeepAliveTimeout 2
MaxKeepAliveRequests 80

Up next is settings for the MPM module prefork. Prefork is enabled by default and I don’t really suggest you change this unless you have absolutely no intention of using PHP or Perl. These are the default settings:

<IfModule prefork.c>
StartServers 1
MinSpareServers 1
MaxSpareServers 20
ServerLimit 20
MaxClients 150
MaxRequestsPerChild 4000
</IfModule>

Now, these settings depend heavily on the amount of RAM your server has available. MaxClients is an especially crucial value to your servers performance under stress. If it’s too low, resources will go to waste. Too high and an a sudden burst connections will bring the server crawl. I personally find this to be a general setting that works well on a broad array of server setups (but if you pack a lot of ram, you might want to beef it up a bit):

StartServers 1
MinSpareServers 1
MaxSpareServers 4
ServerLimit 50
MaxClients 50
MaxRequestsPerChild 4000

Apache has a module that’s loaded by default called mod_deflate. This module is loaded, but not ACTIVATED by default. What it does is compress files before it sends them out, to save bandwidth and improve response times. You can reduce bandwidth usage by up 50% and improve response time dramatically by using mod_deflate.

LoadModule deflate_module modules/mod_deflate.so
<Location />
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml application/x-javascript
</Location>

With all these new, cool memory saving features in the game, and your conf file edited, you can now restart your httpd service with;

/etc/init.d/httpd restart

That’s the end of part 1. Next part will discuss mySQL optimization and performance :D



Feel free to check out the feed or leave a trackback from your own site.

5 Responses to “Web server performance guide, part 1/3”

Post A Comment

Site served by; centOS5, mysql 5.1 & php 5.2.6
'If it ain't broken, it must at least be due for a service?'