linux-tutorial/codes/linux/soft/redis-install.sh

107 lines
3.1 KiB
Bash
Raw Normal View History

2018-02-22 09:48:39 +08:00
#!/usr/bin/env bash
2019-08-19 19:19:57 +08:00
###################################################################################
# 控制台颜色
BLACK="\033[1;30m"
RED="\033[1;31m"
GREEN="\033[1;32m"
YELLOW="\033[1;33m"
BLUE="\033[1;34m"
PURPLE="\033[1;35m"
CYAN="\033[1;36m"
RESET="$(tput sgr0)"
2019-10-29 12:19:46 +08:00
##########################################################################cd#########
2019-08-19 19:19:57 +08:00
2019-10-29 12:19:46 +08:00
printf "${BLUE}\n"
2019-05-07 11:08:13 +08:00
cat << EOF
2018-02-24 14:16:40 +08:00
###################################################################################
2019-10-29 12:19:46 +08:00
# 采用编译方式安装 Redis
# @system: 适用于 CentOS7+
2019-05-07 14:29:16 +08:00
# @author: Zhang Peng
2018-02-24 14:16:40 +08:00
###################################################################################
2019-05-07 11:08:13 +08:00
EOF
2019-10-29 12:19:46 +08:00
printf "${RESET}\n"
2018-02-22 09:48:39 +08:00
2019-10-29 12:19:46 +08:00
command -v yum > /dev/null 2>&1 || {
printf "${RED}Require yum but it's not installed.${RESET}\n";
exit 1;
}
printf "\n${GREEN}>>>>>>>> install redis begin${RESET}\n"
2019-05-09 11:26:06 +08:00
2019-10-10 08:56:31 +08:00
if [[ $# -lt 1 ]] || [[ $# -lt 2 ]] || [[ $# -lt 3 ]] || [[ $# -lt 4 ]]; then
2019-10-29 12:19:46 +08:00
printf "${PURPLE}[Hint]\n"
2019-10-29 18:22:19 +08:00
printf "\t Usage: sh redis-install.sh [version] [port] [password] \n"
printf "\t Default: sh redis-install.sh 5.0.4 6379 <null> \n"
printf "\t Example: sh redis-install.sh 5.0.4 6379 123456 \n"
printf "${RESET}\n"
2019-05-09 11:26:06 +08:00
fi
2019-05-07 11:08:13 +08:00
version=5.0.4
if [[ -n $1 ]]; then
2019-10-29 18:22:19 +08:00
version=$1
2019-05-07 11:08:13 +08:00
fi
2018-02-22 09:48:39 +08:00
2019-05-08 17:13:04 +08:00
port=6379
2019-10-29 12:19:46 +08:00
if [[ -n $2 ]]; then
2019-10-29 18:22:19 +08:00
port=$2
2019-05-08 17:13:04 +08:00
fi
2019-07-11 16:52:03 +08:00
password=
2019-10-29 12:19:46 +08:00
if [[ -n $3 ]]; then
2019-10-29 18:22:19 +08:00
password=$3
2019-05-08 17:13:04 +08:00
fi
2019-10-29 12:19:46 +08:00
# install info
printf "${PURPLE}[Install Info]\n"
printf "\t version = ${version}\n"
printf "\t port = ${port}\n"
printf "\t password = ${password}\n"
printf "${RESET}\n"
2019-08-19 19:19:57 +08:00
2019-10-29 12:19:46 +08:00
printf "${CYAN}>>>> install required libs${RESET}\n"
2019-05-07 11:08:13 +08:00
yum install -y zlib zlib-devel gcc-c++ libtool openssl openssl-devel tcl
2019-10-29 12:19:46 +08:00
# download and decompression
printf "${CYAN}>>>> download redis${RESET}\n"
temp="/tmp/redis"
path="/usr/local/redis"
mkdir -p ${temp}
curl -o ${temp}/redis-${version}.tar.gz http://download.redis.io/releases/redis-${version}.tar.gz
tar zxf ${temp}/redis-${version}.tar.gz -C ${temp}
mv ${temp}/redis-${version} ${path}
# configure and makefile
printf "${CYAN}>>>> compile redis${RESET}\n"
2019-05-08 17:13:04 +08:00
cd ${path}
2019-05-07 15:39:11 +08:00
make && make install
2019-10-29 12:19:46 +08:00
rm -rf ${temp}
2019-05-07 11:08:13 +08:00
cd -
2019-05-08 17:13:04 +08:00
2019-10-29 12:19:46 +08:00
printf "${CYAN}>>>> modify redis config${RESET}\n"
2019-05-08 17:13:04 +08:00
cp ${path}/redis.conf ${path}/redis.conf.default
2019-10-29 12:19:46 +08:00
wget -N https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/soft/config/redis/redis.conf -O ${path}/redis.conf
sed -i "s/^port 6379/port ${port}/g" ${path}/redis.conf
2019-08-19 19:19:57 +08:00
if [[ -n ${password} ]]; then
2019-10-29 18:22:19 +08:00
sed -i "s/^protected-mode no/protected-mode yes/g" ${path}/redis.conf
sed -i "s/^# requirepass/requirepass ${password}/g" ${path}/redis.conf
2019-08-19 19:19:57 +08:00
fi
2019-05-08 17:13:04 +08:00
2019-10-29 12:19:46 +08:00
printf "\n${CYAN}>>>> open redis port in firewall${RESET}\n"
2019-05-08 17:13:04 +08:00
firewall-cmd --zone=public --add-port=${port}/tcp --permanent
firewall-cmd --reload
2019-10-29 12:19:46 +08:00
# setting systemd service
printf "${CYAN}>>>> set redis as a systemd service${RESET}\n"
wget -N https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/soft/config/redis/redis.service -O /usr/lib/systemd/system/redis.service
chmod +x /usr/lib/systemd/system/redis.service
# boot redis
printf "${CYAN}>>>> start redis${RESET}\n"
systemctl enable redis.service
systemctl start redis.service
2019-05-08 17:13:04 +08:00
2019-10-29 12:19:46 +08:00
printf "\n${GREEN}<<<<<<<< install redis end${RESET}\n"
printf "\n${PURPLE}redis service status: ${RESET}\n"
systemctl status redis