#!/bin/sh
#***************************************************************#
# ScriptName: installredis.sh
# Author: xhy
# Create Date: 2021-05-14 17:12
# Modify Author: xhy
# Modify Date: 2021-05-14 17:12
# Version: v1
#***************************************************************#
read -p "Do you want enable remote access, Y or N ?" answer

# The kernel parameters
echo "vm.overcommit_memory=1" >> /etc/sysctl.d/redis-sysctl.conf
sysctl -p /etc/sysctl.d/redis-sysctl.conf
echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.d/rc.local

# Gcc & Gcc-c++
yum -y install gcc gcc-c++ 

# Install redis
VERSION=6.2.3
wget -O /root/redis-${VERSION}.tar.gz https://download.redis.io/releases/redis-${VERSION}.tar.gz
cd /root/
tar -zxvf redis-${VERSION}.tar.gz
cd redis-${VERSION}
time make -j4
make install PREFIX=/usr/local/redis

echo "PATH=/usr/local/redis/bin:\$PATH" >> /etc/profile.d/redis.sh

# Start redis
mkdir -p /etc/redis/
cp /root/redis-${VERSION}/redis.conf /etc/redis/redis_6379.conf
sed -i "s/^daemonize.*/daemonize yes/g" /etc/redis/redis_6379.conf

cat > /usr/lib/systemd/system/redis-server.service << EOF
[Unit]
Description=Redis Server Manager
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/local/redis/bin/redis-server /etc/redis/redis_6379.conf
ExecStop=/usr/local/redis/bin/redis-cli shutdown
Type=forking
PIDFile=/var/run/redis_6379.pid
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload

case ${answer} in
    y | Y | yes | Yes)
        sed -i "s/^bind.*/# bind 127.0.0.1 -::1/g" /etc/redis/redis_6379.conf
        sed -i "s/^protected-mode.*/protected-mode no/g" /etc/redis/redis_6379.conf
        echo "Install complate!"
    ;;
    *)
        echo "Install complate!"
esac
