Already a customer? Please sign in.

Optimize Apache for WordPress

by Drew Strojny on January 7, 2009 / 15 comments

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.

If you’re looking for some tips on SSL performance with your WordPress site be sure to check out our guide on using SPDY and SSL. If you need a WordPress theme, be sure to check out our WordPress templates or read about how to find the best WordPress themes.

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 your httpd.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 leave KeepAlive turned on and make some additional tweaks.

If you decide to leave KeepAlive turned on it is important you change the default KeepAliveTimeout 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 increase MaxKeepAliveRequests. 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

Enjoy this post? Read more like it in Tutorials.

15 Comments

  1. 1moneymania

    Excellent write up. I was looking for a trick to optimize my Apache server for WordPress because I am testing WordPress with lots of plugins. After installing all the required plugins my website was very sluggish to load, these tips really did help speed it up.

    You are the man.

  2. Doesn't Matter

    The keep alive timeout was set to 15, All the time i had like 700-800 “requests currently being processed”…

    I tried setting the value 50 for KeepAliveTimeout and i noticed huge decrease in users online.

    Then i set the value to 5, more users were able to surf my site…!

    Thanks for the tips…:)

    Any more tips to reduce RAM usage?

    My server has 8 Gb RAM, and i get memory outage many times…
    The site is on wordpress, i have optimized lot of stuff already on wordpress, reduced the number of queries in theme, plugins, css, wp super cache, removed useless HTML code.
    like “”
    Removal of “post-id” mostly don’t effect the output but reduces one Mysql query.
    Most of the pages on the site have around 10 images.
    What can i do reduce RAM usage further?
    Thanks in advance…..

  3. Javid Jamae

    This is an excellent write up, thank you. Using these techniques really helped speed things up. The only problem I keep running into is that my WP sites run lightning fast when I start the server, then after a day or so, the slow down to a crawl and load very slowly. Non-wordpress pages / sites seem to load fast however. Do you have any ideas on how to debug this or any references on other things I can try to optimize? Thanks!

  4. Christoph

    Thx for this good wrap-up. I tried to optimize my apache before with several options, but every changed seemed to make it even worse. Not the pages render a bit faster. But the initial request to a page takes ages.

    Once it is in the browser cache, loading is lightning fast. I use the WP-Cache and WP-Super-Cache plugins (on different blogs). But it doesn’t really matter.

    Any ideas are highly appreciated.

    Thanks a million and keep it up!

  5. Drew Strojny

    Good question. Unfortunately, this cannot be done via htaccess. You will need to edit your Apache config file. The location of this file will vary depending on your Linux distribution.

  6. Chris Gomez

    I left the modules for last thinking they would be more work to sort through than the performance gain was worth. Wow.. was I wrong!

    I went from:
    3000ms min, 17000ms max @100 requests/50 concurrency

    to:
    2ms min, 35ms max @100 requests/50 concurrency

    Now, this is on my 3.04ghz p4/1gb RAM development box, but I’m going to implement on my SliceHost VPS later, and hope to see similar results. Thanks so much. This was a great introduction to Apache configuration for me. Invaluable!

  7. Matt Taylor

    The only loadmodule that I have in my httpd.conf is:

    LoadModule bwlimited_module modules/mod_bwlimited.so

    Shouldn’t there be more loadmodules?

    BTW, this is a great article. It was really easy to comprehend. Very well done. :)

  8. Drew Strojny

    Thanks Matt :) They might be located somewhere else, it depends on your Linux distribution.

  9. Ben

    Just bought a VPS to chuck a load of my affiliate WordPress blogs on and hopefully this will help me.

    Thank you very much Drew.

  10. Xav

    This was great but I found I needed to enable the following for WordPress on my host;

    LoadModule authnfilemodule modules/modauthnfile.so
    LoadModule authndefaultmodule modules/modauthndefault.so
    LoadModule authzhostmodule modules/modauthzhost.so
    LoadModule authzusermodule modules/modauthzuser.so
    LoadModule authzownermodule modules/modauthzowner.so
    LoadModule authzgroupfilemodule modules/modauthzgroupfile.so
    LoadModule authzdbmmodule modules/modauthzdbm.so
    LoadModule authzdefaultmodule modules/modauthzdefault.so

    Maybe something to do with either eAccelerator or running PHP as mod_php?

    Still, the prefork stuff worked a treat!

    Thanks!

  11. Arnold

    Thanks for this.

    I found out that the KeepAlive directive is the one that’s killing my server. Not exactly bringing my vps to a halt but more on throwing an error 500. After turning off KeepAlive, everything went smoothly.

Comments are closed.