Install Redis with auto restart on CentOS 6, 7
Today we will share a guide to install Redis on CentOS Machine (incl. cPanel based server) . This guide is compatible with both CentOS 6 and 7 and provides solution of automatic start of redis service on server reboot/start.
We will install it for the user only not for root but we have covered for users who want it as root and want all users to use the Redis
We will Here we start:-
- Linux Machine with CentOS 6/7 with Root Permissions
- Internet Connection
Compile Redis from Source
- Install some required packages (needs root permissions)
sudo yum install wget make gcc tcl -y
- Download latest stable source for Redis from official release directory.
- Extract tar ball
tar -xvf redis-stable.tar.gz
- Run the following commands one by one to compile Redis from source
Please note that make commands below will take some time to complete, so keep patience
make hiredis lua jemalloc linenoise
- If make test commands show output as below then you have successfully compiled Redis.
- If you want to install for all users on your machine run the following command or else skip it (root permissions required)
sudo make install
Install init Script
We need init configuration so that redis-server should start itself on boot and no manual commands are needed by the user.
Note – We need root to create init Script
- Goto your redis-stable directory where you have compiled Redis from source using above steps
- Run the following command to get full path of your Redis dir, which I needed when creating init Script
As is above photo for me full path is /home/deepak/redis-stable
- Goto utils directory in redis-stable
- Run install_server.sh script as root
It will ask you some details while creating init script. To keep default just press ENTER Please select the redis port for this instance: 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default – /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default – /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default – /var/lib/redis/6379
Please select the redis executable path  /home/deepak/redis-stable/src/redis-serverIn my config, I have used everything default and have customized only the ‘redis executable path’, which means that we have to define the path where we have compiled the Redis ourselves.
So, now we will use the full path of redis-stable dir generated in Step #2 above and for me redis is compiled inside ‘src’ dir so for me redis executable path is /home/deepak/redis-stable/src/redis-server
Press enter after entering correct path and it will show you the selected configuration, just cross check and press ENTER to confirm.
Note- Users who have installed it as root don’t need to change the ‘redis executable path’
- If everything goes correct then install_server script will show success message on your terminal window.
- Now we have to start the service created by init script which needs root permissions
For CentOS 6 run
service redis_6379 start
For CentOS 7 run
systemctl start redis_6379
Note – If you have used custom port then your service name should be different.
Create alias for redis-cli
This is only needed if you have installed for specific user and not as root
- Edit .bashrc file for alias
- Paste the following lines at the end
Note- Please adjust if your redis-stable dir in not in root of your user.
- Apply .bashrc changessource ~/.bashrc
- Run redis-cli using following command and run proceeding commands to test redis.
> set welcome “Hello from BaseZap”
> get welcome
The output should be “Hello from BaseZap”
So, we have finished the Installation for Redis on CentOS Machine.