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