基于FPM制作RPM软件包!

工作中有如下情况需要将文件打包rpm:

  1. 避免重复工作,将源码程序打包为rpm
  2. 使用yum发布项目,项目打包为rpm
  3. 将自己写好的程序打包为rpm,提供给用户下载
  4. 其他

以前打包rpm是一个非常复杂的一件事情,自从有了fpm,打包rpm就和tar打包文件一样

一:搭建Epel和Base Yum 源

[root@localhost ~]# rz -E                                   //导入epel-release-latest-7.noarch.rpm包
z waiting to receive.**B0100000023be50                              
[root@localhost ~]# ls
anaconda-ks.cfg     nginx-1.16.0.tar.gz            模板     下载
date         original-ks.cfg       视频        音乐     nginx-1.15.9.tar.gz 
epel-release-latest-7.noarch.rpm    website-1.0.1-1.x86_64.rpm      图片    桌面
initial-setup-ks.cfg 公共 文档
[root@localhost ~]# rpm -ivh epel-release-latest-7.noarch.rpm                      //安装epel-release-latest-7.noarch.rpm
警告:epel-release-latest-7.noarch.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID 352c64e5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:epel-release-7-11 ################################# [100%]
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
CentOS-Base.repo    CentOS-fasttrack.repo    CentOS-Vault.repo
CentOS-CR.repo      CentOS-Media.repo        epel.repo
CentOS-Debuginfo.repo CentOS-Sources.repo epel-testing.repo
[root@localhost yum.repos.d]# yum clean all && yum makecache 

二:安装ruby环境和gem命令FPM

[root@localhost yum.repos.d]# yum -y install rubygems ruby-devel

[root@localhost yum.repos.d]# gem update --system                  //升级rubygems版本,此处会报错
Updating rubygems-update
Fetching: rubygems-update-3.0.6.gem (100%)Fetching: rubygems-update-3.0.6.gem
ERROR: Error installing rubygems-update:
rubygems-update requires Ruby version >= 2.3.0.                       //他报多少版本的错误就安装多少版本的
ERROR: While executing gem ... (NoMethodError)
undefined method `version' for nil:NilClass

[root@localhost ~]# gem install rubygems-update -v 2.3.0
Fetching: rubygems-update-2.3.0.gem (100%)
Successfully installed rubygems-update-2.3.0
Parsing documentation for rubygems-update-2.3.0
Installing ri documentation for rubygems-update-2.3.0
1 gem installed                                                                                  //安装成功

[root@localhost yum.repos.d]# gem update --system                      

...

......

.........

RubyGems system software updated                                             //升级成功

[root@localhost ~]# gem sources -a http://mirrors.aliyun.com/rubygems/           //添加国内阿里云的源
http://mirrors.aliyun.com/rubygems/ added to sources   

[root@localhost ~]# gem sources -l                        //查看yum源
*** CURRENT SOURCES ***

https://rubygems.org/                                                 //国外原始源
http://mirrors.aliyun.com/rubygems/                           //新增的阿里源

[root@localhost ~]# gem sources --remove https://rubygems.org/                         //移除国外原始源
https://rubygems.org/ removed from sources
[root@localhost ~]# gem sources -l
*** CURRENT SOURCES ***

http://mirrors.aliyun.com/rubygems/

[root@localhost ~]# gem install fpm                //安装FPM工具

三:编译Nginx

[root@localhost ~]# rz -E               //导入nginx包

[root@localhost ~]# ls
anaconda-ks.cfg      nginx-1.15.9.tar.gz    nginx.sh    公共    视频    文档    音乐
initial-setup-ks.cfg    original-ks.cfg    模板     图片    下载     桌面

[root@localhost ~]# tar xf nginx-1.15.9.tar.gz -C /usr/src

[root@localhost ~]# cd /usr/src/nginx-1.15.9/
[root@localhost nginx-1.15.9]# yum -y install pcre-devel zlib-devel make rpm-build

[root@localhost nginx-1.15.9]#  ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install

四:打包Nginx生成RPM包

此处做shell脚本的主要原因是为了让用户下载完nginx包时系统自动完成nginx服务的启动从而提高用户体验

[root@localhost nginx-1.15.9]# cd
[root@localhost ~]# vim nginx.sh

#!/bin/bash

useradd -M -s /sbin/nologin nginx                                //创建用户 
ln -s /usr/local/nginx/sbin/nginx /sbin/                        //做软连接
echo "www.source.com" > /usr/local/nginx/html/index.html        //修改主页
nginx                                                           //启动nginx服务

[root@localhost ~]# fpm -s dir -t rpm -n nginx -v 1.15.9 -d 'pcre-devel,zlib-devel' -f --post-install /root/nginx.sh /usr/local/nginx
Created package {:path=>"nginx-1.15.9-1.x86_64.rpm"}

-s    表示对一个目录进行打包

-t      表示要打包成的类型

-n     表示软件包的名字

-v     表示要打包成的版本

-d     表示要指定的依赖包

-f      表示要指定的文件

 /usr/local/nginx     表示指定的位置

[root@localhost ~]# rm -rf /usr/local/nginx/
[root@localhost ~]# rm -rf /usr/src/nginx-1.15.9/
[root@localhost ~]# rm -rf /usr/src/nginx-1.16.0/
[root@localhost ~]# rm -rf /usr/local/sbin/nginx
[root@localhost ~]# killall -9 nginx
[root@localhost ~]# userdel -r nginx
userdel:未找到 nginx 的主目录“/home/nginx”
[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.15.9.tar.gz original-ks.cfg 视频 下载
initial-setup-ks.cfg nginx-1.16.0.tar.gz 公共 图片 音乐
nginx-1.15.9-1.x86_64.rpm nginx.sh 模板 文档 桌面
[root@localhost ~]# rpm -ivh nginx-1.15.9-1.x86_64.rpm
准备中... ################################# [100%]
正在升级/安装...
1:nginx-1.15.9-1 ################################# [100%]

[root@localhost ~]# 

原文地址:https://www.cnblogs.com/CMX_Shmily/p/11532741.html