继Nginx和MySQL的部署脚本之后,相信只要你跟着写了,那么里面的很多关键精髓你已经知晓,今天就来练习一个写Redis部署的脚本吧。
同样地,先给出部署步骤:
1)下载
cd /usr/local/src sudo wget -O redis-7.0.4.tar.gz https://codeload.github.com/redis/redis/tar.gz/refs/tags/7.0.4
2)安装
#解压 sudo tar zxvf redis-7.0.4.tar.gz cd redis-7.0.4/ ## 编译,安装并指定路径 sudo make sudo make PREFIX=/usr/local/redis install ## 创建conf和log目录 sudo mkdir /usr/local/redis/{conf,log} ## 创建数据目录 sudo mkdir -p /data/redis ## 创建redis服务的用户,并更改目录属主 sudo useradd -s /sbin/nologin redis sudo chown redis /data/redis /usr/local/redis/log
3)修改配置文件
sudo cp redis.conf /usr/local/redis/conf/ sudo vi /usr/local/redis/conf/redis.conf #修改如下 daemonize no 改为 daemonize yes logfile "" 改为 logfile "/usr/local/redis/log/redis.log" dir ./ 改为 dir /data/redis pidfile /var/run/redis_6379.pid 改为 pidfile /usr/local/redis/log/redis_6379.pid 在# requirepass foobared 下面增加一行 requirepass aminglinux.Com
4)定义systemd服务管理脚本
sudo vi /lib/systemd/system/redis.service ##内容如下 [Unit] Description=redis After=network.target [Service] User=redis Type=forking TimeoutSec=0 PIDFile=/usr/local/redis/log/redis_6379.pid ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
5)启动redis服务
## 更改内核参数 sudo vi /etc/sysctl.conf #加入两行 net.core.somaxconn = 2048 vm.overcommit_memory = 1 ## 使内核参数生效 sudo sysctl -p ## 加载redis服务 sudo systemctl daemon-reload ## 开启开机自启 sudo systemctl enable redis ## 启动服务 sudo systemctl start redis
把部署步骤写成shell脚本:
#!/bin/bash ck_ok() { if [ $? -ne 0 ] then echo "$1 error." exit 1 fi } download_redis() { cd /usr/local/src if [ -f redis-7.0.4.tar.gz ] then echo "当前目录已存在redis-7.0.4.tar.gz" echo "检测MD5" file_md5=`md5sum redis-7.0.4.tar.gz | awk '{print $1}'` if [ ${file_md5} == '3a2ce76ef8f5ca3cc6463c487f2d532c' ] then return 0 else echo "file redis-7.0.4.tar.gz md5 check failed" /bin/mv redis-7.0.4.tar.gz redis-7.0.4.tar.gz.old fi fi sudo wget -O redis-7.0.4.tar.gz https://codeload.github.com/redis/redis/tar.gz/refs/tags/7.0.4 ck_ok "下载redis" } install_redis() { cd /usr/local/src sudo tar zxf redis-7.0.4.tar.gz ck_ok "解压redis源码包" cd redis-7.0.4/ sudo make && sudo make PREFIX=/usr/local/redis install ck_ok "编译和安装redis" sudo mkdir -p /usr/local/redis/{conf,log} sudo mkdir -p /data/redis if id redis &>/dev/null then echo "系统已经存在redis用户,跳过创建" else echo "创建redis用户" sudo useradd -s /sbin/nologin redis fi ck_ok "创建redis用户" sudo chown -R redis /data/redis /usr/local/redis/log } config_redis() { echo "配置redis.conf" sudo /bin/cp /usr/local/src/redis-7.0.4/redis.conf /usr/local/redis/conf/redis.conf sudo sed -i 's/daemonize no/daemonize yes/' /usr/local/redis/conf/redis.conf sudo sed -i 's@logfile ""@logfile "/usr/local/redis/log/redis.log"@' /usr/local/redis/conf/redis.conf sudo sed -i 's@dir ./@dir /data/redis@' /usr/local/redis/conf/redis.conf sudo sed -i 's@pidfile /var/run/redis_6379.pid@pidfile /usr/local/redis/log/redis_6379.pid@' /usr/local/redis/conf/redis.conf sudo sed -i '/# requirepass foobared/a requirepass aminglinux.Com' /usr/local/redis/conf/redis.conf echo "配置systemd service" cat >/lib/systemd/system/redis.service <
说明:几个关键要领
1)下载之前要先判断本地是否已经下载过该包,如果本地已经有包,还需要判断该包的md5是否符合预期;
2)解压后和make以及make install后都要做判断,这是关键操作,如果这个没做对,后面的步骤没必要执行;
3)创建用户之前先做判断,看看用户是否已经存在;
4)启动完服务还要检查服务到底有没有起来。
审核编辑:刘清
全部0条评论
快来发表一下你的评论吧 !