Sunday, April 28, 2024

Installing Redis Server with PHP Extension on Ubuntu

Assalamu Alaikum.
Welcome to Uddoyon, in this article we know about How to Installing Redis Server with PHP Extension for Nginx Environment on Ubuntu System so keep reading…

Redis is an open-source, in-memory data structure store that can be used as a database, cache, and message broker. When integrated with PHP and Nginx, Redis can significantly enhance the performance of web applications by providing fast and efficient data storage and retrieval. In this article, we’ll walk through the steps to install Redis Server and the Redis PHP extension in an Nginx environment.

- Advertisement -

Prerequisites:
Before we begin, ensure that you have the following prerequisites:

  1. A server with Nginx installed.
  2. PHP installed on your server.
  3. Administrative access to your server.

Step 1: Install Redis Server:
To install Redis Server, follow these steps:

- Advertisement -
sudo apt update
sudo apt install redis-server -y

After the installation is complete, start the Redis service and enable it to start on boot:

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

Step 2: Install Redis PHP Extension:
Now, let’s install the Redis PHP extension. This extension allows PHP to communicate with the Redis server. (for Multi-PHP: replace php-redis to php7.x/8.x-redis)

- Advertisement -
sudo apt install php-redis -y

After the installation, restart the PHP and Nginx services to apply the changes:

sudo systemctl restart php-fpm
sudo systemctl restart nginx

Step 3: Configure PHP to Use Redis: (Optional)
Open the PHP configuration file using a text editor of your choice. In this example, we’ll use nano:

sudo nano /etc/php/8.x/fpm/php.ini

Add the following line to enable the Redis extension:

extension=redis.so

Save the file and exit the editor. Restart the PHP service to apply the changes:

sudo systemctl restart php-fpm

Step 4: Test Redis Integration:
Create a simple PHP script to test the Redis integration. For example, create a file named test-redis.php in your Nginx web root:

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

echo 'Connected to Redis Server';

$redis->set('example_key', 'Hello, Redis!');
$value = $redis->get('example_key');

echo 'Value from Redis: ' . $value;
?>

Access this script in your web browser (e.g., http://yourdomain.com/test-redis.php) to verify that PHP can connect to Redis and retrieve data.

Congratulations! You’ve successfully installed and configured Redis Server with the Redis PHP extension in an Nginx environment. Utilizing Redis for caching and data storage can greatly enhance the performance and scalability of your web applications.

- Advertisement -
- Advertisement -
Related Articles
- Advertisement -

Popular Articles

- Advertisement -
error: Content is protected !!