Home  >  Article  >  Backend Development  >  Steps to install Redis extension in Linux using PHP

Steps to install Redis extension in Linux using PHP

PHPz
PHPzOriginal
2024-03-04 17:45:04461browse

Steps to install Redis extension in Linux using PHP

Installing the Redis extension and configuring PHP in Linux can improve the performance and reliability of the application. Let us learn the steps on how to install the Redis extension in Linux using PHP.

Step 1: Install Redis server

Before you start installing the Redis extension, you first need to install the Redis server. Redis can be installed in the Linux system through the following command:

sudo apt update
sudo apt install redis-server

After the installation is completed, start the Redis server and ensure that it runs automatically when the system starts:

sudo systemctl start redis-server
sudo systemctl enable redis-server

Step 2: Install the PHP extension tool package

Before installing the Redis extension, you need to ensure that the PHP extension toolkit is installed. You can use the following command to install:

sudo apt install php-dev

Step 3: Install the Redis extension

Next, install the Redis extension through PECL. Execute the following command:

sudo pecl install redis

After installation, add the Redis extension configuration in the PHP.ini file. Find the php.ini file (usually in /etc/php/{version number}/apache2/php.ini or /etc/php/{version number}/cli/php.ini) and add the following lines at the end of the file:

extension=redis.so

After saving and exiting the php.ini file, reload the PHP configuration:

sudo systemctl restart apache2

Step 4: Verify Redis extension installation

In order to verify whether the Redis extension is successfully installed, you can create a Simple PHP script for testing. Create a file named test_redis.php with the following content:

<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Redis连接成功!";

After saving the file, run the script through the browser or command line:

php test_redis.php

If "Redis connection successful!" is output, It means that the Redis extension is successfully installed.

Through the above steps, we successfully installed the Redis extension using PHP in the Linux system and verified that it works normally. Using Redis extensions allows us to better handle tasks such as caching and session storage, and improve application performance and response speed. Hope this article helps you!

The above is the detailed content of Steps to install Redis extension in Linux using PHP. 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