Complete Tutorial: Installing the Redis Extension for PHP on a Linux System

WBOY
Release: 2024-03-04 11:34:02
Original
656 people have browsed it

Complete Tutorial: Installing the Redis Extension for PHP on a Linux System

Installing PHP’s Redis extension on Linux systems

Redis is an open source in-memory database that is widely used in scenarios such as caching, session management, and message queues. In PHP development, the Redis extension can be used to easily interact with the Redis database. This article will introduce you to how to install PHP's Redis extension on a Linux system, with specific code examples.

Step One: Install Redis Server

First, you need to install the Redis server on your Linux system. It can be installed through the package management tool. For example, on the Ubuntu system, you can use the following command:

sudo apt-get update
sudo apt-get install redis-server
Copy after login

After the installation is completed, you can start the Redis server through the following command:

sudo systemctl start redis-server
Copy after login

Step 2: Install PHP Redis extension

Next, you need to install the Redis extension for PHP. You can install the Redis extension through PECL and execute the following command:

pecl install redis
Copy after login

After the installation is complete, you need to add the Redis extension to the PHP configuration file. The location of php.ini can be found with the following command:

php --ini
Copy after login

Add the following lines to the found php.ini file:

extension=redis.so
Copy after login

After saving and exiting, restart the PHP-FPM service for the changes to take effect :

sudo systemctl restart php-fpm
Copy after login

Step 3: Test whether the Redis extension is installed successfully

In order to verify whether the Redis extension is successfully installed, you can write a simple PHP script to connect to Redis and perform some operations. The following is a sample code:

<?php

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

// 设置一个key-value
$redis->set('test_key', 'Hello, Redis!');

// 获取key对应的值
$value = $redis->get('test_key');
echo $value;

// 关闭连接
$redis->close();

?>
Copy after login

Save the code as test_redis.php and execute it in the command line:

php test_redis.php
Copy after login

If the output is "Hello, Redis!", it means that the Redis extension has been Installed successfully and works fine.

At this point, you have successfully installed the PHP Redis extension on the Linux system and verified its normal working through specific code examples. I hope this tutorial can help you successfully use Redis to develop in PHP projects.

The above is the detailed content of Complete Tutorial: Installing the Redis Extension for PHP on a Linux System. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!