WordPress is the most popular CMS (content management system) used on the internet today. While many people use it because it is powerful and simple. sometimes people make a trade-off for convenience at the load and speed of the website and web pages.
Today, we will present two tools that will help you to speed up your wordpress website.
The first tool is : Varnish
Varnish is an HTTP accelerator and a useful tool for speeding up a server, especially during a 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.
The second tool is : Redis
Redis is an open-source key value store that can operate as both an in-memory store and as cache. Redis is a data structure server that can be used as a database server on its own, or paired with a relational database like MySQL to speed things up, as we’re doing in this tutorial.
These are two pretty different pieces of software. The end goal of both pieces of software is the same, though and most sites would use both technologies in order to speed up delivery. One is an HTTP load balancer that does caching Varnish (HTTP accelerator) and the other is a simple key-value store Redis. You should pick whichever one is appropriate for your particular needs.
For this tutorial, Redis and Varnish will be configured as a cache for WordPress to alleviate the redundant and time-consuming http and database queries used to render a WordPress page.
Install and Configure Varnish with Apache
Before you start installing varnish in your wordpress website server, assume that you have installed WordPress on Ubuntu. You can follow our guide on How To Install WordPress In Ubuntu.
You need also to have a user with sudo privileges.
Install Varnish
Varnish is distributed in the Ubuntu package repositories, but the version there might be out of date, so the varnish site recommends installing the varnish package through their repository.
The only supported architecture is amd64.
sudo apt-get install apt-transport-https
curl https://repo.varnish-cache.org/GPG-key.txt | sudo apt-key add -
The next step is to add the repository to the list of apt sources.
sudo echo "deb https://repo.varnish-cache.org/ubuntu/ trusty varnish-4.0" >> /etc/apt/sources.list.d/varnish-cache.list
Save and exit.
Finally, update apt-get and install varnish.
sudo apt-get update
sudo apt-get install varnish
Configure Varnish
Once you installed varnsih , you can start configuring it.
Varnish will serve the content on port 80 and you need to change apache to run on port 8080.
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
Varnish uses the concept of backend or origin server to define where it should retrieve the content from if it’s not persistent in its cache. Edit /etc/varnish/default.vcl with a text editor and ensure the following is present.
backend default { .host = "127.0.0.1"; .port = "8080"; }
Now the last step on varnish configuration is the apache port configuration.
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.
NameVirtualHost 127.0.0.1:8080 Listen 127.0.0.1:8080
Also change these settings in the default virtual host file:
sudo nano /etc/apache2/sites-available/default
The Virtual Host should also be set to port 8080, and updated line looks like this example:
<VirtualHost 127.0.0.1:8080>
Now that all the configuration are complete you need to start Varnish and restart Apache.
Once this is done, all traffic to our WordPress site will pass through Varnish before it hits the Apache server.
sudo service varnish start sudo service apache2 restart
Wordpress Config with Varnish
After installing Varnish you need to allow WordPress to purge the cached content whenever it is modified. There are several plugins to achieve this. In this tutorial we will use Varnish HTTP Purge.
Go to the WordPress dashboard, click on Plugins>Add New and search for ‘Varnish HTTP Purge’. Click on ‘Install Now’ and confirm. Finally, activate it.
To make Varnish HTTP Purge plugin work correctly you need to enable mod_rewrite using this command
sudo a2enmod rewrite
Then use a custom URL structure for permalinks and archives. Go to WordPress dashboard click on Settings>Permalinks and select ‘Custom Structure’. Then type /%year%/%monthnum%/%post_id% and click on ‘Save Changes’.
Best paratice of Varnish with wordpress
One of the best paratice is to exclud any admin or login related pages from hitting the cache.
Open /etc/varnish/default.vcl and add the following before we remove the cookies from the previous step.
if (req.url ~ "wp-admin|wp-login") { return (pass); }
Varnish uses the max-age parameter in the Cache-Control HTTP header to establish how long the content is considered fresh before contacting the backend again.
The default configuration of Varnish is 120 seconds you need to extend this period to one hour we could update /etc/varnish/default.vcl.
sub vcl_backend_response { if (beresp.ttl == 120s) { set beresp.ttl = 1h; } }
Varnish will not cache content for requests including the Cookie or header or responses including the Set-Cookie header.
WordPress sets many cookies that are safe to ignore during normal browsing so let’s update /etc/varnish/default.vcl and add the following inside vcl_recv to remove them.
set req.http.cookie = regsuball(req.http.cookie, "wp-settings-\d+=[^;]+(; )?", ""); set req.http.cookie = regsuball(req.http.cookie, "wp-settings-time-\d+=[^;]+(; )?", ""); set req.http.cookie = regsuball(req.http.cookie, "wordpress_test_cookie=[^;]+(; )?", ""); if (req.http.cookie == "") { unset req.http.cookie; }
Now you need to reload again the configuration of Varnish using this command
sudo service varnish reload
Install and Configure Redis with Apache
Install Redis
In order to use Redis with WordPress, two packages need to be installed:
and
.
The
package provides Redis itself, while the
package provides a PHP extension for PHP applications like WordPress to communicate with Redis.
Use this command to install it
sudo apt-get install redis-server php5-redis
Configure Redis
As Redis can operate as cache server and nosql database. You need to configure it in your wordpress website as a cache. In order to do this, the following settings are required.
Edit the file
and add the following lines at the bottom:
sudo nano /etc/redis/redis.conf
Add these lines at the end of the file:
maxmemory 256mb maxmemory-policy allkeys-lru
When changes are complete, save and close the file.
Wordpress Config with Redis
The next step is to install Redis Object Cache Wordpress Plugin.
This plugin is persistent object cache backend powered by Redis. Supports HHVM’s Redis extension, the PCEL Redis Extension and the Predis library for PHP.
You need to Install and activate this plugin from wordpress dashboard. Go to the WordPress dashboard, click on Plugins>Add New and search for ‘Redis Object Cache’. Click on ‘Install Now’ and confirm. Finally, activate it.
Then you need to enable the object cache under Tools -> Redis.
If necessary, adjust connection parameters of this plugin. By default the object cache drop-in will connect to Redis over TCP at
and select database
.
To adjust the connection parameters, define the following constants in your
.
-
WP_REDIS_CLIENT
[default: not set]Specifies the client used to communicate with Redis. Supports
hhvm,
pecland
predis.
-
WP_REDIS_SCHEME
[default:
tcp]Specifies the protocol used to communicate with an instance of Redis. Internally the client uses the connection class associated to the specified connection scheme. Supports
tcp(TCP/IP),
unix(UNIX domain sockets) or
http(HTTP protocol through Webdis).
-
WP_REDIS_HOST
[default:
127.0.0.1]IP or hostname of the target server. This is ignored when connecting to Redis using UNIX domain sockets.
-
WP_REDIS_PORT
[default:
6379]TCP/IP port of the target server. This is ignored when connecting to Redis using UNIX domain sockets.
-
WP_REDIS_PATH
[default: not set]Path of the UNIX domain socket file used when connecting to Redis using UNIX domain sockets.
-
WP_REDIS_DATABASE
[default:
0]Accepts a numeric value that is used to automatically select a logical database with the
SELECTcommand.
-
WP_REDIS_PASSWORD
[default: not set]Accepts a value used to authenticate with a Redis server protected by password with the
AUTHcommand.
-
WP_REDIS_MAXTTL
[default: not set]Set maximum time-to-live (in seconds) for cache keys with an expiration time of
0.
Prefixing Cache Keys
The
constant is provided to add a prefix to all cache keys.
Users with setups where multiple installs share a common
or
can use this constant to guarantee uniqueness for the keys generated by this object cache.
Finally, restart
and
.
Restart Redis:
sudo service redis-server restart
Restart Apache:
sudo service apache2 restart
You can check now your page load speeds and resource use, you should notice improvements.
To monitor Redis, use the
command like this:
redis-cli monitor
After following this guide, you will speed up your WordPress website.
That’s all enjoy and thanks 🙂