7. Redis 101: Running Redis on Linux and Connecting it to PHP via PRedis - SkillBakery Studios

Breaking

Post Top Ad

Post Top Ad

Tuesday, July 14, 2026

7. Redis 101: Running Redis on Linux and Connecting it to PHP via PRedis

7. Redis 101: Running Redis on Linux and Connecting it to PHP via PRedis

Screenshot from the tutorial
Screenshot from the tutorial

Redis 101: Running Redis on Linux and Connecting it to PHP via PRedis

In this tutorial, we will explore the basics of Redis, a powerful in-memory data structure store often used as a database, cache, and message broker. We will guide you through the process of installing Redis on a Linux system and connecting it to a PHP application using the PRedis library. By the end of this post, you'll be equipped to harness the full potential of Redis within your PHP projects.

What is Redis?

Redis stands for Remote Dictionary Server. It is an open-source, in-memory key-value data store, known for its high performance, flexibility, and rich feature set. Redis supports various data structures such as strings, hashes, lists, sets, and sorted sets, making it suitable for many applications, from caching to real-time analytics.

Prerequisites

Before we begin, ensure you have the following:

  • A Linux-based operating system (Ubuntu, CentOS, etc.)
  • PHP installed on your system
  • Access to the terminal with sudo privileges

Step 1: Installing Redis on Linux

To install Redis on your Linux machine, follow these steps:

For Ubuntu

  1. Update Package Index:

    sudo apt update
    
  2. Install Redis Server:

    sudo apt install redis-server
    
  3. Start Redis Service:

    sudo systemctl start redis.service
    
  4. Enable Redis to Start on Boot:

    sudo systemctl enable redis.service
    
  5. Verify Redis Installation: Run the Redis CLI to check if the server is running:

    redis-cli ping
    

    If Redis is running correctly, it should respond with:

    PONG
    

For CentOS

  1. Install EPEL Repository:

    sudo yum install epel-release
    
  2. Install Redis:

    sudo yum install redis
    
  3. Start Redis Service:

    sudo systemctl start redis
    
  4. Enable Redis to Start on Boot:

    sudo systemctl enable redis
    
  5. Verify Redis Installation:

    redis-cli ping
    

    You should see:

    PONG
    

Step 2: Installing PRedis for PHP

PRedis is a robust PHP extension for interacting with Redis. To install it, follow these steps:

  1. Install Composer: If you haven't installed Composer yet, you can do so by running:

    curl -sS https://getcomposer.org/installer | php
    sudo mv composer.phar /usr/local/bin/composer
    
  2. Create a New PHP Project: Navigate to your desired directory and create a new project:

    mkdir my-redis-app
    cd my-redis-app
    
  3. Install PRedis using Composer:

    composer require predis/predis
    

Step 3: Connecting PHP to Redis

Now that we have Redis installed and the PRedis library ready, let’s create a simple PHP script to connect to Redis and perform basic operations.

  1. Create a PHP File: Create a file named redis_test.php in the project directory:

    touch redis_test.php
    
  2. Add the Following Code: Open the file in your preferred text editor and add the following code:

    <?php
    require 'vendor/autoload.php';
    
    // Connect to Redis
    $client = new Predis\Client();
    
    // Set a value in Redis
    $client->set('key', 'Hello, Redis!');
    
    // Get the value from Redis
    $value = $client->get('key');
    
    // Display the value
    echo "The value of 'key' is: " . $value . PHP_EOL;
    ?>
    
  3. Run the PHP Script: Execute the script in the terminal:

    php redis_test.php
    

    You should see the output:

    The value of 'key' is: Hello, Redis!
    

Conclusion

In this tutorial, we successfully installed Redis on a Linux machine and connected it to a PHP application using the PRedis library. Redis provides a robust and efficient way to handle data in memory, making it an excellent choice for various applications.

Feel free to explore more advanced features of Redis and the PRedis library to take your projects to the next level. Whether you're implementing caching, session storage, or real-time messaging, Redis can significantly enhance your application's performance and scalability. Happy coding!

Another screenshot from the tutorial
Another view from the tutorial

Connect with SkillBakery Studios

Explore more tutorials, tools, and resources:

Posted by SkillBakery Studios

No comments:

Post a Comment

Post Top Ad