定制rpm包概述
什么是定制rpm包
将原本,使用源码安装的服务,打包成rpm包
为什么要定制rpm包
使用源码安装,步骤繁琐复杂,浪费时间,把源码包打成rpm包安装时可以节省时间,提高工作效率,做好rpm包,可以将rpm包放入yum仓库中,方便安装如何定制rpm包
如何定制rpm包
- fpm
- rpmbuild
安装fpm
fpm是ruby语法写的一种,定制rpm包的工具,所以在安装fpm之前,需要先安装ruby环境。
# 1.下载阿里云的base源和epel源
[root@localhost <sub>]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost </sub>]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
# 2.安装ruby环境
[root@localhost <sub>]# yum install -y ruby rubygems ruby-devel
# 3.查看gem默认的源
[root@localhost </sub>]# gem sources list
[root@localhost <sub>]# gem sources
*<strong> CURRENT SOURCES </strong>*
https://rubygems.org/
gem作用:gem和yum差不多,是包管理工具,yum来管理.rpm的包,gem来管理.gem包
# 4.删除gem默认官方源
[root@localhost </sub>]# gem sources --remove https://rubygems.org/
[root@localhost <sub>]# gem sources
*<strong> CURRENT SOURCES </strong>*
# 5.添加阿里云的gem源
[root@localhost </sub>]# gem sources -a https://mirrors.aliyun.com/rubygems/
[root@localhost <sub>]# gem sources
*<strong> CURRENT SOURCES </strong>*
https://mirrors.aliyun.com/rubygems/
# 6.安装fpm
[root@localhost </sub>]# yum install -y lrzsz
[root@localhost <sub>]# gem install fpm -v 1.3.3 #贼慢
[root@localhost </sub>]# tar xf fpm-1.3.3.x86_64.tar.gz
[root@localhost <sub>]# gem install *.gem
[root@localhost </sub>]# echo $?

报错原因:ruby版本是2.0,安装fpm工具需求是ruby版本要2.4.0以上
[root@localhost ~]# rpm -qa|grep ruby
ruby-2.0.0.648-39.el7_9.x86_6
源码安装nginx
# 0.安装依赖
[root@localhost <sub>]# yum install -y gcc gcc-c++ glibc pcre-devel openssl-devel
# 1.下载nginx源码包
[root@localhost </sub>]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
# 2.解压
[root@localhost <sub>]tar xf nginx-1.20.2.tar.
# 3.生成
[root@localhost </sub>]# cd nginx-1.20.2
[root@localhost nginx-1.20.2]# ./configure --prefix=/opt/nginx-1.20.2
[root@localhost nginx-1.20.2]# ./configure --prefix=/opt/nginx-1.20.2 --with-http_ssl_module --with-http_stub_status_module
# 4.编译和安装
[root@localhost ~]# make && make install
[root@localhost nginx-1.20.2]# vim /opt/nginx-1.20.2/html/index.html
使用fpm定制rpm包
fpm
-s:指定将什么打成rpm包(dir将目录打成rpm包)
-t:打成什么包(rpm 打成rpm包)
-n:指定包名,-n如何指定,yum就如何装(ngx) yum install -y ngx
-v:指定版本号
-d:指定依赖包,在这指定后,yum安装的时候,会根据-d的内容自动安装对应的依赖
--post-install:安装rpm包之后,要执行的脚本
--pre-install:安装rpm包之前,要执行的脚本
--post-uninstall:卸载rpm包之后,要执行的脚本
--pre-uninstall:卸载rpm包之前,要执行的脚本
-f:指定要打包的路径
## 安装完nginx之后
1.做软链接
2.添加nginx的环境变量
## 先写脚本
[root@localhost <sub>]# vim post_install_nginx.sh
ln -s /opt/nginx-1.20.2 /opt/nginx
echo 'PATH="/opt/nginx/sbin:$PATH"' > /etc/profile.d/nginx1.sh
## fpm打包命令
fpm -s dir \
-t rpm \
-n nginx \
-v 1.20.2 \
-d 'openssl-devel,pcre-devel' \
--post-install /root/post_install_nginx.sh \
-f /opt/nginx-1.20.2
#fpm打包
[root@localhost </sub>]# fpm -s dir \
> -t rpm \
> -n nginx \
> -v 1.20.2 \
> -d 'openssl-devel,pcre-devel' \
> --post-install /root/post_install_nginx.sh \
> -f /opt/nginx-1.20.2
no value for epoch is set, defaulting to nil {:level=>:warn}
no value for epoch is set, defaulting to nil {:level=>:warn}
Created package {:path=>"nginx-1.20.2-1.x86_64.rpm"}
##安装
[root@localhost ~]# rpm -ivh nginx-1.20.2-1.x86_64.rpm
Preparing... ################################# [100%]
Updating / installing...
1:nginx-1.20.2-1 ################################# [100%]
## 使用其他机器测试
rpm -ivh nginx-1.20.2-1.x86_64.rpm
yum localinstall -y nginx-1.20.2-1.x86_64.rpm
## 需要提前,下载好nginx需要的所有依赖包
rpm -Uvh *rpm
Preparing... ################################# [100%]
Updating / installing...
1:pcre-devel-8.32-17.el7 ################################# [ 20%]
2:libselinux-devel-2.5-15.el7 ################################# [ 40%]
3:krb5-devel-1.15.1-51.el7_9 ################################# [ 60%]
4:openssl-devel-1:1.0.2k-25.el7_9 ################################# [ 80%]
5:nginx-1.20.2-1 ################################# [100%]

