#!/bin/bash
INSTALL_DIR=/apps/redis
CONF_FILE=/apps/redis/etc/redis.conf
before_install (){
yum makecache && yum install -y gcc jemalloc > /dev/null
cd /usr/local/src && wget https://download.redis.io/releases/redis-5.0.9.tar.gz
tar xzvf redis-5.0.9.tar.gz > /dev/null
}
redis_install (){
cd redis-5.0.9 && make PREFIX=${INSTALL_DIR} install
}
after_install (){
useradd -r -s /sbin/nologin redis
mkdir -p /apps/redis/{etc,log,run,data} && chown redis.redis -R /apps/redis/
cp /usr/local/src/redis-5.0.9/redis.conf /apps/redis/etc/
sed -ri.bak '/^daemonize/s/yes/no/' $CONF_FILE
sed -ri '/^bind/s/127.0.0.1/0.0.0.0/' $CONF_FILE
sed -ri '/^protected-mode/s/yes/no/' $CONF_FILE
sed -ri '/^pidfile/s/\/var/\/apps\/redis/' $CONF_FILE
sed -ri '/^logfile/s/\"\"/\"\/apps\/redis\/log\/redis.log\"/' $CONF_FILE
sed -ri '/^dir/s/\.\//\/apps\/redis\/data/' $CONF_FILE
cat > /lib/systemd/system/redis.service << EOF
[Unit]
Description=Redis persistent key-value database
After=network.target
[Service]
ExecStart=/apps/redis/bin/redis-server /apps/redis/etc/redis.conf --supervised systemd
#ExecStop=/usr/libexec/redis-shutdown
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
cat >> /etc/sysctl.conf << EOF
vm.overcommit_memory = 1
net.core.somaxconn = 1024
EOF
sysctl -p
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
systemctl start redis && echo "redis 启动成功"
}
main (){
before_install
redis_install
after_install
}
main