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

A complete guide to installing Redis extensions on Linux

PHPz
Release: 2024-03-05 10:56:01
Original
814 people have browsed it

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
Copy after login
  1. Start the Redis server:
sudo systemctl start redis-server
Copy after login
  1. Confirm that the Redis server has been started:
sudo systemctl status redis-server
Copy after login

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
Copy after login
  1. Open the PHP configuration file and add the Redis extension:
sudo nano /etc/php/7.4/cli/php.ini
Copy after login

Add the following content at the end of the file:

extension=redis.so
Copy after login

Save and exit.

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

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');
?>
Copy after login
  1. Execute the PHP script:
php test_redis.php
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template