How to install PHP redis extension on Linux

Before install redis extension in PHP, please make sure that you have installed the redis server and can use PHP related commands on your machine, because we need them later.

  • STEP 1: Installation
# download source code
$ git clone https://github.com/phpredis/phpredis.git

# enter the source code directory
$ cd phpredis

# phpize is tool for extension installation in php and it will generate compiling environment
$ /path/phpize  --with-php-config=/path/php-config

# compile
$ ./configure

# install
sudo make
sudo make install

when see this below, is means that you have installed it successfully.

Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-non-zts-20180731/
  • STEP 2: modify php.ini and make it work

find your php.ini and add this:

extension=/usr/local/lib/php/extensions/no-debug-non-zts-20180731/redis.so

then restart your fpm or apache.

  • STEP 3: Testing

Here is a test php script, run it in your linux server:

<?php
    // create a redis instance
	$redis = new Redis();
    // connect redis server, modify your redis server address and port
	$redis->connect('127.0.0.1', 6379);
	echo "connect\n";
	echo 'running: ' . $redis->ping();

The script will print this if it works:

$ php redisTest.php
connect
running: pong

Feel free if you have any problem.

RSS