简体   繁体   中英

Installing and configuring PhpRedis for Laravel 7

Configuring Laravel 7 For Use With PhpRedis

Preface

After scouring the internet in an attempt to figure out how to properly install and use PhpRedis, as recommended by Laravel, I was unable to find a single source of truth or guide that didn't leave me with more questions than answers.

I'm hoping, with the help of the community, to create and continuously improve that guide here.

Goals of this guide

  • Provide a step-by-step guide for installing and configuring PhpRedis for Laravel 7 on Homestead in the most correct and future-proof way possible.
  • Explain how to use Redis now that PhpRedis is installed/configured.

Why not just use Predis?

As per the Laravel 7 docs, Predis seems to have been abandoned and in future versions of Laravel, it may no longer be supported. Additionally, PhpRedis outperforms Predis.

PhpRedis vs Predis: Comparison on real production data

References

How to Install PhpRedis for Laravel on Ubuntu

PhpRedis in Laravel - Redis Series Episode 2

Installing PHP REDIS PHP7 Branch On Fresh Install Homestead PHP7

Laravel 7 Redis Docs


Installing/configuring PhpRedis on a fresh install of Laravel 7 on Homestead

This guide assumes that you're using Laravel 7 in a Homestead development environment.

Installing the PhpRedis package.

1. SSH into your homestead.

$ vagrant ssh

2. Download the PhpRedis package.

$ wget https://github.com/phpredis/phpredis/archive/master.zip

Note: This link may be deprecated. If so, check for the most recent release

https://github.com/phpredis/phpredis/releases

After the download completes, you should see a file called master.zip by using the command ls .

3. Extract the phpredis-master folder from master.zip then delete master.zip. Move the extracted folder to your /etc/ directory. Then navigate to the extracted phpredis-master folder.

$ unzip master.zip
$ rm master.zip
$ sudo mv phpredis-master/ /etc/
$ cd /etc/phpredis-master/

4. Prepare the build environment for the PhpRedis PHP extension.

$ phpize
$ ./configure
$ make && make install

If you get an error recipe for target 'install-modules' failed , use sudo

$ sudo make instsall

5. Update the redis.ini file using using Vim or your preferred editor.

Make sure to replace 7.4 with the version of PHP you're using. You can check by running php -v . In my case it returned PHP 7.4.4 . I then ran ls /etc/php/ to list my PHP folders and found the /etc/php/7.4/ directory.

$ sudo vim /etc/php/7.4/fpm/conf.d/redis.ini

You should now have the file opened in your Vim editor. Copy the line below and then press i in Vim to begin inserting. Then paste by either right-clicking and choosing paste, or by pressing Shift+Ins .

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

Once you've added the line, exit editing mode by pressing Esc and then save and quit by pressing :wq and then Enter .

6. Restart services

Make sure you're in the directory /etc/phpredis-master/

Again, replace 7.4 with the version of PHP you're using.

$ sudo service php7.4-fpm restart
$ sudo service nginx restart

7. Make sure everything is working.

$ sudo nginx -t

This should output the below if successful.

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Configuring Laravel 7 to use PhpRedis.

You should now have PhpRedis installed on your server. We will now configure Laravel to begin using PhpRedis.

1. Change the Redis alias to RedisManager .

Open config/app.php and then in the aliases array change Redis to RedisManager

'aliases' => [
    ...
    'RedisManager' => Illuminate\Support\Facades\Redis::class,
    ...
]

You can rename Redis to something other than RedisManager, but I've just followed the suggestion in the Laravel docs for consistency.

Test that Redis is working

Add the following to your web.php routes file and then navigate to the route in your application.

Route::get('/redis', function () {
    $app = RedisManager::connection();
    $app->set('key', 'Testing PhpRedis');
    echo $app->get('key');
});

When navigating to yoursite.local/redis , you should see the message, "Testing PhpRedis".

Using Redis in your projects

Once you've installed PhpRedis and updated your Laravel configuration, you should now be able to start using Redis in your project.

Using Redis n your controller

// ExampleController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use RedisManager;

class ExampleController extends Controller
{
    public function index()
    {
        $visits = RedisManager::incr('visits');

        return $visits;
    }
}

FAQ

Why exactly do we have to rename the Redis alias to RedisManager?

Again, since Laravel 7 uses PhpRedis by default, why do we have to rename the Redis alias to RedisManager?

How will renaming the Redis alias to RedisManager affect my application and how I use it?

Laravel 7 says that PhpRedis is the default, so why do I have to do all of this?

I've successfully got PhpRedis working locally, but when I deploy on Vapor, it doesn't work. What do I do?

What if I'm not using Homestead?


Todo

  • Research if downloading the PhpRedis package with $ wget https://github.com/phpredis/phpredis/archive/master.zip is the optimal way of going about installing the package, or if there's a better option such as possibly installing via PECL.

You can simplify the part for installing PHPRedis by using the following commands:

vagrant ssh
sudo apt-get install php-redis
sudo apt-get install php8.0-redis

The example above first will install the default module PHPRedis and then the specific PHP version of the PHPRedis extension. So the example is for PHP 8.0, and if you need a PHP 7.3 version you should change php8.0-redis into php7.3-redis .

If you are using Laravel/Forge, you do not need to install Phpredis as it is being installed by Forge during server provisioning.

You will find the extension=redis.so already exists in sudo nano /etc/php/7.4/fpm/conf.d/20-redis.ini

The rest of the process should be similar to the guide, but I have yet to test it myself. Will post back my findings.

Really good guide for installing on Homestead locally. For ease I would run sudo su after vagrant ssh so that you can run everything as root.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM