Working with PHP 7 and Redis on Linux
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.
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"
sudo docker compose up -d
Check out if docker container is running
sudo docker ps
Ping redis server
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
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.