https://torbjornzetterlund.com/wp-content/uploads/2015/05/cof_orange_hex_400x400-e1430750445296-960x600_c.jpg

How to speed up page load for your WordPress blog


As part of launching a new WordPress website, you need to do some optimization from the standard installation and give your WordPress improved performance  improve page loads. There are a number of ways you can do this, I will in this article take you through the options that are available to you. There might be something that fits your configuration, I also will tell you what I did to improve my wordpress website.

WordPress is written in PHP and PHP is a server-side scripting language designed for web development – PHP functions used in plugins and application can slow your site down, to improve PHP execution you can add a PHP Accelerators (OPCaches) as seen on this Wikipedia list.

These products typically are drop in; no code change instant performance boost. With large code bases (Drupal, WordPress) the performance can be up to 3x better while lowering response time and memory usage.

Data Caching

Memcache – you might think of it as a lightweight key value system that can be scaled to multiple servers. Software has to be enhanced to support Memcache, and it solves certain problems better than others. If you had a list of real time stock values on your website, you might use Memcache to keep a resident list of the current value that is displayed across your website. You might use it to store session data for short term reuse. You wouldn’t use it for other things such as full-page caches, or as a replacement for MySQL.

There are also WordPress addons such as WP-Super-Cache that can drastically improve WordPress’ performance (infact, WP-Super-Cache can rival static HTML based sites in many cases).

HTTP accelerator

Varnish is an HTTP accelerator and a useful tool for speeding up a server, especially during times when there is high traffic to a site. It works by redirecting visitors to static pages whenever possible and only drawing on the virtual private server itself if there is a need for an active process.

What I did in the end

Choosing one solution is not enough you would need to implement several performance improvements, in the end I choose to go with Opcache which is part of PHP 5.5  and Varnish for HTTP acceleration, I also have WP-Super-Cache plugin active on one of my site in my WordPress Multi Site installation.

I’m running Ubuntu Opcache is now standard with Ubuntu 14.04. The first step is to enable Opcache – you do that in php.ini

sudo nano /etc/php5/apache2/php.ini

Change:

;opcache.enable=0 
to 
opcache.enable=1
;opcache.memory_consumption=64 
to 
opcache.memory_consumption=128
;opcache.max_accelerated_files=2000 
to 
opcache.max_accelerated_files=4000
;opcache.revalidate_freq=2 
to 
opcache.revalidate_freq=60

Next, make sure the php mod is enabled:

php5enmod opcache

Finally restart Apache:

sudo service apache2 restart

To perform the steps in this tutorial, you will need to both have a user with sudo privileges and apache installed on your virtual private server.

Install Varnish

The varnish site recommends installing the varnish package through their repository.

You can start that process by grabbing the repository:

sudo curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -

The next step is to add the repository to the list of apt sources. Go ahead and open up that file.

sudo nano /etc/apt/sources.list

Once inside the file, add the varnish repository to the list of sources.

deb http://repo.varnish-cache.org/ubuntu/ lucid varnish-3.0

Save and exit.

Finally, update apt-get and install varnish.

sudo apt-get update
sudo apt-get install varnish

Configure Varnish

Once you have both apache and varnish installed, you can start to configure them to ease the load on your server from future visitors.

Varnish will serve the content on port 80, while fetching it from apache which will run on port 8080.

Let’s go ahead and start setting that up by opening the /etc/default/varnish file:

sudo nano /etc/default/varnish

Uncomment all of the lines under “DAEMON_OPTS”—under Alternative 2, and make the configuration match the following code:

 DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"

Once you save and exit out of that file, open up the default.vcl file:

sudo nano /etc/varnish/default.vcl

This file tells varnish where to look for the webserver content. Although Apache listens on port 80 by default, we will change the settings for it later. Within this file, we will tell varnish to look for the content on port 8080.

The configuration should like this:

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

Configure Apache

So far we have told varnish that apache ports will be running on 8080. However the default settings for apache are still on port 80. We will correct the discrepancy now. Open up the apache ports file:

sudo nano /etc/apache2/ports.conf

Change the port number for both the NameVirtualHost and the Listen line to port 8080, and the virtual host should only be accessible from the localhost. The configuration should look like this:

NameVirtualHost 127.0.0.1:8080
Listen 127.0.0.1:8080

Change these settings in the default virtual host file as well:

sudo nano /etc/apache2/sites-available/default

The Virtual Host should also be set to port 8080, and updated line looks like this:

 <VirtualHost 127.0.0.1:8080>

Save and exit the file and proceed to restart Apache and Varnish to make the changes effective.

sudo service apache2 restart
sudo service varnish restart

Accessing your domain should instantly take you to the varnish cached version, and you can see the details of varnish’s workings with this command:

varnishstat
Varnish HTTP acceleration

Varnish HTTP acceleration

Any comments are appreciated, see below.

Menu