Using Redis Cache With custom PHP sites on Cloudways

by Ahmed Khan  February 23, 2016

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 with your custom PHP sites? Well, it is possible. And to do so, you need to follow the following steps.

Configuring-Redis-Cache-on-PHP-Site

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

image12

app

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.

 

reddischangename
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 Redis Client Library

Predis is a 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.
    launchssh
  2. Now we need to reach to public_html folder to install predis on it. Type the following command to reach there:
  3. Now install predis in this folder by the following command.

    install
    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.

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:

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.

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 our redis cache 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 your custom PHP code with redis cache successfully.

Redis cache is one of the best ways to make your site load faster. Through the above API, you connect redis 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.

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

About 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 ahmed.khan@cloudways.com

Stay Connected:

You Might Also Like...