Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

masterpowers's avatar

Tutorial Guide : Installing PHP REDIS PHP7 Branch On Fresh Install Homestead PHP7 Branch

Hi I havent Seen Any Tutorial On This Lately So Im Sharing What i Did On a Fresh New Install of Laravel Homestead7 with PHP7

and Install a Php 7 Branch of phpredis/phpredis

SSH in your Vagrant Machine then type the code below

vagrant@homestead:~$ sudo apt-get update
vagrant@homestead:~$ git clone -b php7 https://github.com/phpredis/phpredis.git
vagrant@homestead:~$ sudo mv phpredis/ /etc/
vagrant@homestead:~$ cd /etc/phpredis
vagrant@homestead:/etc/phpredis$  phpize
vagrant@homestead:/etc/phpredis$ ./configure
vagrant@homestead:/etc/phpredis$ make && make install

Note This is an Extension You Need to Enable to Make it Work  in Php 7
This First Command Will Allow You To Call PHPREDIS Facade in Browser

vagrant@homestead:/etc/phpredis$ sudo vim /etc/php/7.0/fpm/conf.d/redis.ini

Inside Vim Paste this
extension=/etc/phpredis/modules/redis.so

:w!     ->write/save
:q  ->exit vim

NOTE This Extension needs To be Enable in PHP ini to ENABLE it on your Command Line Interface Such As Artisan.
The Usual Problem You Will Face If You Wont do this is Get a Redis Class Not Found

vagrant@homestead:/etc/phpredis$ sudo vim /etc/php/7.0/cli/php.ini
then Look for this Word CLI Server
Type
/CLI Server 

Add to the last line of extension

extension=/etc/phpredis/modules/redis.so
:w!     ->write/save
:q  ->exit vim


vagrant@homestead:/etc/phpredis$ sudo service php7.0-fpm restart

vagrant@homestead:/etc/phpredis$ sudo service nginx restart


To Test if PhpRedis Extension is Working
php -r "if (new Redis() == true){ echo \"\r\n OK \r\n\"; }"

It Should Return OK! 

Good Job You Already Set Up PHPRedis on PHP7 Laravel Homestead Branch

0 likes
10 replies
masterpowers's avatar
masterpowers
OP
Best Answer
Level 5

Make Use of PHPREDIS in Laravel App As Replacement For the Default PREDIS... Which Would Work The Same Configuration as PREDIS...

add this at composer.json
"nardev/laravel-5.1-phpredis": "dev-master"

then composer update


In config/app.php
Uncomment Redis Service Provider
// Illuminate\Redis\RedisServiceProvider::class,

Uncomment Redis Facade
// 'Redis'     => Illuminate\Support\Facades\Redis::class,

Add this as a Replacement for the Uncommented Lines
Nardev\PhpRedis\PhpRedisServiceProvider::class,
'PHPRedis'  => Illuminate\Support\Facades\Redis::class,

Now to Test If it works

Route::get('message', function () {
    $app = PHPRedis::connection();
$app->set("masterpowers", "Yeah Baby Yeah");
print_r($app->get("masterpowers"));
});

You Should Get Result of

Yeah Baby Yeah

To Test Redis PubSub Using PHPREDIS Facade Add this to Your Root Folder and name it socket.js

var app = require('http').createServer(handler);
var io = require('socket.io')(app);
var Redis = require('ioredis');
var redis = new Redis();

app.listen(6001, function() {
    console.log('Server is running!');
});

function handler(req, res) {
    res.writeHead(200);
    res.end('');
}

io.on('connection', function(socket) {});

redis.psubscribe('*', function(err, count) {});

redis.on('pmessage', function(subscribed, channel, message) {
    message = JSON.parse(message);
    io.emit(channel + ':' + message.event, message.data);
});

Now in Your Routes File

