Working with PHP 7 and Redis on Linux

August 14, 2017 | No Comments | Programming | linux PHP redis

After upgrading to PHP 7 some PHP extensions are outdated and we need to build them manually. In this short article I will show you how to bootstrap with PHP 7 and Redis on you Linux without any troubles.


I will use Docker and Docker Compose for Redis installation but you can download and install redis-server from official website. My Linux distro РUbuntu 16.04.

Most of tools which used in article can be installed from your package manager such as yum, apt etc.

Let’s setup docker-compose.yml file fist.

version: '2'
    image: "redis:alpine"
      - "6379:6379"

Then run

sudo docker compose up -d

Check out if docker container is running

sudo docker ps

Ping redis server

redis-cli ping

Should return PONG

Building php extension

Now we should compile phpredis extension. I found it as easiest way then use some outdated packages and channels. So you will need installed git and build-essential packages.

git clone -b php7 && cd phpredis 
make && make install
echo "" | sudo tee /etc/php/7.0/mods-available/redis.ini 
sudo phpenmod -v 7.0 redis
sudo service php7.0-fpm restart # if you use php-fpm cgi


Create PHP CLI file and test our connection between PHP and Redis

$redis = new \Redis();
$redis->pconnect('', 6379);
$redis->set('name', 'John Doe');
var_dump($redis->strlen('name')); // return 8

Check in redis-cli. Jus type redis-cli in terminal.> MGET name
1) "John Doe"

As we can see, now PHP communicates with Redis and we can start integration with our application.

About the Author / Artem Zhuravlev

Artem Zhuravlev. Web developer. Blog writer.

Need help with your website ? Contact with me by email for services of experienced web developer.

Follow @infernosquad