rss feed Twitter Page Facebook Page Github Page Stack Over Flow Page

Install redis on Ubuntu with PHP

Posted on Sunday December 16, 2012 by Eric Potvin

Isn't good to have traffic on your website? The more traffic the merrier right? Sometime we need a little help from tools to help our site be more responsive to the demand. One of these tools is Redis.

Redis is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.

Here's a simple tutorial on how to install Redis on Ubuntu.

sudo aptitude install redis-server

You will see something like this:

Setting up redis-server (2:2.2.12-1build1) ...
Starting redis-server: redis-server.

You can always change the redis config file for your needs. To do so, simply edit the redis.conf file:

sudo /etc/init.d/redis-server stop
sudo vi /etc/redis/redis.conf
sudo /etc/init.d/redis-server start

Verifying installation.

redis-cli

this will prompt:

redis 127.0.0.1:6379>

and type: INFO

redis_version:2.2.12
redis_git_sha1:00000000
redis_git_dirty:0
arch_bits:32
multiplexing_api:epoll
process_id:4376
uptime_in_seconds:46
uptime_in_days:0
lru_clock:1245433
used_cpu_sys:0.01
used_cpu_user:0.00
used_cpu_sys_children:0.00
used_cpu_user_children:0.00
connected_clients:1
connected_slaves:0
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
used_memory:547620
used_memory_human:534.79K
used_memory_rss:1187840
mem_fragmentation_ratio:2.17
use_tcmalloc:0
loading:0
aof_enabled:0
changes_since_last_save:1
bgsave_in_progress:0
last_save_time:1354631602
bgrewriteaof_in_progress:0
total_connections_received:1
total_commands_processed:1
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
hash_max_zipmap_entries:512
hash_max_zipmap_value:64
pubsub_channels:0
pubsub_patterns:0
vm_enabled:0
role:master

Installing PHP with Redis

Here you have 2 options. You can either go with a PHP solution or the compiled PHP module.

PHP Version

This one only requires the PHP library for Redis. Here's two of them:

Predis

Predis is a flexible and feature-complete PHP (>= 5.3) client library for the Redis key-value store.

The library does not require any additional extension loaded in PHP but it can be optionally paired with the phpiredis C extension to lower the overhead of serializing and parsing the Redis protocol. Predis is also available in an asynchronous fashion through the experimental client provided by the Predis\Async library.

For a list of frequently asked questions about Predis, see FAQ.md in the root of the repository. More details are available on the official wiki of the project.

Main features

Available at: https://github.com/nrk/predis

redisent

Redisent is a simple, no-nonsense interface to the Redis data structure store for modest developers. It is designed to flexible and tolerant of changes to the Redis protocol.

Available at: https://github.com/jdp/redisent

Compiled version

The compiled version is available at https://github.com/nicolasff/phpredis.

cd /tmp && wget https://github.com/nicolasff/phpredis/archive/master.zip && unzip master.zip && cd phpredis-master/

Compiling shared PECL extensions.

phpize

If you would like phpredis to serialize your data using the igbinary library, run configure with --enable-redis-igbinary.

./configure [--enable-redis-igbinary]

Make it!

make

You will see something like this:

Build complete.
Don't forget to run 'make test'.

Then make the package:

sudo make install

This will create the module file:

Installing shared extensions:     /usr/lib/php5/20090626+lfs/

Then add it to apache:

sudo vi /etc/php5/apache2/conf.d/redis.ini

And add this configuration:

; configuration for php PDO module
extension=/path/to/php5/redis.so

Stop and start Apache:

sudo /etc/init.d/apache2 restart

Now, if you create a php file with the phpinfo(); you will see something like:

redis
Redis Support	enabled
Redis Version 	2.2.2

Registered save handlers 	files user memcache memcached redis

Then use it with PHP

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
if($redis->exists('key')) {
  $redis->set('key', 'value');
}
$redis->get('key');

All the commands are available at: https://github.com/nicolasff/phpredis/blob/master/README.markdown#classes-and-methods