본문 바로가기

IT/PHP

How to install Redis and Redis php client

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 onofficial 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 version. Download it and unpack:

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

Next step is to compile with make utility and install:

make
sudo make install clean

If you have 64 bit system it is necessary to install libc6-dev-i386 before compile. In Ubuntu it is possible to make with flowing steps:

sudo apt-get install libc6-dev-i386
make 32bit
sudo make install clean

Create config:

mkdir /etc/redis
cp redis.conf /etc/redis/redis.conf

Edit it:

nano /etc/redis/redis.conf 

Example of minimal configuration:

#start as a daemon in background
daemonize yes
#set port, by default is 6379
port 6379
#set ip on which daemon will be listening for connections
bind 127.0.0.1
#where to dump database
dir /var/opt

By default redis-server will install in /usr/local/bin/redis-server. Check it whith whereis command:

whereis redis-server
#redis-server: /usr/local/bin/redis-server

If you want the server be accessible after system start, it is possible to write a line in /etc/rc.local file before "exit 0"

/usr/local/bin/redis-server /etc/redis/redis.conf

Start server

redis-server /etc/redis/redis.conf 

And try to connect

redis-cli

PhpRedis Installation

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

We need php5-dev

In Ubuntu it is possible to install with this command:

apt-get install php5-dev

In CentOS:

yum install php5-dev

Next step is to download, compile and install phpredis

sudo -i
cd /tmp
wget https://github.com/nicolasff/phpredis/zipball/master -O phpredis.zip
unzip phpredis.zip
cd nicolasff-phpredis-*
phpize
./configure
make && make install

Now it is necessary to add the module to php config for Ubuntu with PHP 5.3

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

for Ubuntu with PHP 5.5

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

for CentOS

touch /etc/php.d/redis.ini
echo extension=redis.so > /etc/php.d/redis.ini

You can check PhpRedis with command bellow, after that you should see "OK" inscription

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

Before using PhpRedis in your php scripts don't forget to restart your web server

if this is Apache

/etc/init.d/apache2 restart

if nginx

/etc/init.d/nginx restart