After enabling a few plugins, the WordPress installation started feeling a little sluggish. With a little bit of googling you’ll see this is a common complaint with WP sites. While I don’t have to worry about having anything digged, apart from possibly some embarassing photos, it’s still a fun project to optimize your server which lead me onto tonights project.
There’s a few things you can do to get the most of your Apache installation, although it’s highly tuned out of the box.
You might want to consider disabling some unused modules, if you have more than you need running and consuming resources. All I did here was clean out my virtual host configuration files to improve readability. I removed the cgi-bin and doc references as they’re not needed.
Enable content caching of static content
What do I mean by static content? Static content can be classed as resources that are not created dynamically, like images, text files, CSS whilst dynamic content would be things like PHP or ASP scripts.
First of all we need to enable the Apache module ‘cache’ by running:
Add the following to either your main Apache configuration file, or a virtual host file to enable in-memory caching of static content for some of the common cacheable resource types (HTML,CSS,JPEG,GIF)
CacheEnable mem /
ExpiresDefault "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/html "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
For a syntax guide see the mod_expires documentation and go to wikipedia for a list of MIME types.
Put your pages on a diet with mod_deflate
Mod deflate is the Apache module responsible for compressing your static content before it’s transmitted down the wire to browsers that are capable of handling compressed files (most are).
On the one hand this will increase CPU usage but if you’ve got the spare cycles, and it’s not like it’s going to bring your server to it’s knees, then you should probably do this as it should mean reduced bandwidth usage on your site, and faster response times.
Make sure Apache still sends out ‘304 Not Modified’ HTTP headers
A problem with using mod_deflate is that it appends -gzip to the end of any E-Tags outside of the quoted string, contrary to the HTTP specification. This means that we always receive 200OK responses to our GET requests along with the full body content, effectively bypassing the caching. What we really want is the server to return a ‘304 Not Modified’ response, meaning it doesn’t need to send the content again.
To accomplish this, we need to add the following rules to our apache.conf:
RequestHeader edit "If-None-Match" "^(.*)-gzip$" "$1"
Header edit "ETag" "^(.*[^g][^z][^i][^p])$" "$1-gzip"
Side note: this also means you need to enable the module ‘headers’ with:
E-Accelerator, formerly Turk MMCache, improves the performance of your PHP scripts by caching them in a compiled state in shared memory meaning they don’t have to be recompiled every time a page is requested.
Install WP Super Cache plugin
This one is pretty self explanatory… you just need to install the plugin from your administrative control panel. Well okay, there are a few extra steps. Make sure you have ‘permalinks’ enabled under “Settings -> Permalinks” and then enable the cache in the “WP Super Cache” settings page.
This should drastically speed up your site. It creates static pages for all of your lovely WordPress content, and serves that with an .htaccess rule rather than regenerating the content on the fly, reducing CPU load and page load times.
You might be wondering why we need to do this if we have enabled caching in Apache. Well, the reason is that Apache can’t cache dynamically generated content – that is, content generated by a scripting language like PHP. WP-Supercache creates static versions of our WordPress pages that can be cached, and also saves the script from having to be recompiled in PHP.