Redis is an in-memory data store commonly used for caching, session storage, message queues, and high-performance database operations. Many PHP applications such as WordPress, Laravel, Magento, and custom web applications use Redis to improve performance and reduce database load.

This guide explains how to install and enable the Redis PHP extension on an Ubuntu server running Nginx and PHP-FPM.

Prerequisites

Before proceeding, ensure the following components are installed:

  • Ubuntu Server
  • Nginx Web Server
  • PHP and PHP-FPM
  • Git
  • Build tools (php-dev, make, gcc)

If Nginx, PHP, or MariaDB are not installed, refer to:

Install Nginx, PHP, and MariaDB on Ubuntu

Step 1: Install Required Packages

Update the package repository and install the required development tools.

sudo apt update

sudo apt install -y \
    git \
    php-dev \
    php-pear \
    build-essential

Step 2: Download the Redis PHP Extension Source

Navigate to the source directory and clone the Redis PHP extension repository.

cd /usr/local/src

git clone https://github.com/phpredis/phpredis.git

Step 3: Build and Install the Extension

Move to the cloned directory.

cd /usr/local/src/phpredis

Prepare the build environment.

phpize

Configure the extension.

./configure

Compile and install.

make

sudo make install

The installation will place the Redis extension (redis.so) into the PHP extensions directory.

Step 4: Enable the Redis Extension

Create the Redis configuration file.

For PHP 7.0:

echo "extension=redis.so" | sudo tee /etc/php/7.0/mods-available/redis.ini

Enable the module.

sudo phpenmod redis

Step 5: Restart Services

Restart PHP-FPM and Nginx to load the newly installed extension.

sudo systemctl restart php7.0-fpm

sudo systemctl restart nginx

Step 6: Verify the Installation

Create a PHP information page.

sudo vi /var/www/html/info.php

Add the following content:

<?php
phpinfo();
?>

Save the file and access it from a browser:

http://YOUR_SERVER_IP/info.php

Search for Redis on the page. If the extension is installed correctly, a Redis section will be displayed.

Alternative Verification

You can also verify the extension directly from the command line:

php -m | grep redis

Expected output:

redis

Or:

php --ri redis

This command displays detailed Redis extension information.

Security Recommendation

After verification, remove the info.php file to prevent exposing server configuration details.

sudo rm -f /var/www/html/info.php

Conclusion

You have successfully installed and enabled the Redis PHP extension on an Ubuntu server running Nginx and PHP-FPM. Your PHP applications can now connect to Redis for caching, session storage, and performance optimization.

For production environments, consider installing the Redis server package and configuring persistence, authentication, and memory limits according to your application requirements.

Leave a Reply