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.
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
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
This guide assumes that you're using Laravel 7 in a Homestead development environment.
$ 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
.
$ 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
$ sudo make instsall
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 .
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
$ 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
You should now have PhpRedis installed on your server. We will now configure Laravel to begin using PhpRedis.
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.
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".
Once you've installed PhpRedis and updated your Laravel configuration, you should now be able to start using Redis in your project.
// 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. 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.