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
After the installation is completed, you can start the Redis server through the following command:
sudo systemctl start redis-server
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
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
Add the following lines to the found php.ini file:
extension=redis.so
After saving and exiting, restart the PHP-FPM service for the changes to take effect :
sudo systemctl restart php-fpm
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(); ?>
Save the code as test_redis.php and execute it in the command line:
php test_redis.php
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!