Current Location: Home> Latest Articles> Guide to Installing PHP Redis Extension on Linux for Fast Redis Caching

Guide to Installing PHP Redis Extension on Linux for Fast Redis Caching

M66 2025-08-05

Installing Redis Server

First, you need to install the Redis server on your Linux system. Open the terminal and run the following commands:

sudo apt update
sudo apt install redis-server
sudo systemctl start redis-server
sudo systemctl status redis-server

Once you confirm the Redis server is running successfully, proceed to the next step.

Installing PHP Redis Extension

Next, install the PHP Redis extension by following these steps:

sudo apt-get install php-redis
sudo nano /etc/php/7.4/cli/php.ini

Add the following line at the end of the php.ini file:

extension=redis.so

Save and exit the editor, then restart the PHP service to apply the changes:

sudo systemctl restart php7.4-fpm

Testing the Redis Extension Installation

Create a PHP test file, for example test_redis.php, with the following content:

<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('test_key', 'Hello, Redis!');
echo $redis->get('test_key');
?>

Run the script using this command:

php test_redis.php

If the output is Hello, Redis!, it means the Redis extension is installed and configured properly and is ready to be used in your PHP projects.

Conclusion

Following these steps, you have successfully installed and configured the PHP Redis extension on your Linux system. Using Redis caching can significantly improve your application's performance and response time. Happy coding!