Fatal error: Class 'Redis' not found
Asked Answered
W

6

32

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

Wingover answered 10/8, 2014 at 15:57 Comment(0)
S
43

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
Sulphur answered 29/7, 2018 at 17:38 Comment(6)
...and then restart php-fpm, as suggested below.Dumbwaiter
this is what fixed it for me, but what's the difference between redis and php-redis?Catto
@Catto 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.Sulphur
Do you need to enable redis php extension in some way ?Mauldin
This 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.Sulphur
This did it. Also, I was running Lumen's development server. I killed it and started again after installing php-redia and bingo.Ancon
B
16

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.

Burp answered 28/3, 2018 at 15:56 Comment(1)
On 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.Cesena
W
12

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

Wingover answered 13/8, 2014 at 22:37 Comment(1)
For 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.gitMoraine
G
2

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_'),
    ],
]
Gianna answered 1/10, 2021 at 9:16 Comment(0)
A
1

I tried the answers but was still getting the error. Heres how I solved it.

Step 1: Check the version of PHP that you have enabled by the phpinfo method. Often there are multiple versions of php installed simultaneously. You need to make sure that we are installing the extension for the right version

Step 2: Instead of running the below command

sudo apt-get install php-redis

Try the running the version specific command as below (in my case I had both php8.1 & php8.2 & the system was defaulting to php8.2 while the php8.1 was active and enabled.

sudo apt-get install php8.1-redis

Thats all. After restarting the web-server the php had access to redis class.

Ardyce answered 13/6, 2023 at 18:5 Comment(0)
A
0

make sure to install a stable version of redis from there: https://pecl.php.net/package/redis

Atlante answered 6/2, 2022 at 21:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.