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.
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
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.
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!