fpm工具底层,调用了rpmbuild这个工具,所以需要提前安装rpmbuild
解决方法:yum install -y rpm-build
# 1. 拷贝到另一台虚拟机上
[root@localhost <sub>]# scp nginx-1.20.2-1.x86_64.rpm root@10.0.0.101:/root
The authenticity of host '10.0.0.101 (10.0.0.101)' can't be established.
ECDSA key fingerprint is SHA256:3nEOJkYPzLAslbZnyhe7yvoZjp7gvg4ZpKShnx/DJko.
ECDSA key fingerprint is MD5:4b:d4:28:39:a9:6d:2e:c8:48:a2:9e:ec:52:37:5e:95.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.101' (ECDSA) to the list of known hosts.
root@10.0.0.101's password:
nginx-1.20.2-1.x86_64.rpm 100% 2736KB 89.7MB/s 00:00
# 2. 10.0.0.101 虚拟机安装rpm报错
[root@shiying </sub>]# rpm -ivh nginx-1.20.2-1.x86_64.rpm
error: Failed dependencies:
openssl-devel is needed by nginx-1.20.2-1.x86_64
pcre-devel is needed by nginx-1.20.2-1.x86_64
[root@shiying <sub>]# ll
total 2740
-rw-r--r--. 1 root root 2801982 Apr 28 18:43 nginx-1.20.2-1.x86_64.rpm
# yum 解决依赖安装
[root@shiying </sub>]# yum localinstall -y nginx-1.20.2-1.x86_64.rpm
[root@shiying <sub>]# ll /opt
total 0
lrwxrwxrwx. 1 root root 17 Apr 28 18:30 nginx -> /opt/nginx-1.20.2
drwxr-xr-x. 6 root root 74 Apr 28 18:44 nginx-1.20.2
[root@shiying </sub>]# ll /etc/profile.d/
-rw-r--r--. 1 root root 29 Apr 28 18:44 nginx1.sh
[root@shiying <sub>]# cat /etc/profile.d/nginx1.sh
PATH="/opt/nginx/sbin:$PATH"
[root@shiying </sub>]# source /etc/profile.d/nginx1.sh
[root@shiying <sub>]# ng
ngettext nginx
[root@shiying </sub>]# nginx
[root@shiying ~]# ps -ef |grep nginx
root 5541 1 0 18:46 ? 00:00:00 nginx: master process nginx
nobody 5542 5541 0 18:46 ? 00:00:00 nginx: worker process
root 5545 1749 0 18:47 pts/1 00:00:00 grep --color=auto nginx

作业:
1.制作一个nginx的yum仓库
2.nginx打好的rpm包和nginx所有的依赖包cd放入yum仓库
3.其他机器,不使用其他yum源情况下,只使用自定义nginx仓库安装nginx
新启虚拟机安装依赖包
[root@localhost <sub>]# yum install -y ruby rubygems ruby-devel
[root@localhost </sub>]# cp nginx-1.20.2-1.x86_64.rpm /data/yum_data/centos/7/os/x86_64/Packages
[root@localhost ~]# ll /data/yum_data/centos/7/os/x86_64/Packages/
total 2740
-rw-r--r--. 1 root root 2801982 Apr 28 21:26 nginx-1.20.2-1.x86_64.rpm

