Skip to main content

How to install WordPress + W3 Total Cache + Redis

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. Because of Memcached is not well maintained (not compatible with PHP 7) and could be considered as end-of-life, Redis should be a good alternative for Memcached.

Redis is a single-threaded server. It is not designed to benefit from multiple CPU cores. People are supposed to launch several Redis instances to scale out on several cores if needed. It is not really fair to compare one single Redis instance to a multi-threaded data store. Redis can also be configured as a cluster.

Step 1 — Install Redis

First we need to enable the EPEL repository on our machine. If you are unfamiliar with it, EPEL is the Extra Packages for Enterprise Linux repo, developed by the Fedora project with the intention of providing quality third-party packages for enterprise users of RHEL-based distros.

wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm

NOTE for CentOS users
You can install EPEL by running yum install epel-release. The package is included in the CentOS Extras repository, enabled by default.

Now run:

sudo rpm -ivh epel-release-7-5.noarch.rpm

And now type in:

sudo yum -y update

Note that this may take a while to complete. Now you may install Redis on your machine, by running:

sudo yum install redis -y

Once the installation process has finished, starting the Redis service is done by entering the following command:

sudo systemctl start redis

And checking its status can be done with the following command:

sudo systemctl status redis

Finally, let’s test our Redis setup by running:

redis-cli ping
PONG

This should print a PONG as the response. If this is the case, you now have Redis running on your server, and we can start configuring it. An additional test for our setup can be done by running:

redis-benchmark -q -n 1000 -c 10 -P 5

The above command is saying that we want redis-benchmark to run in quiet mode, with 1000 total requests, 10 parallel connections and pipeline 5 requests. For more information on running benchmarks for Redis, typing redis-benchmark --help in your terminal will print useful information with examples.

Let the benchmark run. After it’s finished, you should see output similar to the following:


Output
PING_INLINE: 166666.67 requests per second
PING_BULK: 249999.98 requests per second
SET: 200000.00 requests per second
GET: 200000.00 requests per second
INCR: 200000.00 requests per second
LPUSH: 200000.00 requests per second
LPOP: 200000.00 requests per second
SADD: 200000.00 requests per second
SPOP: 249999.98 requests per second
LPUSH (needed to benchmark LRANGE): 200000.00 requests per second
LRANGE_100 (first 100 elements): 35714.29 requests per second
LRANGE_300 (first 300 elements): 11111.11 requests per second
LRANGE_500 (first 450 elements): 7194.24 requests per second
LRANGE_600 (first 600 elements): 5050.50 requests per second
MSET (10 keys): 100000.00 requests per second

Build from source (OPTIONAL)

Because this is an old stable version (Redis v2.8). You can better install from source (Redis v3.2.5):

Compile from source:

wget http://download.redis.io/redis-stable.tar.gz
tar -xzvf redis-stable.tar.gz
cd redis-3.2.5
make
make install

 

Step 2 — Config Redis

Better to skip this first, do this after installation.

You can check for configuration errors, by starting Redis the normal way: redis-server

Problem 1:

WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

$ sudo nano /etc/sysctl.conf
vm.overcommit_memory=1
$ sysctl vm.overcommit_memory=1
$ sysctl -w fs.file-max=100000
WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128

Solution:
then to solve that:
add at /etc/sysctl.conf file, this line:


net.core.somaxconn=65535

after run:

sysctl -w net.core.somaxconn=65535

Problem 2:


WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.

Problem 3:


WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

Service commands:


$ systemctl start/stop/restart redis

Step 3 — Install Phpredis

This extension provides an API for communicating with Redis servers. For more information: Pecl Redis package or Phpredis Github page

pecl install redis

Note: If pecl is not working, because /tmp directory is not executable. Do the following:

sudo mount -o remount,exec /tmp

 

Note: If pecl was not working, because /tmp directory was not executable. Do the following to revert the changes to the /tmp directory:

sudo mount -o remount,noexec /tmp

Now it is necessary to add compiled extension to php config

Add PhpRedis extension to PHP 7

To do so, either edit your php.ini or add a redis.ini file in /usr/local/php70/lib/php.conf.d with the following contents: extension=redis.so:

sudo touch /usr/local/php70/lib/php.conf.d/redis.ini && echo extension=redis.so > /usr/local/php70/lib/php.conf.d/redis.ini

Restart PHP-FPM:

systemctl restart php-fpm70

Install Redis Object Cache plugin (OPTIONAL)

Next, we need to install the Redis Object Cache plugin. It installs a PHP script that helps WordPress communicate with Redis. After installing the plugin, move the object-cache.php file from plugins/redis folder to wp-content folder if not done already.

Note: Make sure object-cache.php file isn’t present under the Redis folder anymore. So first disable object caching in W3 Total Cache plugin.

Edit the wp-config.php file

At this point, you must make a backup of your wp-config.php file before proceeding.

In the wp-config.php file, we need to add cache key salt using the define (‘WP_CACHE_KEY_SALT’, ‘yourURL.com’); under * Authentication Unique Keys and Salts.

You can use any unique string in yourdomain.com, we recommend you to use the URL of your website. It will be helpful if you are hosting multiple websites on single server.


/**#@+
* Authentication Unique Keys and Salts.
* Change these to different unique phrases!
* You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
*/
define('WP_CACHE_KEY_SALT', 'yourdomain.com');

 

Check that it’s enabled

Check that Redis Cache is activated by typing this command in your terminal:

$~: redis-cli monitor

or if you are on a UNIX socket

$~: redis-cli -s /var/run/redis.sock monitor

Set a Password for the Redis Server

To add an extra layer of security to your Redis installation, you are encouraged to set a password for accessing the server data. We will edit the same configuration file from the previous step, /etc/redis/redis.conf:

sudo vim /etc/redis/redis.conf

Now, uncomment the line that contains requirepass, and set a strong password:

/etc/redis/redis.conf
requirepass yourverycomplexpasswordhere

Restart the Redis service so the changes take effect:

sudo systemctl redis-server restart

Set Redis as the Default Session Handler on the Web Server

Now we need to edit the php.ini file on the web server to change the default session handler for PHP.

Open your php.ini file and search for the line containing session.save_handler. The default value is files. You should change it to redis.

/usr/local/php70/lib/php.ini

After edit it should look like this:

session.save_handler = redis

Now you should find the line containing session.save_path. Uncomment it and change the value so it contains the Redis connection string. The content should follow this format, all in one line: tcp://IPADDRESS:PORT?auth=REDISPASSWORD

After edit is should look like this:

session.save_path = "tcp://127.0.0.1:6379?auth=yourverycomplexpasswordhere"

Note: You only need to provide the parameter auth if you did set a password when configuring Redis.

After editing restart php-fpm service:

sudo systemctl restart php-fpm70

Config W3 Total Cache plugin

W3 Total Cache improves the user experience of your site by increasing website performance, reducing download times via features like content delivery network (CDN) integration.

Now Redis is working, the following is possible to configure:

schermafbeelding-2016-12-02-om-00-25-20

Conclusion

Redis is a powerful and fast key-value storage service that can also be used as caching engine and session handler for PHP, enabling scalable PHP environments by providing a distributed system for session storage.

Reacties (2)

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *