Table of Contents
Redis is an open-source in-memory data structure that is used as a cache, database server, and message broker. It is written in C programming language. There are various modules available for communication with the Redis server. Eg: PHP, Node.js, Ruby, Python, and more. In this tutorial, we will use the php module for the example.
In this tutorial, we will learn how to install Redis on ubuntu 19.04, 18.04 stable, and 16.04. We will also learn how to install modules for the different programming languages.
Step 1. Prerequisite
For this tutorial, I'll be using an AWS server for the Redis installation. Login to your server with the following command.
ssh -i id_rsa [email protected]
Once you logged in update your server apt package cache. Enter the following command to update the apt package cache
sudo apt-get update sudo apt-get upgrade
Step 2. Installing Redis
In Ubuntu by default, the Redis package is available. Once you update your apt package cache enter the following command to install the Redis server in your machine.
sudo apt-get install redis-server
The above command will download and install the stable version and dependencies of Redis. Once you are done with the installation of the Redis server Enter the following command to enable the Redis server to start on every server boot.
sudo systemctl enable redis-server.service
After enabling the Redis service enter the following command to test if the Redis server is running properly.
sudo systemctl status redis
Step 3. Configuring Redis
We can use the Redis server with it's By default setting. But according to your need, you can edit and configure the setting of the Redis server. You can find all the Redis configuration file at
/etc/redis location. Enter the following command to edit and configure the Redis settings
sudo vi /etc/redis/redis.conf
I will increase the memory size of the Redis server for this tutorial. By default, it is commented. You can update the settings according to your application needs.
maxmemory 256mb maxmemory-policy allkeys-lru
maxmemory-policy allkeys-lru will remove any key according to the LRU algorithm when the memory size reaches to 256mb. Now save the configuration and restart the Redis server with the following command.
sudo service redis-server restart
Step 4. Install PHP Redis Extension
In this section, we will install the PHP Redis extension. To install the extension enter the following command.
sudo apt-get install php-redis
Step 5. Test Redis server connection.
We will use
redis-cli to test the Redis server connection. Enter the following command to test
redis-cli 127.0.0.1:6379> ping PONG 127.0.0.1:6379>
Now we will set and get a key. To do so enter the following command to set the key.
127.0.0.1:6379> set test "This is redis test server" OK 127.0.0.1:6379> get test "This is redis test server" 127.0.0.1:6379>
To get the info on Redis server You can use below commands.
redis-cli info redis-cli info stats redis-cli info server