RPM, 定制redis包

rpm入门学习请前往: Linux, RPM初体验

1. 目标

定制自己的redis rpm包

  • 指定安装目录: /usr/local/redis-3.2.4
  • 配置redis密码: redis@123
  • 配置systemctl的service文件, 用于开机启动
  • 卸载删除目录

学习%post, %preun, %postun的用法

  • %post, 安装后执行
  • %preun, 卸载前执行
  • %postun, 卸载后执行

2. spec文件

说明:

  • redis-3.2.4.tar.gz安装包已编译, 解压即可使用
Name:           redis
Version:        3.2.4 
Release:        1%{?dist}
Summary:        redis server

License:        GPLv3
Source0:        redis-3.2.4.tar.gz 

BuildRequires:  bash

%global		app_bin_dir		/usr/local/%{name}-%{version}
%global		redis_password		redis@123
%global		debug_package 		%{nil}

%description
redis server

%prep
%setup -q -n %{name}-%{version}

%install
mkdir -p %{buildroot}/%{app_bin_dir}/
cp -r ./* %{buildroot}/%{app_bin_dir}/


%files
%defattr(-,root,root,500)
%{app_bin_dir}/*

%post
# 修改redis配置
sed -i -n -e '/^bind/!p' -e '$a bind 0.0.0.0' %{app_bin_dir}/redis.conf
sed -i -n -e "/^requirepass/!p" -e "\$a requirepass %{redis_password}" %{app_bin_dir}/redis.conf

# 生成redis.service文件
cat > /etc/systemd/system/redis.service <<EOF
[Unit]
Description=redis service
After=network.target

[Service]
ExecStart=%{app_bin_dir}/src/redis-server %{app_bin_dir}/redis.conf
ExecStop=/bin/kill -s TERM \$MAINPID

[Install]
WantedBy=multi-user.target
EOF
systemctl enable redis.service

# 启动
systemctl start redis

%preun
systemctl stop redis

%postun
rm -fr %{app_bin_dir}

3. 效果演示

安装后, redis直接启动

[root@localhost ~]# ps aux |grep redis
root      10294  0.0  0.0 112648   960 pts/0    R+   08:25   0:00 grep --color=auto redis

[root@localhost ~]# rpm -ivh ~/rpmbuild/RPMS/x86_64/redis-3.2.4-1.el7.centos.x86_64.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:redis-3.2.4-1.el7.centos         ################################# [100%]

[root@localhost ~]# ps aux |grep redis
root      10315  2.0  0.0 136920  7944 ?        Ssl  08:25   0:00 /usr/local/redis-3.2.4/src/redis-server 0.0.0.0:6379
root      10319  0.0  0.0 112648   956 pts/0    S+   08:25   0:00 grep --color=auto redis

访问redis

[root@localhost ~]# /usr/local/redis-3.2.4/src/redis-cli -a redis@123 keys '*'
(empty list or set)

卸载redis, 进程停止, 安装目录清除

[root@localhost ~]# ls /usr/local/redis-3.2.4
00-RELEASENOTES  cluster       COPYING  INSTALL   MANIFESTO  redis-3.2.2.gem  runtest          runtest-sentinel  src    utils
BUGS             CONTRIBUTING  deps     Makefile  README.md  redis.conf       runtest-cluster  sentinel.conf     tests

[root@localhost ~]# rpm -e redis

[root@localhost ~]# ps aux |grep redis
root      10332  0.0  0.0 112648   960 pts/0    R+   08:27   0:00 grep --color=auto redis

[root@localhost ~]# ls /usr/local/redis-3.2.4
ls: cannot access /usr/local/redis-3.2.4: No such file or directory
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值