简体   繁体   English

为 Laravel 7 安装和配置 PhpRedis

[英]Installing and configuring PhpRedis for Laravel 7

Configuring Laravel 7 For Use With PhpRedis配置 Laravel 7 以与 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.按照 Laravel 的建议,在互联网上搜索以试图弄清楚如何正确安装和使用 PhpRedis 之后,我找不到一个没有让我问题多于答案的单一事实来源或指南。

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.提供分步指南,以最正确和面向未来的方式在 Homestead 上为 Laravel 7 安装和配置 PhpRedis。
  • Explain how to use Redis now that PhpRedis is installed/configured.说明如何使用 Redis 现在 PhpRedis 已安装/配置。

Why not just use Predis?为什么不直接使用 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.根据 Laravel 7 文档,Predis 似乎已被废弃,并且在 Laravel 的未来版本中,它可能不再受支持。 Additionally, PhpRedis outperforms Predis.此外,PhpRedis 的性能优于 Predis。

PhpRedis vs Predis: Comparison on real production data PhpRedis vs Predis:真实生产数据的比较

References参考

How to Install PhpRedis for Laravel on Ubuntu如何在 Ubuntu 上为 Laravel 安装 PhpRedis

PhpRedis in Laravel - Redis Series Episode 2 Laravel - Redis 系列中的 PhpRedis 第 2 集

Installing PHP REDIS PHP7 Branch On Fresh Install Homestead PHP7 在全新安装 Homestead PHP7 上安装 PHP REDIS PHP7 分支

Laravel 7 Redis Docs Laravel 7 Redis 文档


Installing/configuring PhpRedis on a fresh install of Laravel 7 on Homestead在 Homestead 上全新安装 Laravel 7 时安装/配置 PhpRedis

This guide assumes that you're using Laravel 7 in a Homestead development environment.本指南假定您在 Homestead 开发环境中使用 Laravel 7。

Installing the PhpRedis package.安装 PhpRedis package。

1. SSH into your homestead. 1. SSH 进入你的家园。

$ vagrant ssh

2. Download the PhpRedis package. 2. 下载 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 .下载完成后,您应该使用命令ls看到一个名为master.zip的文件。

3. Extract the phpredis-master folder from master.zip then delete master.zip. 3.从master.zip中提取phpredis-master文件夹,然后删除master.zip。 Move the extracted folder to your /etc/ directory.将提取的文件夹移动到您的/etc/目录。 Then navigate to the extracted phpredis-master folder.然后导航到提取的 phpredis-master 文件夹。

$ 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. 4. 为 PhpRedis PHP 扩展准备构建环境。

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

If you get an error recipe for target 'install-modules' failed , use sudo如果您收到recipe for target 'install-modules' failed ,请使用 sudo

$ sudo make instsall

5. Update the redis.ini file using using Vim or your preferred editor. 5. 使用 Vim 或您的首选编辑器更新 redis.ini 文件。

Make sure to replace 7.4 with the version of PHP you're using.确保将7.4替换为您正在使用的 PHP 版本。 You can check by running php -v .您可以通过运行php -v进行检查。 In my case it returned PHP 7.4.4 .就我而言,它返回PHP 7.4.4 I then ran ls /etc/php/ to list my PHP folders and found the /etc/php/7.4/ directory.然后我运行ls /etc/php/列出我的 PHP 文件夹并找到/etc/php/7.4/目录。

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

You should now have the file opened in your Vim editor.您现在应该在 Vim 编辑器中打开该文件。 Copy the line below and then press i in Vim to begin inserting.复制下面的行,然后在 Vim 中按i开始插入。 Then paste by either right-clicking and choosing paste, or by pressing Shift+Ins .然后通过右键单击并选择粘贴或按Shift+Ins进行粘贴。

extension=/etc/phpredis-master/modules/redis.so扩展=/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 .添加该行后,按Esc退出编辑模式,然后按:wq然后按 Enter保存并退出。

