Home  >  Article  >  Backend Development  >  A complete guide to installing Redis extensions on Linux

A complete guide to installing Redis extensions on Linux

PHPz
PHPzOriginal
2024-03-05 10:54:03805browse

A complete guide to installing Redis extensions on Linux

Complete guide to installing Redis extensions under Linux system

Redis is an open source, high-performance key-value storage database that is widely used in Scenarios such as caching and message queues. Installing the Redis extension under the Linux system allows us to use the Redis database more efficiently. This article will introduce in detail how to install the Redis extension under Linux system and provide specific code examples.

Step 1: Install Redis

  1. Open the terminal and use the following command to install the Redis server:
sudo apt update
sudo apt install redis-server
  1. Start the Redis server:
sudo systemctl start redis-server
  1. Confirm that the Redis server has been started:
sudo systemctl status redis-server

Step 2: Install the PHP extension

  1. Open the terminal and use the following command to install Redis extension for PHP:
sudo apt-get install php-redis
  1. Open the PHP configuration file and add the Redis extension:
sudo nano /etc/php/7.4/cli/php.ini

Add the following content at the end of the file:

extension=redis.so

Save and exit.

  1. Restart the PHP service:
sudo systemctl restart php7.4-fpm

Step 3: Test the Redis extension

  1. Create a PHP file, such as test_redis. php, enter the following code:
<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('test_key', 'Hello, Redis!');
echo $redis->get('test_key');
?>
  1. Execute the PHP script:
php test_redis.php

If everything goes well, Hello, Redis! , indicating that the Redis extension has been successfully installed and connected to the Redis server.

Summary

Through the above steps, you have successfully installed the Redis extension under the Linux system and conducted a simple test. Now you can use Redis database in PHP projects to improve performance and efficiency. Hope this article is helpful to you!

The above is the detailed content of A complete guide to installing Redis extensions on Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn