Redis is an open-source, in-memory key-value data store, which is widely used for caching, message queue, and real-time data processing. Redis provides many client libraries for different programming languages, including PHP. The phpRedis is a PHP extension that provides a client for Redis in PHP. Installing phpRedis on CentOS is a straightforward process, and this guide will take you through the steps needed to do so.
Step 1: Install the EPEL Repository
The phpRedis package is not available in the default CentOS repositories, so we need to install the Extra Packages for Enterprise Linux (EPEL) repository. To install the EPEL repository, run the following command:
sudo yum install epel-release
Step 2: Install the Redis Server and PHP Development Tools
Before installing the phpRedis extension, we need to install the Redis server and PHP development tools. To install Redis and PHP development tools, run the following command:
sudo yum install redis php-devel php-pear
Step 3: Install the phpRedis Extension
Once the Redis server and PHP development tools are installed, we can proceed with installing the phpRedis extension. To install the phpRedis extension, run the following command:
sudo pecl install redis
This command will download and install the phpRedis extension. During the installation, you will be prompted to provide the Redis server address. If the Redis server is running on the same machine, you can leave the default value and press Enter.
Step 4: Enable the phpRedis Extension
After installing the phpRedis extension, we need to enable it in PHP. To do so, we need to create a new configuration file for phpRedis. Run the following command to create a new configuration file:
sudo nano /etc/php.d/redis.ini
Add the following line to the configuration file:
extension=redis.so
Save and close the file.
Step 5: Restart Apache and Redis Services
To apply the changes, we need to restart the Apache web server and Redis service. To restart Apache, run the following command:
sudo systemctl restart httpd
To restart Redis, run the following command:
sudo systemctl restart redis
Step 6: Verify the Installation
To verify that phpRedis is installed and enabled, create a PHP file with the following content:
Save the file as phpinfo.php in your web server’s document root directory, usually located in /var/www/html/.
Now, open a web browser and navigate to http://your_server_ip/phpinfo.php. You should see a page with PHP information. Scroll down the page, and you should see the Redis section. If everything is installed correctly, you should see the Redis section with the phpRedis extension enabled.
Commands Mentioned:
- yum install – installs packages from CentOS repositories
- pecl install – installs packages from the PHP Extension Community Library (PECL)
- sudo nano – opens a file for editing in the Nano text editor with root privileges
- systemctl restart – restarts a service
Conclusion:
In this guide, we have installed phpRedis for CentOS. First, we installed the EPEL repository, Redis server, and PHP development tools. Then, we installed the phpRedis extension using the PECL tool. Next, we enabled the phpRedis extension by creating a configuration file and restarting the Apache and Redis services. Finally, we verified the installation by checking the phpinfo page.
PhpRedis can be used to communicate with Redis servers from PHP scripts, which can be useful for caching, real-time data processing, and message queues. With phpRedis installed and enabled, you can use its functions to connect to Redis servers and perform various operations on Redis keys.
If you encounter any issues during the installation process or have any questions, feel free to comment below, and I will try my best to help you.