How to Integrate PHP Redis On Your Cloudways Platform

Redis is open-source. It is usually a data structure server since keys can contain strings, hashes, records, sets, and sorted sets. Before utilizing Redis with Laravel, we empower you to install and utilize the phpredis PHP expansion through PECL.

This PHP extension gives the client-server get to to the Redis server. It isn’t Redis itself. Redis is a server that offers a key/value store. After you introduce the Redis PHP expansion, it’ll permit PHP Code to communicate with a Redis server.

Redis, made by Salvatore Sanfilippo, is an open-source, in-memory data structure server with advanced key-value cache and store, frequently alluded to as a NoSQL database. It is also known as a data structure server since it can store strings, hashes, records, sets, sorted sets, and more.

Before using Redis in your PHP programs, you would like to be sure that you have Redis PHP driver and PHP set up on the machine. You’ll check the PHP instructional exercise for PHP establishment on your machine.

What is a Redis Session?

Sessions give a compelling way to store the information of individual clients against a unique session ID, as this ID is utilized to protect state data of the clients between distinctive page demands. PHP session handlers are especially utilized in user/webserver communicated websites, majorly ecommerce web apps and social organizing locales to approve the state of the clients logged-in and logged-out of the application. The default behavior of the session handler fulfills all your needs in most cases.

Prerequisites

Before we begin you would like to make sure what tools you need to have in order to integrate Redis on server.

  • PHP 8.x
  • MYSQL
  • Redis

Installation

Step 1: Choose a Server

Select your server and custom PHP Stack application. Select PHP server size as per your website traffic and location according to your preferred region.

GIF

Step 2: Launch PHP

After completing all these steps, you’ll explore the application tab and select the PHP stack application. Tap on it to discover advance subtle elements inside

GIF

Step 03: Turning On Redis On Cloudways Platform

By default, Redis is “disabled” on Cloudways, and you need to “enable” it to use it. For this, go to your server management tab and navigate to Settings and Packages and click on Packages to open it. Over there, you will see Redis is Uninstalled. Now click on the Install.

Wait for the server to load your new package.

Now go to the same tab, and you will see Redis is now installed.

Connect to Redis Server

<?php
//Connecting to Redis server on localhost
$redis = new Redis();
$redis->connect(‘127.0.0.1’, 6379);
echo “Connection to server sucessfully”;
//check whether server is running or not
echo “Server is running: “.$redis->ping();
?>

How to Install PHP Redis Extension

Step 1: Installing PHP Redis Client Library

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

Now, to configure Redis with PHP application, Move on to the “Server Management tab” to launch “SSH Terminal,” located in the “Master Credentials.” Here you can find your login credentials for the SSH terminal.

You have to open the SSH terminal to run the composer command. You can either use the Putty terminal or Cloudways SSH terminal as per the requirements.

Now I would access the public_html folder with the following command.


LS command is used to list files or directories in Linux

CD use for change directory


Now install Predis PHP in this folder by the following command.

  1. git clone git://github.com/nrk/predis.git/appstarter

Predis is successfully installed on your folder.

Step 2: Using Predis To Connect Custom PHP With Redis

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

vi index.php

You will see that your file has been viewed on your terminal. Now let’s start editing it.

Press the insert key on your keyboard. Move your cursor to:

Paste the following code in it by clicking the right button of your mouse and selecting paste from the browser.

<!–?php require “predis/autoload.php”;
Predis\Autoloader::register();
try { $redis = new Predis\Client();
$redis->set(“hello_world”, “Hi from PHP!”);
$value = $redis->get(“hello_world”);
var_dump($value);
}
catch (Exception $e) { echo “Couldn’t connect to Redis”; echo $e->getMessage();
}?–>

After pasting, your terminal should look like this:

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

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

Note: You can also edit the files by connecting to SFTP in Server Management tab under Master Credentials using FTP client. (That would be easier)

Step 3: Running It:

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

  1. redis-cli monitor

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

Now go to the SSH terminal tab, and you will find the following result on your redis-cli, which we have let’s run.

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

Conclusion

The Stripe PHP is an example of the effective use of PHP Redis. That does not influence your live information or is connected with managing an accounting system. The API key you utilize to verify the ask decides whether the ask is a live mode or test mode.