一键编译安装 Redis 脚本

注:此脚本适用于centos安装,具体需要根据实际情况进行测试调整。

[root@centos7 ~]# cat install_redis_for_centos.sh
#!/bin/bash
#
#**************************************************
#Author:                Xan_Yum
#QQ:                    7993167
#Email:                 waluna@qq.com
#Version:               1.0
#Date:                  2021-06-21
#FileName:              install_redis_for_centos.sh
#Description:           Install redis
#URL:                   https://waluna.top
#Copyroght (C):         2021 ALL rights reserved
#**************************************************

echo -e "\e[1;31mPlease wait ...\e[0m"
. /etc/init.d/functions
VERSION=redis-4.0.14
PASSWORD=waluna
PORT=6379
DIR=/apps/redis

install() {

rpm -q gcc jemalloc-devel &> /dev/null || yum install gcc jemalloc-devel -y &> /dev/null || { action "install packages fail" false;exit; } 
wget http://download.redis.io/releases/${VERSION}.tar.gz &> /dev/null || { action "Redis source code download fail" false; exit; }

[ -d $DIR ] || mkdir /apps
tar xf ${VERSION}.tar.gz
cd $VERSION
make -j 4 PREFIX=${DIR} install &> /dev/null && action "Redis install complete" || { action "Rdeis install fail" false; exit; }

ln -s ${DIR}/bin/redis-* /usr/bin/
mkdir -p ${DIR}/{etc,log,data,run}
cp redis.conf ${DIR}/etc/redis_${PORT}.conf

sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e "/# requirepass/a requirepass $PASSWORD" -e "/^dir .*/c dir ${DIR}/data/" -e "/logfile .*/c logfile ${DIR}/log/redis_${PORT}.log" -e "/^pidfile .*/c pidfile ${DIR}/run/redis_${PORT}.pid" ${DIR}/etc/redis_${PORT}.conf

if id redis &> /dev/null;then
    action "redis user is exist" false
else
    useradd -r -s /sbin/nologin redis
    action "redis user is created"
fi

chown -R redis.redis ${DIR}

echo "net.core.somaxconn = 1024" >> /etc/sysctl.conf
echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf
sysctl -p &> /dev/null

echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
. /etc/rc.d/rc.local

cat > /lib/systemd/system/redis.service <<EOF
[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=${DIR}/bin/redis-server ${DIR}/etc/redis_${PORT}.conf --supervised systemd
ExecStop=/bin/kill -s QUIT \$MAINPID
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now redis.service &> /dev/null && action "Redis service is up, Redis infomation: " || { action "Redis Failed to start" false; exit; }

redis-cli -a $PASSWORD INFO Server 2> /dev/null
}

install