How to optimize and speed up WordPress

If you’ve been reading the web recently you might already noticed that your site loading speed affect your search rankings as written by Google Websearch team. Although previously i’ve wrote about the same topic in the past, i decided that perhaps a rewrite with proper formatting would be better and although the title says WordPress, most of the instructions listed here can be applied to non WordPress based sites too

And before i begin, i need to mention that in order to use this you need to enable specific Apache Module or it’s not going to work at all. And i choose to not include the conditional check to make sure that the rule being applied is working as it should (assuming that you’ve typed everything correctly) or else you’ll get 500 Internal Server Error Message (if the required module is not enabled or being disabled by your web host) which is going to be useful especially if you’re in Shared Hosting. But unfortunately if you’re on shared hosting some of the tips here can’t be applied because of the limitation from shared hosting itself

And with the above said, let’s begin the WordPress Optimization ;)

Enable Deflate / Gzip

If you have full control of your server (VPS or Dedicated) or sure that mod_deflate and mod_setenvif is installed and enabled by your web host (Shared Hosting), by using this method you can reduce your page size and in the end your visitor will benefit from faster loading times due to smaller page size and all you have to do is just put this in your .htaccess file

# Feel free to add more mime type as needed
AddOutputFilterByType DEFLATE text/plain text/html text/css text/javascript application/x-javascript

Enable WordPress Query Cache

By using query cache you can reduce the number of queries used, and in short you can get the benefit of having faster page load while at the same time reducing load on your server

If you’re on shared hosting, then all you have to do is just download W3 Total Cache or you can also use other cache plugins as long as they offer Object cache option

Using mod_expires to Cache Static Files

If you have frequent visitors that came to your site and they are not the type that automatically clear their browser cache whenever they close their browser, then using Far Future Expiration will definitely help your site to load faster for them, but obviously if they are the type that will automatically clear everything when their exit their browser then this tip will do nothing to help your site performance (feel free to check them at Yuiblog here and here)

In order to do this, you’ll need to have mod_expires enabled (in most case it should already be enabled), and you can put below code in the httpd.conf or if you don’t have access to apache configuration file, then you can use .htaccess method but you don’t have to worry because this code is compatible whether it is placed in httpd.conf or .htaccess file

And here’s the code, you can put it in the .htaccess file that is located in your main directory / root directory or you can also put it in the httpd.conf (below code can be used directly unless you want to change it)

ExpiresActive On
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType text/javascript A2592000
ExpiresByType application/javascript A2592000
ExpiresByType application/x-javascript A2592000
ExpiresByType text/css A2592000

And here’s the simple explanation on what the above code do:

ExpiresByType image/gif A2592000 means any Gif Image Mime-Type will expire after 2592000 seconds or 720 hour in clients (visitors) cache after being accessed

But what if you don’t like it to expire for about 720 hour? then simple you can always change 2592000 seconds into something else you want or better if you don’t want to count how many hour xxxx seconds is, then you can always type it like below (note: below code is just an example you should change it to a far future expiry)

ExpiresActive On
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 day"
ExpiresByType image/jpg "access plus 1 hour"
ExpiresByType image/jpeg "access plus 10 minutes"
ExpiresByType text/javascript "access plus 1 month 10 days"
ExpiresByType application/javascript "access plus 10 days 5 minutes"
ExpiresByType application/x-javascript "access plus 1 month 20 days"
ExpiresByType text/css "access plus 1 month 1 day"

Obviously the above example is much easier to read, right? so feel free to choose on which method do you want to use ;) and for more info you can read Apache mod_expires manual

Disable ETags

From Wikipedia:

An ETag (entity tag) is part of HTTP, the protocol for the World Wide Web. It is a response header that may be returned by an HTTP/1.1 compliant web server and is used to determine change in content at a given URL. When a new HTTP response contains the same ETag as an older HTTP response, the client can conclude that the content is the same without further downloading. The header is useful for intermediary devices that perform caching, as well as for client web browsers that cache results. One method of generating the ETag is based on the last modified time of the file and the size of the file, another is using a checksum.

And according to Yuiblog

If you're not taking advantage of the flexible validation model that ETags provide, it's better to just remove the ETag altogether. The Last-Modified  header validates based on the component's timestamp. And removing the ETag reduces the size of the HTTP headers in both the response and subsequent requests.

And fortunately it’s really simple on how to remove / disable etags completely from being sent, all you have to do is just put this in your httpd.conf (if you have root access) or put it in your .htaccess file that is located in your root directory

FileETag None

But what if you want to disable Etags for specific filetype only, like images for example? then use below code:

<FilesMatch "\.(gif|jpe?g|png)$">
FileETag None

Enable MySQL Query Cache and Tweak MySQL

By enabling MySQL Query Cache and also tweaking MySQL you can improve overall WordPress performance because WordPress relies on MySQL (other than PHP of course)

But unfortunately you’ll need root access which is not available on shared hosting. So in order to perform this you’ll need at least VPS (but i’m sure most shared hosting already enabled query cache)

In order to do this you’ll need to edit my.cnf file and add / adjust query_cache_type, query_cache_limit, query_cache_size and query_cache_min_res_unit but because there's no perfect rule here, you'll need to adjust it by yourself

Use PHP Opcode Cacher / PHP Accelerator

By using PHP Opcode Cacher you get the benefit of having reduced load on your server and also faster execution time of PHP Scripts

Unfortunately if you’re on shared host you’re out of luck on this one too because you’ll need root access in order to install a PHP Accelerator / PHP Opcode Cacher

But if you’re on VPS or Dedicated server then all you have to do is just choose between APC, Xcache, or eAccelerator (those three are the most known and most used PHP Accelerator)

Convert to Static from Dynamic

This is actually is optional (in my opinion) because i never used it for obvious reason and that is i don’t have that many visitors per day, but when you have tons of visitors per day or your site getting slashdotted or dugged (well i’ve never experienced any of them) then the only thing you need is to reduce load on your server to almost next to nothing and that is done by simply converting your dynamic site (php in this case) to static site (pure html)

The easiest way to get into this mode is you just have to choose between WP Super Cache or W3 Total Cache

But of course if your server / host still can’t cope up with the demand you can always use CoralCDN

Optimize images for webpages

Smaller images means faster page loading time, and so in order to make your website load as fast as possible to your site visitors, you can try to optimize it a little bit further

My recommendation for this section are jpegoptim and optipng, you can use it on your server (if you have root access) or you can also use it from your own computer before uploading images to your web server

Obviously this part can be considered as generic website optimization especially to those who use many images on their site

Obviously …

There are many other methods to optimize your blog even more like tuning Apache, using reverse proxy, change unneeded call to MySQL like displaying blogname into a static one, disabling unneeded plugins, removing those useless image in sidebar that makes your blog looks like MySpace, etc.


Note: Comment may not appear right away.

4 comments on “How to optimize and speed up WordPress

  1. I never thought about compressing anything. My site is using lightbox so it has javascript, and I didn’t even realize that perhaps it would be allocating server resources. I just did the Gzipping so I’ll see if any thing is reduced on the server and if I notice an speed increases!

    1. Actually if your site have many javascripts then you don’t need to worry about using many server resources because javascript is parsed at the client side ;) but of course by compressing them you can reduce the filesize and in the end your visitors will get faster pageload and less bandwidth usage on your site because of the reduced file size but depending on the method you choose there will be an increased in cpu usage