Why install Redis? One of the huge benefits to installing Redis Server is that you get can crazy, insane, ridiculously fast performance out of WordPress when coupled with the right plugin so the two can interface.
In this quick, easy guide we’ll be provisioning Redis Server and PHPRedis on a Ubuntu 16.04 LEMP server. My environment already has PHP 7 installed and is running WordPress 4.9.5. If you’re not on PHP 7 currently, you may want to upgrade it. Otherwise you’ll need to adapt the “sudo apt install php7.0-dev git -y” part at the end of this tutorial to your version of PHP.
In order for Redis to do its magical performance optimizing dance with WordPress, we’re going to require two components:
Redis Server (hello captain obvious)
The ever popular Redis PHP extension, often called “phpredis,” “predis” or simply “that awesome php thing for Redis.”
It’s Really Happening – You’re About to Install Redis Server
Are you feeling good about this? Because I am. I think, with this guide, you have an excellent chance of getting this done in the next 5 to 10 minutes.
And here we go! Let’s quick update Ubuntu’s local aptitude cache and install build-essential:
Fun, right? But we’re going to have even more fun. Let’s grab the latest Redis build:
curl -O http://download.redis.io/redis-stable.tar.gz
tar xzvf redis-stable.tar.gz
sudo make install PREFIX=/usr
sudo mkdir /etc/redis
sudo cp /tmp/redis-stable/redis.conf /etc/redis
rm -R redis*
Now we need a to create a user. We’ll give it the totally creative name of “redis” and restrict all of its privileges. It’s not that we hate this user, it’s more of a security thing:
Now let’s check on this completely unexciting user we just created – just to make sure it’s there:
cat /etc/passwd | grep redis
Is poor little redis there? If not, it’s time to give up on running CLI in Ubuntu. Nah, just kidding, you’re doing great! If not, you can try the “sudo adduser” part (from above) again – infinitely if need be, or until it works.
Now let’s add our redis user to a group so it can feel like it has friends (even though you and I both know it doesn’t), but we’re going to give it some lovely Unix domain sockets that will be listening to every single thing it has to say from hereon, so it’s all good!
Note: If like me, your redis.conf file turns out to be empty/non existent in the next step about tweaking the redis.conf file, you can come back here and use the command below to fix it. Otherwise, skip over this step.
cd /etc/redis #opens the directory that redis.conf.bak resides in
cp redis.conf.bak redis.conf #copies redis.conf.bak to a file named redis.conf
cd #returns to root directory
Now that we’ve got it open, we’re going to do some redis.conf tweaking:
Look for an uncommented entry for deamonize and ensure it’s set to yes.
Locate the uncommented bind entries and remove all but the localhost (bind 127.0.0.1):
Find maxmemory and set it to 40M.
Set maxmemory-policy to allkeys-lru
Set stop-writes-on-bgsave-error to no
Set rdbcompression to yes
Once the values shown above have been updated, you can exit and save the file.
Now we need to create a directory that our pid file will live in shortly:
sudo mkdir -p /var/run/redis
Now we’re going to give the redis user and group we created earlier ownership of the directory we just created so that the Unix sockets can work properly:
sudo chown -R redis:www-data /var/run/redis
Redis tends to be rather sleepy, so we need to give it a little kick on startup. To do this, we’ll create a systemd service entry so it can autostart on boot:
sudo nano /etc/systemd/system/redis.service
Now paste this into the blank file you’ve just opened then exit and save it:
As far installing the Redis Server goes, you’re done! Give yourself a warm hug and pat on the back. You’re now ready for round #2.
To get started we’ll provision the PHP development tools. In this instance I’m using PHP 7.0.28 so all of my commands will be applicable to this build of PHP (you can check your PHP version by running the command php -v):
sudo apt install php7.0-dev git -y
Now we’re going to git clone the latest and greatest PHPRedis build and install it:
git clone https://github.com/phpredis/phpredis
It’s time to enable the Redis PHP PECL extension for NGINX by adding an entry to mods-available:
sudo nano /etc/php/7.0/mods-available/redis.ini
…And (if not already present) add this line of code to redis.ini and save it (in my case, it was already there):
This is it! The very last bit of script you need to run to finish up. All that’s left to do is create a symbolic link for redis.ini:
By jove, you’ve really done it! It’s time for some air guitar along with some self-initiated and received high-fives!
We’re here to help you take the suck out of life for a few precious moments each day. We’re also here to inspire you to go out there and live life to the fullest. Ayrne is a digital sanctuary of ideas, inspiration, and laughter.