What is Redis?
Redis is an open source key-value cache and storage system, also referred to as a data structure server for its advanced support for several data types, such as hashes, lists, sets, and bitmaps, amongst others. It is used by many sites to increase their site search time.
The session handler is responsible for storing and retrieving data saved into sessions – by default, PHP uses files for that. In this tutorial, I will show you how to install Redis on a server, and then use Redis as a session handler for a PHP application on Cloudways. The process will include the following steps:
- Run PHP Stack App
- Running Redis Server
- Setting Redis As Session Handler
- Verifying Redis Session Handling
Step 1: Run PHP Stack App
If you are already a member, log in to Cloudways console. If you are not a member yet, Sign Up now for free on Cloudways and enjoy 3 days of free trial on DigitalOcean server to test your application with the optimized Cloudways PHP stack.
Once your server is launched, go to the server page and click on Add Application and from there, select PHP stack application and click on Add New Application.
Now go to application management tab and select your newly added application and change the folder name to RedisCache in order to easily remember it.
Now your application is ready for for Redis server to run on it.
You might also like: Using Redis Cache With Custom PHP Sites On Cloudways
Step 2: Running Redis Server
By default, redis server doesn’t come preinstalled on Cloudways but you can install it in just a single click. To begin installing, sign in to Cloudways Console and select Server Management Tab and under it find Server Setting and Packages. Under Server Setting and Packages select Packages Tab and then from the drop down select Yes and save it.
Wait till Redis is installed on your server.
Now Redis is installed on your server.
Let us now make it our session handler.
Step 3: Setting Redis As Session Handler
In order to change session handler in PHP, we need to change to values of two variables “session.save_handler” whose default value is “file”, we will change it to “redis”; and “session.save_path”, whose default value is “/var/lib/php5/sessions”, we will change it to the Redis server path which is “tcp://localhost:6379” in PHP.ini file. Since we can’t edit PHP.ini file directly in Cloudways, we will be using ini_set() function to change the values for session handler locally. Our DevOps team will be working on providing you Redis as a session handler in one click, in the meantime you can use this method.
Now go to the Server Management Tab and Launch SSH Terminal and sign in with the credentials. Now on your terminal, type the following command to reach to your new folder:
Now type the following command to edit your index.php file.
Now press insert key on your keyboard to start writing on this file. Now remove all the code from the file and paste the following code in it.
$count = isset($_SESSION['count']) ? $_SESSION['count'] : 1;
$_SESSION['count'] = ++$count;
Now press Esc, press “:”and type “wq” and hit enter to save and exit the file. Now go to your Application Management Tab and click on URL to run your index.php script. When you run the script you will get 1 as output and when you will run again, you will get 2 as an output, this means your session is working.
Step 4: Verifying Redis Session Handling
Now we are sure that session is working, we will now verify that the session is being done by Redis or not. For this, go to your SSH Terminal and type the following commands:
If you get PHPREDIS_SESSION after typing the keys * command then this means that you have successfully configured Redis as your session handler.
Using Redis as your session handler is only the tip of the ice-berg when it comes to the possibilities that you can explore with Redis. You can use it with your search on your site. Save the keyword as a key in your Redis DB and the result as its value so that when anyone searches for that keyword again, the result will be fetched in less time than the previous one. That is the power of Redis, and combined with Cloudways Managed PHP Cloud Hosting, it gives you optimal results.
Create PHP websites without the worry of server management.
Deploy your PHP app on optimized PHP hosting servers.