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.

Installation

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'
services:
  redis:
    image: "redis:alpine"
    ports:
      - "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 https://github.com/phpredis/phpredis.git && cd phpredis 
phpize
./configure
make && make install
echo "extension=redis.so" | 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

Testing

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

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

Check in redis-cli. Jus type redis-cli in terminal.

127.0.0.1:6379> 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 infzanoza@gmail.com for services of experienced web developer.

Follow @infernosquad

LEAVE A COMMENT