Centos 7 Install Redis cache

When your project is growing you will need a proper cache, now there are several different options available in the market including services like Cloudflare, Akamai, and Imperva But if your project isn't that big and you don't want to pay or you want the cache to be stored on your infrastructure, therefore we have several options, but today we will have a look at Redise free solution.

Redis is a useful open-source in-memory data structure storage. It can be used in many possible ways for example it can be used as a database, cache, and message broker and supports various data structures such as Strings, Hashes, Lists, Sets, etc. It provides high availability via Redis Sentinel including monitoring, notifications Automatic failover. It also can provide automatic partitioning across multiple Redis nodes if you have installed and configured them with Redis Cluster.

As all that is said we can proceed for installation.

By the default, many Linux System administrators, IS admins, DevOps by the default will be logged in as root into the server, but for a safety enter sudo su into your terminal if it doesn't ask for password then you are as a root user if it asks for a password then enter it and press enter. While you type it will not show you what you type.

By default, the Redis library is not included in basic Centos 7 Installation so we would need to start by adding the package.

sudo yum install epel-release yum-utils  ;
sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm sudo yum-config-manager --enable remi

When we have added Redis library with the above commands now we can Install Redis on to our Centos 7

sudo yum install redis nano

Once we have installed Redis now we can Enable it to start automatically on startup and start it for the first time.

sudo systemctl start redis
sudo systemctl enable redis

Now you can check if Redis is started by typing:

sudo systemctl status redis

You should see output like this:

redis.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis.service; enabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/redis.service.d
        └─limit.conf
Active: active (running) since Sat 2018-11-24 15:21:55 PST; 40s ago
Main PID: 2157 (redis-server)
CGroup: /system.slice/redis.service
        └─2157 /usr/bin/redis-server 127.0.0.1:6379

If you get the same output as above, then congratulations you have installed Redis, if not then you need to do cat /var/log/redis/redis.log but sometimes Redis fails if you have disabled IPV6

By default, Redis doesn't allow remote connection so we can change that and allow remote access.

Note: Perform the following steps only if you want to connect to your Redis server from remote hosts. If you are using a single server setup, where the application and Redis are running on the same machine then you should not enable remote access.

If you need it and would like to proceed, then let's do it

sudo nano /etc/redis.conf

Locate the line that begins with bind 127.0.0.1 and add your server private IP address after 127.0.0.1. In my example, I'm having 192.168.100.5 It should look like this:

# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1 192.168.100.5

Please be sure, that you have replaced mine 192.168.100.5 with your real external IP. If you want to know your external IP go to https://ip.me if you are using internal IPS on your server then you would need to locate your internal IP. If you are using Windows you can open CMD and type ipconfig -all if you are using macOS then in terminal type ipconfig getifaddr en0 if Linux, then Just type in terminal ip a 

After you have added your IP restart Redis to apply changes

sudo systemctl restart redis

So that is it your Redis instance is ready to cache your webserver and other caches thanks to that your website should start working much faster and you should feel the changes.

If you want this all to be done by one command Just copy and paste this in terminal hit enter and wait.

sudo yum -y install git ; git clone http://www.amberit.in/linux-scripts/centos-7-install-redis.git /srv/ ; chmod +x /srv/amberit-redis.sh ; /srv/amberit-redis.sh ; rm -rf /srv/amberit-redis.sh