Redis is one of my favorite new data stores of the NoSQL movement. It has the power and speed to act as a caching server like Memcached and it has more advanced data types to make it more like an RDBMS. Before we can talk about using Redis as a session handler for PHP, we need to install the proper extension from source. Unfortunately, Redis clients built in PHP (like Predis and Rediska) provide no native session handling. You could write a custom session handler and use those clients, but the time it takes to install
phpredis from source is negligible.
To get things started, you will need to get your hands on the source code. The project is available on GitHub or you can easily grab it using
wget (see below). Please note that this installation guide is for Linux systems, the project page discusses some additional steps if you are installing on OS X.
wget https://github.com/nicolasff/phpredis/archive/master.zip unzip master.zip cd phpredis-master
Once you have the source code downloaded and extracted
cd to the directory (if you haven’t already) and you run:
phpize ./configure make sudo make install
phpize prepares the PHP extension for compiling, think of it like a
configure script for PHP stuff.
make install do the compiling and copy the extension to the right place, respectively. Now that the extension is installed, we need to configure PHP to use it. You can either add a line to your
php.ini or simply create a new INI file just for Redis:
sudo echo "extension=redis.so" > /etc/php5/conf.d/redis.ini
Once the file is created, all you will need to do is restart your web serve (if you’re using the Apache module) or the PHP Fast Process Manager itself. Once you’ve done so, do ahead and pull up
phpinfo() and you should see a new section labeled “Redis”. That’s all there is to it, once installed you can start using it immediately. Further documentation on how to use
phpredis is available here.