6. Restart services 6.重启服务

Make sure you're in the directory /etc/phpredis-master/确保你在目录/etc/phpredis-master/

Again, replace 7.4 with the version of PHP you're using.同样,将 7.4 替换为您正在使用的 PHP 版本。

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

7. Make sure everything is working. 7. 确保一切正常。

$ sudo nginx -t

This should output the below if successful.如果成功,这应该是下面的 output。

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx:配置文件/etc/nginx/nginx.conf语法没问题
nginx: configuration file /etc/nginx/nginx.conf test is successful nginx:配置文件/etc/nginx/nginx.conf 测试成功

Configuring Laravel 7 to use PhpRedis.配置 Laravel 7 以使用 PhpRedis。

You should now have PhpRedis installed on your server.你现在应该在你的服务器上安装了 PhpRedis。 We will now configure Laravel to begin using PhpRedis.我们现在将配置 Laravel 以开始使用 PhpRedis。

1. Change the Redis alias to RedisManager . 1. 将Redis别名更改为RedisManager

Open config/app.php and then in the aliases array change Redis to RedisManager打开config/app.php然后在别名数组中将Redis 更改为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.您可以将 Redis 重命名为 RedisManager 以外的名称,但我只是遵循 Laravel 文档中的建议以保持一致性。

Test that Redis is working测试 Redis 是否正常工作

Add the following to your web.php routes file and then navigate to the route in your application.将以下内容添加到 web.php 路由文件中,然后导航到应用程序中的路由。

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".导航到yoursite.local/redis时,您应该会看到消息“Testing PhpRedis”。

Using Redis in your projects在您的项目中使用 Redis

Once you've installed PhpRedis and updated your Laravel configuration, you should now be able to start using Redis in your project.安装 PhpRedis 并更新 Laravel 配置后,您现在应该可以开始在项目中使用 Redis。

Using Redis n your controller在您的 controller 中使用 Redis

// 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?为什么我们必须将 Redis 别名重命名为 RedisManager?

Again, since Laravel 7 uses PhpRedis by default, why do we have to rename the Redis alias to RedisManager?再说一遍,既然 Laravel 7 默认使用 PhpRedis,为什么我们要重命名 Redis 别名为 RedisManager?

How will renaming the Redis alias to RedisManager affect my application and how I use it?将 Redis 别名重命名为 RedisManager 将如何影响我的应用程序以及我如何使用它?

Laravel 7 says that PhpRedis is the default, so why do I have to do all of this? Laravel 7 说 PhpRedis 是默认的,那我为什么要做这一切呢?

I've successfully got PhpRedis working locally, but when I deploy on Vapor, it doesn't work.我已经成功地让 PhpRedis 在本地工作,但是当我在 Vapor 上部署时,它不起作用。 What do I do?我该怎么办?

What if I'm not using Homestead?如果我不使用 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. 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:您可以使用以下命令简化安装 PHPRedis 的部分:

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.上面的示例首先将安装默认模块 PHPRedis,然后安装 PHPRedis 扩展的特定 PHP 版本。 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 .所以这个例子是 PHP 8.0,如果你需要 PHP 7.3 版本,你应该将php8.0-redis更改为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.如果您使用 Laravel/Forge,则不需要安装 Phpredis,因为 Forge 在服务器配置期间安装了它。

You will find the extension=redis.so already exists in sudo nano /etc/php/7.4/fpm/conf.d/20-redis.ini你会发现extension=redis.so已经存在于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.过程的rest应该和教程差不多,不过我自己还没有测试过。 Will post back my findings.将发回我的发现。

Really good guide for installing on Homestead locally.在本地安装 Homestead 的非常好的指南。 For ease I would run sudo su after vagrant ssh so that you can run everything as root.为方便起见,我会在vagrant ssh之后运行sudo su ,以便您可以以 root 身份运行所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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