Fatal error: Class 'Redis' not found
Solution 1
You have installed redis but not php-redis. you can simply run the command below to install php-redis
sudo apt-get install php-redis
Solution 2
TLDR;
On Ubuntu 16.04 using NGINX with PHP 7 I found that PHP-FPM was not restarted. A simple restart of the service worked for me:
sudo service php-fpm restart
OR
sudo service php7.0-fpm restart
OR
sudo service php5-fpm restart
You may need to need to search "restart PHP-FPM" in case any of the above commands don't work for you.
To give some context, I had installed phpredis using the standard sudo apt-get install php-redis
and I restarted nginx using sudo systemctl restart nginx
but whenever trying to use new Redis()
in PHP I received the same error as in the question (... Class 'Redis' not found...).
When running phpinfo();
in a PHP file on the NGINX server I could see the PHP-FPM was loading additional configuration from /etc/php/7.0/fpm/conf.d
("Scan this dir for additional .ini files" section). Looking in that directory with a simple ls -al /etc/php/7.0/fpm/conf.d
I can see there is a symlink named 20-redis.ini
but that file was not being loaded in the phpinfo section "Additional .ini files parsed".
The problem as I see it now is that restarting NGINX did not restart PHP-FPM. Using ps aux | grep php-fpm
to see if there were any PHP-FPM processes running when I had stopped NGINX confirmed my suspicions. Because a restart is required to reload the PHP modules the PHP-FPM restart was required in addition to the NGINX restart.
Solution 3
Manual instalation of PhpRedis solved this problem
git clone git://github.com/nicolasff/phpredis.git
cd phpredis
phpize
./configure
make
sudo -s make install
sudo -s
echo "extension=redis.so">/etc/php5/conf.d/redis.ini
ln -s /etc/php5/conf.d/redis.ini /etc/php5/fpm/conf.d/20-redis.ini
exit
copied from Rico's Tech Memo
Solution 4
composer require predis/predis
Then add "predis" in app/config/database.php :
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME',
'laravel'), '_').'_database_'),
],
]
Related videos on Youtube
Admin
Updated on February 07, 2022Comments
-
Admin about 2 years
I've installed Redis on my ubuntu 14 server with phpredis extension. Im using Nginx server. I have php testing script
$redis=new Redis() or die("Cannot load Redis module."); $redis->connect('localhost'); $redis->set('random', rand(5000,6000)); echo $redis->get('random');
which is working fine from command-line but not from web browser.
Nginx error log:
[info] 31102#0: Using 32768KiB of shared memory for push module in /etc/nginx/nginx.conf:82 [error] 31108#0: *21 FastCGI sent in stderr: "PHP message: PHP Fatal error: Class 'Redis' not found in...
i can't see it even in
phpinfo()
all installed with apt-get install
all other modules (mysql, imagemagick...) are working fine
i've spent few hours on google but haven't found nothing useful
-
naomi about 5 years...and then restart php-fpm, as suggested below.
-
abbood over 4 yearsthis is what fixed it for me, but what's the difference between redis and php-redis?
-
Kareem over 4 years@abbood redis installs redis on your server. php-redis allows you to pull and manipulate redis data using php. You can run redis on the command line without php-redis.
-
Pat Gilmour over 4 yearsOn Ubuntu 18, use this to restart php-fpm
sudo systemctl restart php7.2-fpm.service
replacing "7.2" with your version of php. The above solution worked on my server after hours of head-scratching. Restarting nginx was not enough. -
Serhii Popov about 4 yearsFor googlers, as mention in this issues: PHP5 is not supported anymore. 4.3.0 was the last release which works with older versions of PHP. Use next command for clone correct branch
git clone -b 4.3.0 --single-branch https://github.com/phpredis/phpredis.git
-
famas23 about 4 yearsDo you need to enable redis php extension in some way ?
-
Kareem about 4 yearsThis Fatal Error barely means that php can't execute commands the same way you do on the console. To enable the bridge between php and redis, install php-redis. If redis works in the console, it will work through php without any extra configuration.
-
Qumber about 4 yearsThis did it. Also, I was running Lumen's development server. I killed it and started again after installing php-redia and bingo.