Install phpredis on CentOS 8 / RHEL 8

Last modified on October 30, 2020 1 min read

We're available to do freelance project. Take a look at our services!

In this article, I’m going to show how to install phpredis on RHEL / CentOS 8. Let’s get started:

Table of Contents

  1. Install Redis
  2. Install phpredis
  3. Open Port & Config SELinux
  4. Store and Fetch Data

Install Redis

At first, we need to install Redis on our server. To do that, please read this article first: Install & Configure Redis on RHEL / CentOS 8.

Install phpredis

Before installing phpredis, we need to install some dependencies. Let’s install those:

# yum-utils
sudo dnf -y install yum-utils

# epel repo
sudo dnf -y install

# remi repo
sudo dnf -y install

Now we’re able to install phpredis:

sudo dnf -y install php-pecl-redis5

We need to restart webserver:

# apache
sudo systemctl restart httpd

# nginx
sudo systemctl restart nginx

# php-fpm if installed
sudo systemctl restart php-fpm

Open Port & Config SELinux

By default Redis is running on port 6379. We need to allow this port from firewall:

# firewalld
sudo firewall-cmd --add-port=6379/tcp --permanent
sudo firewall-cmd --reload

# ufw
sudo ufw allow 6379/tcp

If you’re using another firewall, then open 6379 port from the firewall.

If SELinux is enabled, we’ve to run this command to work Redis in PHP file:

setsebool -P httpd_can_network_connect on

Store and Fetch Data

Create a file named cache.php and paste this code:

// connecting to Redis server on localhost 
$redis = new Redis(); 
$redis->connect('', 6379); 
echo "Connection successful<br/><br/>"; 

// caching
$cache_name = "test_redis_cache";
$expire_time = 10;

if ($redis->exists($cache_name)) {
    echo 'Load from cache:<br>';
    $data = $redis->get($cache_name);
else {
    echo 'Cache missed!<br>';
    $data = Date("d M, Y - h:i:s");
    $redis->set($cache_name, $data);
    $redis->expire($cache_name, $expire_time); // will disappear in 10 seconds.

echo $data;

*** Check phpredis official GitHub repository to know more.

That’s it. Thanks for reading. 🙂


Hey, I'm Md Obydullah. I build open-source projects and write on Laravel, Linux server, modern JavaScript and more on web development.


Leave a Reply

Your email address will not be published. Required fields are marked *