How to install Redis on Ubuntu

How to install Redis on Ubuntu

Redis is one of the many in-memory database thingies that makes stuff faster.

I use the Redis Object Cache plugin for WordPress, but you obviously need a Redis server for that. This tutorial will be on how to install redis on Ubuntu server.

Install

The installation is pretty simple, all you need is to install two packages; one for the actual server, and another one for PHP. Both can be installed with:

sudo apt install redis-server php-redis

You should now be able to run:

sudo service redis-server restart

and not get an error 🙂 .

Configuration

I prefer to use Unix sockets because they’re faster than TCP if you’re on the same system. Since I only own one VPS, I chose to use redis on a socket. To do this, you’ll need to edit the redis config file:

sudo nano /etc/redis/redis.conf

and look for the following section:

# Specify the path for the Unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
# on a unix socket when not specified.
# 
#unixsocket /var/run/redis/redis.sock

All you need to do is uncomment the unixsocket line:

# Specify the path for the Unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
# on a unix socket when not specified.
# 
unixsocket /var/run/redis/redis.sock

Then, restart the redis-server. Next, you’ll need to configure the plugin, which can be done by adding the following line to wp-config.php:

define('WP_REDIS_SCHEME', 'unix');
define('WP_REDIS_PATH', '/var/run/redis/redis.sock');

Permissions

You’ll now realize that the plugin won’t work for some reason. This is due to the fact that the socket permissions are set to the redis user and not the web-server user. This can be easily fixed with:

sudo chown www-data:www-data /var/run/redis/redis.sock

After running the command, check again. You should now have a fully functional redis install that can be accessed via a unix socket from PHP, or any other process that runs as www-data, such as python(if you’re using that to power a site).

Checking RAM usage

To check how much RAM the redis server is using(i.e. check if it’s doing anything), you can run the following command:

ps -C redis-server v

which will return something along the lines of(the RSS is the memory):

  PID TTY      STAT   TIME  MAJFL   TRS   DRS   RSS %MEM COMMAND
21793 ?        Ssl    4:50   1788     0 65004  7220  1.4 /usr/bin/redis-server 1

I have no idea what the arguments do, but I found it somewhere on lowendbox, and it works 🙂 . It gives you the size in Kilobytes, so just run the following command to convert to Megabytes:

expr 7220 / 1024

Leave a Reply(Markdown is On)

%d bloggers like this: