Menu

Optimize Apache for WordPress

If you made the decision to move your WordPress install from shared hosting to a shiny new VPS you should consider optimizing Apache by making some tweaks to your httpd.conf file. Apache is a fast, reliable, and flexible server but is heavy on resources by default. If you are running a small VPS, and using it just for WordPress, you can make some small tweaks to your configuration and get some significant performance gains.

Trim Down Those Modules

Open up the httpd.conf file on your server in your favorite text editor and look for a bunch of lines starting with LoadModule. It should look something like this:

# Example: # LoadModule foo_module modules/mod_foo.so # LoadModule auth_basic_module modules/mod_auth_basic.so LoadModule auth_digest_module modules/mod_auth_digest.so LoadModule authn_file_module modules/mod_authn_file.so LoadModule authn_alias_module modules/mod_authn_alias.so

The actual list will be much longer but you get the idea. You can disable these modules by “commenting them out”. Which essentially amounts to adding a # in front of the line like so:

# LoadModule auth_basic_module modules/mod_auth_basic.so

You are going to disable a bunch of these modules that a basic WordPress website does not require. In fact, I want you to comment out every module EXCEPT FOR the following:

LoadModule authz_host_module modules/mod_authz_host.so 
LoadModule log_config_module modules/mod_log_config.so
LoadModule expires_module modules/mod_expires.so 
LoadModule deflate_module modules/mod_deflate.so 
LoadModule headers_module modules/mod_headers.so 
LoadModule setenvif_module modules/mod_setenvif.so 
LoadModule mime_module modules/mod_mime.so 
LoadModule autoindex_module modules/mod_autoindex.so 
LoadModule dir_module modules/mod_dir.so 
LoadModule alias_module modules/mod_alias.so LoadModule rewrite_module modules/mod_rewrite.so

One of the lines you probably commented out was this one:

LoadModule negotiation_module modules/mod_negotiation.so

This provides some functions for auto-completing mistyped URL’s that you don’t really need. It also is required if you are serving multilingual pages. If that is the case then DO NOT comment out the above module.

If you did in fact comment out the mod_negotiation.so module you will need to make a couple more tweaks. Find these two lines (they are in different locations) and comment them out:

LanguagePriority en ca cs da de el eo es et fr he hr it ja ko ltz nl nn no pl pt pt-BR ru sv zh-CN zh-TW
ForceLanguagePriority Prefer Fallback

You should see a significant performance boost after disabling those modules. They were eating up a good size chunk of your limited system resources.

Apache MPM Prefork Module

Another handy tweak is to make some adjustments to the Apache MPM prefork module. This is assuming you are using Apache in prefork mode, which is highly recommended and likely if you are on a small VPS.

This module controls the number of processes and spare processes Apache will start and run. This is especially important if you are running a small VPS that is handling MySQL and Apache. Unless you are getting slammed with really heavy traffic on a regular basis (in which case you should be on a dedicated server) there is no need to be running the default configuration. Find these lines in yourhttpd.conf file:

<IfModule prefork.c> StartServers 8 MinSpareServers 5 MaxSpareServers 20 ServerLimit 256 MaxClients 256 MaxRequestsPerChild 4000 </IfModule>

MinSpareservers and MaxSpareServers control the number of spare processes your webserver is permitted to run and StartServers controls how many are started by default.

ServerLimit controls the maximum configured value for MaxClients. Reducing MaxClients on a webserver that is serving dynamic content (e.g. WordPress) can make a big difference. If you experience a traffic spike on your VPS and your MaxClients is set too high your server will more than likely get stuck in an endless loop of swapping pages from physical memory to virtual memory, commonly referred to as thrashing. The accepted way of calculating an appropriate MaxClients value is dividing your total available system memory by the size per Apache process. For example, if you had a 500MB left for Apache to use and each Apache process was using around 10MB you would set your MaxClients to (512-12) / 10 = 50. To check real time memory usage on your VPS use top.

MaxRequestsPerChild limits the number of requests a child server will handle during it’s life. We can safely reduce this value and realize a small gain.

So let’s go ahead and pare down those values:

<IfModule prefork.c> StartServers 3 MinSpareServers 3 MaxSpareServers 10 ServerLimit 50 MaxClients 50 MaxRequestsPerChild 2000 </IfModule>

Remember these are not concrete “best” values, they depend on the size of your VPS and how small or large you Apache process is.

Optimize Your KeepAlive

KeepAlive allows your visitors to issue multiple requests over the same TCP connection, in theory this helps improve latency because your visitors can request your webpage, images, and javascripts all over one connection. Unfortunately, Apache must use a worker process to service each and every request. The worker process stays busy servicing each request for a full 15 seconds by default, even if your visitor is no longer using it! This means you have less worker processes available on your system at any given time. With the limited system resources you have on your small VPS we always want open worker processes to be actually working. One way of accomplishing this is turning off KeepAlive. Find this line in your httpd.conf file:

KeepAlive On

and change it to:

KeepAlive Off

If you have a site with lots of images and javascripts it is usually better leaveKeepAlive turned on and make some additional tweaks.

If you decide to leave KeepAlive turned on it is important you change the defaultKeepAliveTimeout value. This prevents unused connections from staying open for excessive amounts of time. Find this line in your httpd.conf file:

KeepAliveTimeout 15

You want to leave this connection open for 2 seconds, just long enough for the client to request most if not all of the necessary files. So change that line to:

KeepAliveTimeout 2

If you are going to leave KeepAlive on you will want to increaseMaxKeepAliveRequests. Setting this higher allows more requests per connection and increases efficiency. Find this line:

MaxKeepAliveRequests 100

and change it to:

MaxKeepAliveRequests 200

Adjust Timeout

Another minor tweak that will give you a small performance boost as well as help reduce the effects of a DOS attack is changing the TimeOut Directive. This directive tells Apache how many seconds to wait while receiving an incoming request, processing it, and sending back a response. Find this line:

Timeout 120

and change it to:

Timeout 40

Restart Apache and Enjoy!

Now restart Apache and enjoy your performance boost! Always remember to backup your httpd.conf file in case something goes wrong. Keep in mind this is just a sample configuration and we aren’t responsible if it breaks something on your server.

Additional Resources

Facebook Comments