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:
sudo apt-get update sudo apt install build-essential -y
Fun, right? But we’re going to have even more fun. Let’s grab the latest Redis build:
cd /tmp curl -O http://download.redis.io/redis-stable.tar.gz tar xzvf redis-stable.tar.gz cd redis* sudo make make test sudo make install PREFIX=/usr sudo mkdir /etc/redis sudo cp /tmp/redis-stable/redis.conf /etc/redis cd .. 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:
sudo adduser --system --group --disabled-login redis --no-create-home --shell /bin/nologin --quiet
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!
sudo usermod -g www-data redis
On to Configuring Your New Redis Environment!
Now it’s time to do some moving and shaking:
sudo mv /etc/redis/redis.conf /etc/redis/redis.conf.bak sudo nano /etc/redis/redis.conf
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 (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):
- 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:
[Unit] Description=Redis Datastore Server After=network.target [Service] Type=forking PIDFile=/var/run/redis/redis.pid User=redis Group=www-data ExecStartPre=/bin/mkdir -p /var/run/redis ExecStartPre=/bin/chown redis:www-data /var/run/redis ExecStart=/sbin/start-stop-daemon --start --pidfile /var/run/redis/redis.pid --umask 007 --exec /usr/bin/redis-server -- /etc/redis/redis.conf ExecReload=/bin/kill -USR2 $MAINPID ExecStop=/usr/bin/redis-cli shutdown Restart=always [Install] WantedBy=multi-user.target
Now we need to enable the script we just created:
sudo systemctl enable redis-server
And it’s time to kick off our fancy new systemd script:
sudo service redis-server start
Make Sure You Didn’t Screw Anything Up
In many cases netstat can be your best friend. We’ll use it here to look at the server connections and make sure the Redis server process is running as our user, little redis.
If all was done correctly, you should get something back that looks a bit like this:
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 7777/redis-server 1
As you can see, the loopback adapter (127.0.0.1) is listening on port 6379.
Now let’s make sure that our user, little redis, is the one the Redis server process is running under:
ps aux | grep redis
You’ll want to look for a line similar to this:
redis 7777 0.5 0.4 52324 15324 pts/0 Ssl 04:42 0:00 /usr/bin/redis-server 127.0.0.1:6379
Note: If redis-server is running as root, you’ve either got a potential security issue or you’ve been hacked. This would look something like this:
Final Lap – Installing PHPRedis
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:
cd /tmp git clone https://github.com/phpredis/phpredis cd phpredis phpize ./configure sudo make make install
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:
sudo ln -s /etc/php/7.0/mods-available/redis.ini /etc/php/7.0/fpm/conf.d/30-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, otherwise known as clapping!