[root@localhost <sub>]# yum install -y gcc gcc-c++ glibc pcre-devel openssl-devel--downloadonly --downloaddir=/tmp/
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Package gcc-4.8.5-44.el7.x86_64 already installed and latest version
Package gcc-c++-4.8.5-44.el7.x86_64 already installed and latest version
Package glibc-2.17-325.el7_9.x86_64 already installed and latest version
Package pcre-devel-8.32-17.el7.x86_64 already installed and latest version
No package openssl-devel--downloadonly available.
Nothing to do
[root@localhost </sub>]# ll /tmp
total 8
drwx------. 3 root root 17 Apr 20 16:14 systemd-private-4dd8afe880b54aff95b6f9a1fd1419d6-systemd-hostnamed.service-BSY1dI
drwx------. 2 root root 6 Apr 20 16:28 vmware-root_548-2999460674
drwx------. 2 root root 6 Apr 20 16:16 vmware-root_555-4282367637
drwx------. 2 root root 6 Apr 20 16:12 vmware-root_557-4282236562
drwx------. 2 root root 6 Apr 20 16:14 vmware-root_559-4290559760
-rw-------. 1 root root 253 Apr 25 20:55 yum_save_tx.2022-04-25.20-55.3y78vk.yumtx
-rw-------. 1 root root 243 Apr 26 17:27 yum_save_tx.2022-04-26.17-27.BXAlUa.yumtx
[root@localhost ~]# cd /tmp
[root@localhost tmp]# cp ./* /data/yum_data/centos/7/os/x86_64/Packages/
[root@localhost tmp]# createrepo /data/yum_data/centos/7/os/x86_64/
Spawning worker 0 with 1 pkgs
Spawning worker 1 with 0 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete

[root@localhost <sub>]# ll /etc/yum.repos.d/
total 40
-rw-r--r--. 1 root root 1664 Oct 23 2020 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 Oct 23 2020 CentOS-CR.repo
-rw-r--r--. 1 root root 649 Oct 23 2020 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 314 Oct 23 2020 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 630 Oct 23 2020 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Oct 23 2020 CentOS-Sources.repo
-rw-r--r--. 1 root root 8515 Oct 23 2020 CentOS-Vault.repo
-rw-r--r--. 1 root root 616 Oct 23 2020 CentOS-x86_64-kernel.repo
[root@localhost </sub>]# gzip -r /etc/yum.repos.d/
[root@localhost <sub>]# ll /etc/yum.repos.d/
total 32
-rw-r--r--. 1 root root 549 Oct 23 2020 CentOS-Base.repo.gz
-rw-r--r--. 1 root root 735 Oct 23 2020 CentOS-CR.repo.gz
-rw-r--r--. 1 root root 426 Oct 23 2020 CentOS-Debuginfo.repo.gz
-rw-r--r--. 1 root root 232 Oct 23 2020 CentOS-fasttrack.repo.gz
-rw-r--r--. 1 root root 381 Oct 23 2020 CentOS-Media.repo.gz
-rw-r--r--. 1 root root 506 Oct 23 2020 CentOS-Sources.repo.gz
-rw-r--r--. 1 root root 813 Oct 23 2020 CentOS-Vault.repo.gz
-rw-r--r--. 1 root root 272 Oct 23 2020 CentOS-x86_64-kernel.repo.gz
[root@localhost </sub>]# vi /etc/yum.repos.d/nginx_yum.repo
[shy003]
name=nginx remotebase
baseurl=http://10.0.0.106/centos/$releasever/os/$basearch/
gpgcheck=0
enabled=1
[root@localhost ~]# yum repolist
Loaded plugins: fastestmirror
Determining fastest mirrors
shy003 | 2.9 kB 00:00:00
shy003/7/x86_64/primary_db | 1.7 kB 00:00:00
repo id repo name status
shy003/7/x86_64 nginx remotebase 1
repolist: 1