Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

Using Redis Cache With custom PHP sites on Cloudways

February 23, 2016

4 Min Read
Reading Time: 4 minutes

We all know that Cloudways servers come pre-installed with Redis, irrespective of your application framework. You just need to enable it in one click on your server. If you are using any PHP framework or CMS, there are many plugins and extensions which help you to connect Redis with your framework, however, what if you want to use Redis cache in PHP site that is built without any framework or CMS? Well, it is possible. And to do so, you need to follow the steps mentioned below in this PHP Redis example.

php redis example

First, sign in to your Cloudways account. If you don’t have one, signup now for free.

Step 1: Choose a Server

Choose your desired cloud provider and select the PHP Stack Application



Now click on Launch Server and wait till your server is fully launched. Once your server is launched, go to the application management tab and click on rename and name your application folder to redis for now, to make it easy for you to access in terminal.

change application name

You might also like: How To Setup Redis Server As A Session Handler In PHP

Step 2: Turning On Redis On Cloudways Platform

By default, Redis is “disabled” on Cloudways and you need to “enable” it in order to use it. For this, go to your server management tab and scroll down to Server Settings and Packages and click on Plus sign to open it. Now click on Packages tab. Over there, you will see Redis is Disabled. Now click on the edit marker on Redis and from the drop down menu, select Yes and click on Save.


Wait for the server to load your new package.

upddate package

Now go to the same tab and you will see Redis is now turned on.

Step 3: Installing PHP Redis Client Library

Predis is a PHP Redis Client Library which helps you to connect to Redis API very easily. Through this you can perform all the redis commands. Now let us install this.

  1. Go to your server management tab and click on Launch SSH terminal and login with your details that can be found on that tab.
  2. Now we need to reach to public_html folder to install predis on it. Type the following command to reach there:
    cd applications/<application_name>/public_html
  3. Now install predis in this folder by the following command.
    git clone git://

    Predis is successfully installed on your folder.

Step 4: Using Predis To Connect Custom PHP With Redis

Now let us edit our index.php file on our SSH terminal by typing following command.

vi index.php

You will see that your file has been viewed on your terminal. Now let’s start editing it. Press insert key on your keyboard. Move your cursor to:

echo '<h1>PHP Stack - Test Page</h1>';

Remove this whole line by backspace. And paste the following code in it, by clicking right button of your mouse and selecting paste from browser.


require "predis/autoload.php";


try {

   $redis = new Predis\Client();

   $redis->set("hello_world", "Hi from php!");

$value = $redis->get("hello_world");



catch (Exception $e) {

   echo "Couldn't connect to Redis";

   echo $e->getMessage();


After pasting, your terminal should look like this:

editted index php

Now press ESC key and type :w this will save the index.php and now press ESC key and type :q this will exit the viewer.

In the code above we are creating a new variable “hello_world” for our redis cache and setting it value to “Hi from php”. After that, we are getting the value of our variable from redis cache in order to check redis is working or not.

Note: You can also edit the files by connecting to SFTP provided in Server Management tab under Master Credentials using FTP client.

Step 5: Running It:

Type the below command on SSH terminal and hit enter. This command will let us know whether the Redis cache in our PHP site is working or not.

redis-cli monitor

When you run the above command you will see a OK message.Leave the SSH terminal window open and go to the Application Management tab on your Cloudways panel and click on application URL. You will see the following result on the newly open tab:.

string ‘Hi from php!’ (length=12)

Now goto SSH terminal tab and you will find the following result on your redis-cli which we have let running.

redis cli

If you see the above window then this means you have configured PHP with redis cache successfully.

Redis cache is one of the best ways to make your site load faster. By following the PHP Redis example above, you connect Redis cache with PHP easily. If you need to know more about the Predis API, then you can visit this page. If you have any issues in the installation procedure or get stuck at any step, then feel free to ask me through the comments section below.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Launch PHP websites without the worry of Server Management.

Pre-Installed Optimized Stack with Git, Composer & SSH

Ahmed Khan

Ahmed was a PHP community expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is a software engineer with extensive knowledge in PHP and SEO. He loves watching Game of Thrones is his free time. Follow Ahmed on Twitter to stay updated with his works. You can email him at

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!