[英]Installing and configuring PhpRedis for Laravel 7
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.我希望在社区的帮助下,在这里创建并不断改进该指南。
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:真实生产数据的比较
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 文档
This guide assumes that you're using Laravel 7 in a Homestead development environment.本指南假定您在 Homestead 开发环境中使用 Laravel 7。
$ vagrant ssh
$ 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的文件。
$ unzip master.zip
$ rm master.zip
$ sudo mv phpredis-master/ /etc/
$ cd /etc/phpredis-master/
$ 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
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保存并退出。
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
$ 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 successfulnginx:配置文件/etc/nginx/nginx.conf 测试成功
You should now have PhpRedis installed on your server.你现在应该在你的服务器上安装了 PhpRedis。 We will now configure Laravel to begin using PhpRedis.
我们现在将配置 Laravel 以开始使用 PhpRedis。
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 文档中的建议以保持一致性。
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”。
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。
// 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;
}
}
$ 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. $ 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.