Are you searching for an option to speed up your website?
If so, the PHP Redis extension is one better option. Redis is an open-source, cache store mechanism that supports a wide range of data structures.
PHP Redis extension allows accessing the Redis server from PHP. We mainly use it as a cache for storing data effectively.
At Bobcares, we often receive requests to set up the PHP Redis extension as part of Server Management Services.
Today, let’s discuss this extension in detail and see how our Support Engineers set it up for our customers.
What is the PHP Redis extension?
As we all know, caching stores data in the cache aka temporary storage. Data stored in these caches are easily retrievable. Thus, caching essentially provides very fast data access.
Redis is one such cache that supports various data structures like strings, hashes, lists, etc.
It is developer-friendly and is supported widely by many programming languages. Redis application includes tech-giants like GitHub, Pinterest, Snapchat and so on.
Also, we can use Redis with PHP. For using Redis with PHP programs, it is necessary to have a Redis PHP driver and a PHP set up on the machine.
Install PHP Redis in Linux
Recently, one of our customers approached us to set up Redis in Linux and make it available for PHP scripts. Let’s see how our Support Engineers set it up for our customers.
Firstly, we need to install the dependencies of the package on the server. This requires the devel package of the PHP version on the server. On an Ubuntu server, we use
apt-get install php7.1-dev
Here, the server was having PHP 7.1. The proper package name depends on the server PHP version.
Next, we need to download the phpredis from the Github repository.
cd /tmp
wget https://github.com/phpredis/phpredis/archive/master.zip -O phpredis.zip
After downloading it, we extract the files to the phpredis directory and compile it. For that, we execute the commands:
cd phpredis && phpize && ./configure && make && sudo make install
Next, we copy and paste the contents of the modules folder to the PHP extension directory. Then, we add the following lines in the respective php.ini file. For instance, here we add the following entry in /etc/php/7.1/mods-available/redis.ini
extension = redis.so
Further, we restart PHP-FPM with
service php7.1-fpm restart
Finally, we restart Apache using:
service apache2 restart
Again, if the server has an Nginx web server, it may not even require a restart. The extension will be effective with the restart of PHP-FPM.
This completes the installation of Redis.
Redis in Windows
Similarly, in Windows, we initially install Predis, a Redis client for PHP.
To use Predis, we clone the repository into our working directory,
git clone git://github.com/nrk/predis.git
Connecting to the Redis server
After installation, we need to connect to the Redis server and check its availability.
<?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();
?>
When we execute this program, it will produce the output,
Connection to server sucessfully
Server is running: PONG
[Need more help to set up PHP Redis extension?- We’ll help you.]
Conclusion
In short, we use PHP Redis extension as a cache mechanism to store the data effectively for faster retrieval. Today’s write-up discussed how our Support Engineers set up this extension for our customers.
0 Comments