Install and run WordPress cached with Redis on a Linux VPS

redisWordPress page loading speed can be very slow, especially if your site have many visitors and/or you use a lot of plugins. Caching is the best way to improve the loading speed of a WordPress website and overall users experience. In this guide, we will walk you through the basic installation process of WordPress on an Ubuntu 14.04 VPS. with Nginx, MariaBD and PHP FPM and setting up a Redis cache to boost WordPress performance. This should work on other Linux VPS systems as well but was tested and written for Ubuntu 14.04.

Update the system and install necessary packages.

apt-get -y update && apt-get -y upgrade
apt-get install software-properties-common curl git

Install the latest stable version of Redis

apt-add-repository ppa:chris-lea/redis-server
apt-get update
apt-get install redis-server

Install MariaDB 10.0

apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
add-apt-repository 'deb http://ftp.osuosl.org/pub/mariadb/repo/10.0/ubuntu trusty main'
apt-get update
apt-get install mariadb-server

When installation complete, run the following command to secure your installation:

mysql_secure_installation

Next, we need to create a database for our WordPress instance.

mysql -uroot -p
MariaDB [(none)]> CREATE DATABASE wpdb;
MariaDB [(none)]> GRANT ALL PRIVILEGES ON wpdb.* TO 'wpuser'@'localhost' IDENTIFIED BY 'wpuser_passwd';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> \q

Install PHP and Nginx

The latest version of Nginx 1.6.2 is not available via the default Ubuntu repositories, so we will add the “nginx/stable” PPA, update the system and install the nginx package.

add-apt-repository ppa:nginx/stable
apt-get update
apt-get install nginx php5-fpm php-cli php5-gd php5-mysqlnd

Nginx configuration

Create a new Nginx server block with the following content:

nano /etc/nginx/sites-available/example.com
server {
  server_name example.com;
  listen 80;
  root /var/www/example.com/public_html;
  access_log /var/www/example.com/logs/access.log;
  error_log /var/www/example.com/logs/error.log;
  index index.php;

  location / {
    try_files $uri $uri/ /index.php?q=$uri&$args;
  }

  location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
    access_log off;
    expires max;
  }

  location ~ /\.ht {
    deny  all;
  }

  location ~ \.php$ {
    fastcgi_index index.php;
    fastcgi_keep_conn on;
    include /etc/nginx/fastcgi_params;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
}

Activate the server block by creating a symlink:

ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com

Test the Nginx configuration and restart the server by running the following commands:

nginx -t
/etc/init.d/nginx restart

Install WordPress

Create a new directory for your WordPress site:

mkdir -p /var/www/example.com/{public_html,logs}

Download and extract the WordPress package:

wget -q -O - http://wordpress.org/latest.tar.gz | tar -xzf - --strip 1 -C /var/www/example.com/public_html

Set the correct permissions:

chown www-data: -R /var/www/example.com/public_html

Finally run the WordPress installation script by accessing the URL in your web browser of choice. http://example.com/

Redis Cache

Clone the BenjaminAdams/wp-redis-cache github repository:

 git clone https://github.com/BenjaminAdams/wp-redis-cache.git /tmp/wp-redis-cache

Move index.php and index-wp-redis.php to your WordPress directory.

 mv /tmp/wp-redis-cache/{index-wp-redis.php,index.php} /var/www/example.com/public_html/

Open the index-wp-redis.php file and change $websiteIp with your server IP address, and $secret_string with a random string.

Move the wp-redis-cache diectory to your WordPress plugin directory and activate the plugin via the ‘Plugins’ menu in WordPress.

mv /tmp/wp-redis-cache/wp-redis-cache /var/www/example.com/public_html/wp-content/plugins/

Please note, sometimes when you upgrade WordPress it will overwrite the index.php file and you will have to manually change the index.php to:

<?php
require('index-wp-redis.php');

For more information about wp-redis-cache please check the README.md file.

Of course, you don’t have to do any of this if you use one of our Linux VPS Hosting services, in which case you can simply ask our expert Linux admins to setup this for you. They are available 24×7 and will take care of your request immediately. You can also consider reading our post on How to Speed Up WordPress with Redis Caching.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

5 thoughts on “Install and run WordPress cached with Redis on a Linux VPS”

    • hi,

      you can check if redis is caching by using its cli tool known as ‘redis-cli’, for example:

      redis-cli MONITOR

      to learn more on how to use ‘redis-cli’ please refer to redis official documentation at http://redis.io/commands and http://redis.io/documentation

      Reply
  1. Redis is an awesome cache. I found I had to set the following in the redis.conf:

    maxmemory 256mb #(or your max mem you want to use)
    maxmemory-policy allkeys-lru

    The default was 0 which meant ‘unlimited’ – the server would literally run out of RAM and the host would grind to a halt.

    Reply

Leave a Comment