Route::get('sender', function () {
    $data = [
        'event' => 'UserSignedUp',
        'data'  => [
            'username' => 'JohnDoe',
        ],
    ];
  
    PHPRedis::publish('test-chanel', json_encode($data));

    return "User Signed Up!";
});

Route::get('receiver', function () {
    return view('receiver');
});

Then Simply Run node socket.js

And For Redis you can type redis-cli --port 6001

Then In Your View if Your Using Vue Js You Can Do this

<!DOCTYPE html>
<html>
    <head>
        <title>Laravel</title>
    </head>
    <body>
        <h1>New Users</h1>

        <ul>
            <li v-repeat="user: users">@{{ user }}</li>
        </ul>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/0.12.16/vue.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.7/socket.io.min.js"></script>

        <script>
        var socket = io(window.location.origin + ':6001');

            new Vue({
                el: 'body',
                data: {
                    users: []
                },
                ready: function() {
                    socket.on('test-chanel:UserSignedUp', function(data) {
                        this.users.push(data.username);
                    }.bind(this));
                }
            });
        </script>
    </body>
</html>

If You Visit /receiver Route You Should See Name "John Doe" is Added in the List Everytime You Hit /sender

I Also Successfully Implement Redis Pub/Sub RealTime Broadcasting Using this Tutorial Inside Laracast!

https://laracasts.com/discuss/channels/general-discussion/step-by-step-guide-to-installing-socketio-and-broadcasting-events-with-laravel-51/

The Only Difference is Im Using PHPREDIS Behind the Scene and Not Predis

Hope Someone Find This Thread useful! Comments and Suggestions Are Welcome

:D

lara30453's avatar

Does this work out of the box on a homestead installation? I thought you had to install PHPRedis separately on homestead as it does not come with homestead?

masterpowers's avatar

Out of the Box a Laravel Homestead PHP7 Branch Dont Have a PHPRedis Set Up. But REDIS and PREDIS. This Guide is How you Set Up and Start Running PHPREDIS in your Laravel Homestead with PHP7.

1 like
lara30453's avatar

Thanks! Have you had any issues with PHP7? Are there are performance or compatibility issues at the moment with laravel 5.1?

masterpowers's avatar

None All is Fully Functional. Ive tested it Already Works 100%

1 like
lara30453's avatar

Great thanks for the update. Guess I will put it into use!

lara30453's avatar

Everything is working, however, as you stated above I have an error saying the Redis class should not be found. I have followed the above and added the extensions to both files. When I use vim to go into this file - sudo vim /etc/php/7.0/fpm/conf.d/redis.ini It's empty, is this the issue?

And here, I don't understand? Should I remove something and replace it with the extension?

vagrant@homestead:/etc/phpredis$ sudo vim /etc/php/7.0/cli/php.ini then Look for this Word CLI Server Type /CLI Server

Add to the last line of extension

extension=/etc/phpredis/modules/redis.so :w! ->write/save :q ->exit vim

masterpowers's avatar

if this is empty

sudo vim /etc/php/7.0/fpm/conf.d/redis.ini

then you need to paste

extension=/etc/phpredis/modules/redis.so

If Your Using Vim /{search} {word} slash will allow you to search any keyword in our case CLI Server

above that there are many extension paste this extension=/etc/phpredis/modules/redis.so

To check if everything is ok then do

php -r "if (new Redis() == true){ echo \"\r\n OK \r\n\"; }"
Henry9162's avatar

This works perfect for me, thanks so so much!... Using it already to store sessions and cache database, but I'm having serious issues with queueing as it pushes my job to redis but not processing it.. When I try to run php artisan queue:work, it throws error: class Redis not found!.. Don't know why it still looks for laravel redis instead of my aliased LRedis.. I'm using tillKruss phpredis... Thanks for the help

zerogpm's avatar

@masterpowers Does this work with laravel5.4? I could not find nardev/laravel-5.4-phpredis

Or There is another package works for 5.4 version. Thanks in advance.

Please or to participate in this conversation.