Do you have a website or blog powered by WordPress? Discover how to speed up loading, thanks to Redis! In this tutorial we set up a NoSQL Redis database, and use it to cache WordPress objects. The payload gains will be visible on your site for your visitors, but also in the administration of WordPress.
All actions performed in this tutorial are the responsibility of the Customer. OVH will not be held responsible for any failure. Remember to back up your files before making any changes.
This tutorial requires :
- A Web hosting compatible and with the module PHP -redis installed or installable ( an OVH shared Web hosting is currently not compatible ): VPS , Dedicated Server, Public Cloud .. at OVH or another provider.
- An administrator account on your WordPress, in order to install a plugin.
- WordPress version 4.x or higher
- And of course a database Redis, activable on our lab !
Redis is an open source tool to keep NoSQL databases in RAM and cache objects. The goal is speed. It is today constantly growing, adopted by Twitter, Github, Flickr, Pinterest and many others. We are not going to talk about Redis anymore, but do not hesitate to read the official documentation that is very well provided.
WordPress, for its part, can be resource consuming. User sessions, database queries, … Their developers allow us to put these “objects” in cache, ie to store them non- persistently.
You will understand, the gains will be more or less significant depending on your use of “objects”. A site with static content, such as a blog, will be much less optimizable than a merchant site, an information portal, and so on.
There are a multitude of plugins to manage this cache, the best known being W3 Total Cache ( W3TC ). However, it does offer a cache of objects locally (on your VPS , or server), which sometimes does not suit us. Maintaining a redis or memcached database can be tedious and memory consuming .
We will see in this tutorial how to configure the Redis Lab proposed by OVH , and link it to your WordPress. The goal is to increase performance when loading WordPress for visitors, but also for its administrators.
Let’s go !
The first step is to get a Redis database and check its operation.
Direction Runabove OVH labs then subscribe to Lab Redis. Once done, via the Sunrise OVH Manager , create a Redis database, change the password, and keep the page open, we will use it.
Open your favorite terminal, and log in to your Redis database command line :
Redis-cli -h my-instance-url -p my-port
For my part it looks like :
Redis-cli -h 950c9520-ed3c-492c-8e0a-c1xxxxxxxxxx.pdb.ovh.net -p 21244
Then authenticate with the password set in your OVH Sunrise Manager . Redis works with a unique password, no User required ..
And finally, let’s monitor what happens on your Redis base :
We will see here what happens on your live base.
At this point, the only thing we know is that the Redis base is running! Let’s see if we can connect to it in PHP .
On your instance hosting your WordPress, we will test the connection to Redis with a very fast script .
Via the medium of your choice ( FTP , SSH , ..) create a phpinfo.php file at the root of your wordpress site and add in it :
Then go to http://www.mywebsite.com/wordpress/phpinfo.php (to change according to your configuration) and look for :
This module allows PHP to interact with the Redis database. If you can not find any paragraph about Redis, you are missing the component. Install it (Debian / Ubuntu compatible command) :
Sudo apt-get install php-redis
You should see the Redis box (if necessary, update PHP and restart your instance).
Let’s now test the connection to Redis in PHP , from your hosting. Via the medium of your choice ( FTP , SSH , …), create a file redis.php at the root of your wordpress site and add in :
// Connecting to Redis server on OVH
$ redis = new Redis ();
$ Redis -> connect ( 'xxxxxxxxx-xxxxxx-xxxxx-xxxxxxxxx.pdb.ovh.net' , 12345 );
$ Redis -> auth ( 'MyPassword' );
Echo "Connection to server ongoing" ;
// check whether server is running or not
echo "Server is running" . $ Redis -> ping ();
Do not forget to fill in the access URL , port and password of your database, entered in your OVH Sunrise Manager . Then go to http://www.mywebsite.com/wordpress/redis.php .
If the connection is successful, you should see “Server is running: + PONG ”
And in your terminal open on your Redis base, a ” PING ”
Congratulations! Everything works based. We now move on to configuring WordPress.
The database works, remains to configure WordPress to use it. If you already have a cache plugin, you may want to disable the Object Cache ( for example, W3TC ).
Via a text editor, open the wp-config.php file at the root of your WordPress. We’re going to add those 3 lines right after the Key Salts.
Define ('WP_REDIS_HOST', '950c9520-ed3c-492c-xxxxx-xxxxxxxxxx.pdb.ovh.net');
Define ('WP_REDIS_PORT', '12345');
Define ('WP_REDIS_PASSWORD', 'MyPassword')
Optional: add a Key Salt. When you have a single application that uses Redis this is not necessary, but if for example you have multiple WordPress sites, this will be required to differentiate who-push-what .
Define ('WP_CACHE_KEY_SALT', 'myvps_');
Then, in order to facilitate our task, we will install the Redis Object Cache plugin . For the curious, it is a fork of the code of Eric Mann and Erick Hitter: Github Redis Object Cache .
If you are allergic to plugins, feel free to do a manual installation (note the names of params that are substantially different in WP – CONFIG . PHP )
This plugin will add a file (or modify) the file object-cache.php in the wp-content / folder of your WordPress. If the plugin fails to do so, add it manually (see the plugin page for details).
If everything is installed, go to your WordPress Admin for Redis.
You should see something like this :
Note the “Connected” status and your Host, Port, Database and Password well informed.
Everything is clean !
If you have not closed the monitoring of your Redis database, you should see activity on your base.
Open a second terminal or stop this monitoring, and on your Redis base enter :
This will allow you to parse all key-value sets stored in Redis. In my case :
48 ) "myvpswp_ how: get_comments: 9503889e74633f729bf0ed7217c233a4: 0.53134300 1486125318"
49 ) "myvpswp_ how: 1"
50 ) "myvpswp_: term_meta: 1"
51 ) "myvps_wp_: userlogins: bastien"
52 ) "myvpswp_: transient: feed_b9388c83948825c1edaef0d856b7b109"
53 ) "myvpswp_: posts: 1"
And to analyze a specific key as an example :
At the performance level, I opened the development tools and then “Network”. Being part of a WordPress totally clean and without content, I do not have a very significant gain but still appreciable !
The site is displayed in 250ms on average instead of 500ms (hosted on a VPS SSD OVH located in Strasbourg).
Do not hesitate to share your feedback on your tests !