How to install Redis and Redis php client (Updated: Feb 23, 2018)
June 10, 2012 • 11 minutes read

Redis — cache-storage for data that is stored in RAM. In this case you will get very fast data access. It is similar to memcache but Redis has vast opportunities of data storage.

Redis allows you to store not only strings but also lists, sets, sorted sets and hashes. You can find more details about Redis on official Redis site. I recommend to install Redis from a source code. In this case you will get the latest version.

Redis installation

So, how to install Redis from source code? Go to Redis Download page and find there the latest stable version.

Install required packages for compilation from source

sudo apt-get install make gcc g++

Download Redis package and unpack

mkdir -p /tmp/redis
cd /tmp/redis
wget http://download.redis.io/releases/redis-stable.tar.gz
tar xzf redis-stable.tar.gz
cd redis-stable

Next step is to compile Redis with make utility and install

make
sudo make install clean

Add user redis

sudo useradd -s /bin/false -d /var/lib/redis -M redis

create Redis pid file directory

sudo mkdir /var/run/redis/ -p && sudo chown redis:redis /var/run/redis

create Redis config directory

sudo mkdir /etc/redis && sudo chown redis:redis /etc/redis -Rf

create Redis logs directory

sudo mkdir /var/log/redis/ -p && sudo chown redis:redis /var/log/redis/ -Rf

create Redis config and put it to /etc/redis/redis.conf:

sudo mkdir /etc/redis
sudo cp redis.conf /etc/redis/redis.conf
sudo chown redis:redis /etc/redis/redis.conf

Change parameters in config with your preferred text editor. For example, with nano

sudo nano /etc/redis/redis.conf 

Minimum configuration example:

#start as a daemon in background
daemonize yes
#where to put pid file
pidfile /var/run/redis/redis.pid
#loglevel and path to log file
loglevel warning
logfile /var/log/redis/redis.log
#set port to listen for incoming connections, by default 6379
port 6379
#set IP on which daemon will be listening for incoming connections
bind 127.0.0.1
#where to dump database
dir /var/lib/redis

Add Redis service (Ubuntu 14.04)

create Upstart file for Redis

sudo touch /etc/init/redis.conf

Put text below to /etc/init/redis.conf file

#!upstart
description "redis server"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
respawn limit 10 5
exec sudo -u redis /usr/local/bin/redis-server /etc/redis/redis.conf

Start server

sudo service redis start

Add Redis service (Ubuntu 16.04+)

create systemd file for Redis

sudo touch /etc/systemd/system/redis.service

Put text below to /etc/systemd/system/redis.service file

[Unit]
Description=Redis Server
After=network.target

[Service]
Type=forking
User=redis
Group=redis
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target

start server

Start server

sudo service redis start

Check Redis with ping command. Redis will response with "PONG"

redis-cli ping

Add Redis to system startup

sudo update-rc.d redis defaults

PhpRedis Installation

PhpRedis - PHP extension from Nicolas Favre-Felix, which allows to access to Redis from php. Why PhpRedis? There are a lot of other php libraries that allow to communicate with Redis. The answer - speed. This module have been written on C and this means that this is faster than libraries that have been written on php.

Next steps would depend on which PHP version do you have. So, first of all check your PHP version:

php -v

PhpRedis for PHP 5.* (Skip it if you have different PHP version)

Install required package

apt-get install php5-dev

Download PhpRedis

cd /tmp
wget https://github.com/phpredis/phpredis/archive/master.zip -O phpredis.zip

Unpack, compile and install PhpRedis

unzip -o /tmp/phpredis.zip && mv /tmp/phpredis-* /tmp/phpredis && cd /tmp/phpredis && phpize && ./configure && make && sudo make install

Now it is necessary to add compiled extension to php config

Add PhpRedis extension to PHP 5.5 and greater

sudo touch /etc/php5/mods-available/redis.ini && echo extension=redis.so > /etc/php5/mods-available/redis.ini
sudo ln -s /etc/php5/mods-available/redis.ini /etc/php5/apache2/conf.d/
sudo ln -s /etc/php5/mods-available/redis.ini /etc/php5/fpm/conf.d/
sudo ln -s /etc/php5/mods-available/redis.ini /etc/php5/cli/conf.d/

Add PhpRedis extension to PHP lower than 5.5 (5.3 - 5.4)

sudo touch /etc/php5/conf.d/redis.ini && echo extension=redis.so > /etc/php5/conf.d/redis.ini

PhpRedis for PHP 7 (Skip it if you have different PHP version)

Check your php version with command:

php -v

Install required package. Use proper package name for your php version. e.g. php7.0-dev, php7.1-dev, php7.2-dev

apt-get install php7.1-dev

Download PhpRedis

cd /tmp
wget https://github.com/phpredis/phpredis/archive/master.zip -O phpredis.zip

Unpack, compile and install PhpRedis

unzip -o /tmp/phpredis.zip && mv /tmp/phpredis-* /tmp/phpredis && cd /tmp/phpredis && phpize && ./configure && make && sudo make install

Now it is necessary to add compiled extension to php config

Add PhpRedis extension to PHP 7. Use proper path to your php configs e.g. /etc/php/7.1/ , /etc/php/7.2/

sudo touch /etc/php/7.1/mods-available/redis.ini && echo extension=redis.so > /etc/php/7.1/mods-available/redis.ini
sudo ln -s /etc/php/7.1/mods-available/redis.ini /etc/php/7.1/apache2/conf.d/redis.ini
sudo ln -s /etc/php/7.1/mods-available/redis.ini /etc/php/7.1/fpm/conf.d/redis.ini
sudo ln -s /etc/php/7.1/mods-available/redis.ini /etc/php/7.1/cli/conf.d/redis.ini

Final steps

Restart PHP-FPM if you have PHP 5

sudo service php5-fpm restart

Restart PHP-FPM if you have PHP 7

sudo service php7.1-fpm restart

Restart Apache

sudo service apache2 restart

Note: if you are using Nginx there is no need to restart it, because in most cases it works wit PHP FPM

You can check successfully installed PhpRedis with command bellow

php -r "if (new Redis() == true){ echo \"OK \r\n\"; }"

Methods for Redis class available on page PhpRedis

Useful resources:

Last update May 9, 2021
Development redis cache php highload
Translation of this article: Русский
Do you have any questions?

Contact Me