HiBlog Hosting and cPanel

How to Connect PHP and WordPress to Redis in cPanel

  • Ali GhanimatiAuthor
  • Last updated
  • 4 minutesReading time
  • 569Views
  • Ali Ghanimati
  • Last updated:
  • 4 min read
  • 569 views
How to Connect PHP and WordPress to Redis in cPanel

Your WordPress shop sends dozens of repeated queries to the database for every page, and at busy hours the dashboard and the cart slow down. You’ve heard Redis takes that load off: it keeps frequently used data in RAM so it isn’t read from the database every time. The good news is that on cPanel hosting you don’t need a separate server to use Redis; you just need to know where to turn it on and how to connect your application to it properly.

In this tutorial you’ll connect to Redis in cPanel step by step: how Redis differs from MySQL, turning on PHP’s redis extension, getting the socket path, connecting the WordPress cache, and correct sample code for PHP and Python.

Short answer: in cPanel, turn on the redis extension under Select PHP Version; that’s only the PHP client. On shared hosting, Redis itself is usually reached through a socket file (redis.sock), not an IP and port; get its path from your host’s support. Then put that path in place of the Host in your WordPress cache plugin or your own code.

What is Redis and how is it different from MySQL?

Redis is a key-value database in RAM. MySQL is where your main data lives: users, orders and posts stay there on disk. Next to it, Redis holds frequently used or temporary data such as query results, user sessions and cache, so the application doesn’t go to the database every time. So Redis doesn’t replace MySQL; it complements it. To learn more, read What Is Redis.

MySQLRedis
Where data livesOn disk, permanentIn RAM, mostly temporary
Used forThe site’s main dataCache, sessions and queues
SpeedGoodVery fast

1. Turn on PHP’s redis extension

  1. In cPanel, click Select PHP Version in the Software section.
  2. At the top of the page, check that Current PHP version is set to a specific version, such as 8.2, not native; the extensions of the native version can’t be changed.
  3. Go to the Extensions tab and tick redis; the change is saved automatically.
The Select PHP Version option in cPanel’s Software section
Choosing a specific PHP version under Current PHP version instead of native
The redis extension ticked on the Extensions tab of Select PHP Version

This extension is only the PHP client, meaning PHP can talk to Redis. The Redis service itself must also be active for your account.

2. Get the Redis socket path

On shared hosting, Redis runs separately for each account, and applications usually connect to it through a socket file: a path inside your account’s folder that ends in redis.sock. This is both faster and keeps your Redis separate from other accounts. Ask your host’s support for your account’s exact socket path and keep it in your project’s private settings.

3. Connect WordPress to Redis

For WordPress, turn on the object cache with one of these two plugins:

  • LiteSpeed Cache: under Cache → Object, turn Object Cache on, set Method to Redis, enter the socket path as the Host and 0 as the Port, and save. The connection status is shown right there.
  • Redis Object Cache: add these two lines to wp-config.php, before the That’s all, stop editing line, then click Enable Object Cache in the plugin’s settings.
define( 'WP_REDIS_SCHEME', 'unix' );
define( 'WP_REDIS_PATH', '/home/USERNAME/path/redis.sock' );

Turn on only one of the two; two object cache plugins conflict with each other.

4. Connect a PHP or Python application

In PHP with the redis extension, you give the socket path instead of an IP:

<?php
$redis = new Redis();
$redis->connect('/home/USERNAME/path/redis.sock');
$redis->set('test:hello', 'ok', 30);
echo $redis->get('test:hello');

In Python, install the redis library in your app’s environment and connect like this. If you run Python apps in cPanel, see How to Run a Flask Project on cPanel.

import redis

r = redis.Redis(unix_socket_path='/home/USERNAME/path/redis.sock', decode_responses=True)
r.set('test:hello', 'ok', ex=30)
print(r.get('test:hello'))

If ok is printed, the connection works. Write test keys with a clear prefix and a short expiry so real data isn’t touched.

Common Redis connection errors

  • Class “Redis” not found: the redis extension isn’t on for that site’s PHP version; go back to step 1.
  • Connection refused or No such file or directory: the socket path is wrong or Redis isn’t active for the account.
  • Permission denied: the application runs as another user, or the socket path belongs to another account.

FAQ

Does Redis keep data forever?

Not for cache; cached data is removed when it expires or when memory fills up. Always keep your main data in MySQL.

Does Redis make a site faster?

When the database is the bottleneck, yes: repeated queries are answered from RAM. If the slowness comes from heavy images or uncached pages, fix those first.

Redis on HiData hosting

On HiData’s professional cPanel hosting, the redis extension is available in Select PHP Version; get your account’s Redis socket path from our 24/7 support with a ticket and turn on the WordPress cache. Running a busy site? Our high-traffic hosting with LSCache is built for exactly that!

Share Post

hidata.org/en/blog/how-to-connect-redis-database-in-cpanel/
In Social Media

Add Comment

Related Articles