Accelerate WordPress with Redis

Linux hhzk 2年前 (2017-02-17) 87次浏览 0个评论
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  !

WordPress + Redis =  ??

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 !

Join the Lab and test its base  Redis

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.

Redis Sunrise manager

Test the connection to Redis from a  terminal

Open your favorite terminal, and log in to your Redis database command line  :

1
Redis-cli -h my-instance-url -p my-port

For my part it looks like  :

1
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 ..

1
Auth MyPassWord

And finally, let’s monitor what happens on your Redis base  :

1
Monitor

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 . 

Test the connection to Redis from its  WordPress site

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  :

1
2
3
<? Php 
    phpinfo (); 
?>

Then go to http://www.mywebsite.com/wordpress/phpinfo.php (to change according to your configuration) and look for  :

PHPinfo and RedisThis 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)  :

1
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  :

1
2
3
4
5
6
7
8
9
 <? Php 
     // 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.

Configuring WordPress with  Redis

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  ).

Changing WPCONFIG . PHP

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.

1
2
3
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 .

1
Define ('WP_CACHE_KEY_SALT', 'myvps_');

Installing the Redis Object  Cache plugin

Redis Object Cache infoThen, 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 WPCONFIG . 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).

Plugin settings

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.

Redis Object Cache infoEverything is clean  !

Checking earnings  performance

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  :

1
Keys *

This will allow you to parse all key-value sets stored in Redis. In my case  :

1
2
3
4
5
6
7
8
...
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  :

1
Get myvpswp_: posts: 1

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).

Chrome Network toolDo not hesitate to share your feedback on your tests  !


乐趣公园 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Accelerate WordPress with Redis
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址