1、编译安装软件,优点是可以定制化安装目录、按需开启功能等,缺点是需要查找并实验出适合的编译参数,诸如MySQL之类的软件编译耗时过长。  

2、yum安装软件,优点是全自动化安装,不需要为依赖问题发愁了,缺点是自主性太差,软件的功能、存放位置都已经固定好了,不易变更。  

3、编译源码,根据自己的需求做成定制RPM包–>搭建内网yum仓库–yum安装。结合前两者的优点,暂未发现什么缺点。可能的缺点就是RPM包的通用性差,只能适用于本公司的环境。

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

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

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

1.FPM项目地址:("https://github.com/jordansissel/fpm")

2.FPM是一个打包工具[ruby的一个模块]

FPM的安装

#centos7系统 
yum -y groupinstall "Development Tools"
yum -y install ruby ruby-devel rubygems gcc openssl-devel

#安装FPM,添加淘宝的Ruby仓库
gem sources --add http://ruby.taobao.org/
#移除原生的Ruby仓库
gem sources --remove http://rubygems.org/
#安装fpm
gem install arr-pm fpm

FPM常用参数

-f :强制覆盖[覆盖同名rpm包]
-n :指定的rpm包名
-p :指定的rpm包文件放置位置
-v :指定的rpm包版本
-d :指定依赖的软件 ( [-d 'name'] or [-d 'name > version'] 例子: -d 'libstdc++ >= 4.4.3')
-a :指定系统架构,如果是noarch则为'-a all' 或者 '-a native' [x86_64] 当软件不区分64位或32位的时候可以 noarch
-s :指定INPUT的数据类型 (["-s dir"] 省略数据类型)
-m :指定打包人员[Packager] ([ -m 'user'])
-C :指定打包的相对路径,类似于buildroot. 譬如-C /tmp/apr/ 而打包机器的数据包路径是/tmp/apr/{opt,usr,etc} 那安装这个rpm包后,在本地的数据就是/opt/,/usr/,/etc/
-t :指定需要制作成什么包,可选项有(deb,rpm,solaris,etc)
支持的源类型::
"dir" "rpm" "gem" "python" "empty" "tar" "deb" "cpan" "npm" "osxpkg" "pear" "pkgin" "virtualenv" "zip"
支持的目标类型:
"rpm" "deb" "solaris" "puppet" "dir" "osxpkg" "p5p" "puppet" "sh" "solaris" "tar" "zip"
--description :软件包描述
--conflicts :指定冲突软件
--url :指定站点[惯例都是添加软件的官网 例如: --url "http://www.cnblog.com/roach57" ]
--verbose :安装过程详细打印
--after-install :包安装之后执行的脚本 也可写作 --post-install FILE
--before-install :包安装之前执行的脚本
--after-remove :包卸载之后执行的脚本
--before-remove :包卸载之前执行的脚本
--after-upgrade :包更新之后执行的脚本[仅支持 deb 和 rpm 这两种包]
--before-upgrade :包更新之前执行的脚本
--iteration :发布序号[就是rpm包里面的release]
--epoch :纪元 [不知道干嘛用的]
--no-rpm-sign :不使用rpm签名 Signature
--license :证书许可 [可选项有 'BSD(开源软件)' 'GPLv2(自由软件)' 'MIT' 'Public Domain(公共域)' 'Distributable(贡献)' 'commercial(商业)' 'Share(共享)等',一般的开发都写'BSD''GPL']
--vendor :供应商名称 [ --vendor 'roach57@163.com']
--no-depends :代表没有任何依赖包,和-d是对立的,不能共用
--config-files :指定配置文件,可以指定目录[递归]
--directories :指定包目录
--category :软件所属的类别[这是个什么软件]下面有个对应的表格:


[参考这个文件 /usr/share/doc/rpm-x.x.x/GROUPS ]
Amusements/Games [娱乐/游戏]
Amusements/Graphics [娱乐/图形]
Applications/Archiving [应用/文档]
Applications/Communications [应用/通讯]
Applications/Databases [应用/数据库]
Applications/Editors [应用/编辑器]
Applications/Emulators [应用/仿真器]
Applications/Engineering [应用/工程]
Applications/File [应用/文件]
Applications/Internet [应用/因特网]
Applications/Multimedia [应用/多媒体]
Applications/Productivity [应用/产品]
Applications/Publishing [应用/印刷]
Applications/System [应用/系统]
Applications/Text [应用/文本]
Development/Debuggers [开发/调试器]
Development/Languages [开发/语言]
Development/Libraries [开发/函数库]
Development/System [开发/系统]
Development/Tools [开发/工具]
Documentation [文档]
System Environment/Base [系统环境/基础]
System Environment/Daemons [系统环境/守护]
System Environment/Kernel [系统环境/内核]
System Environment/Libraries [系统环境/函数库]
System Environment/Shells [系统环境/接口]
User Interface/Desktops [用户界面/桌面]
User Interface/X [用户界面/X窗口]
User Interface/X Hardware Support [用户界面/X硬件支持]

RPM包的组成格式

roach-1.0.1-57.el6.x86_64.rpm
| | | | |
软件名称| | | |
版本号 | |   |
发布号 | |
硬件平台 |
扩展名

例子备注:
roach :软件名称
1.0.1 :软件版本号
57.el6 :发布号主要是对软件存在的bug或漏洞进行修补,在软件功能上并没有变化,el6指的是rhel6系统中发布
x86_64 :指64位的PC架构,另外还有'i386' 'i686' 等32位的PC架构,noarch是指不区分硬件架构
rpm :扩展名

RPM包的制作实例

源码包自定义RPM包

 以Nginx为例:
* 解压nginx的tar包:
[root@localhost nginx]# tar -zxf nginx-1.6.2.tar.gz
* 进入到解压目录中进行预编译:
[root@localhost nginx]# cd nginx-1.6.2
[root@localhost nginx-1.6.2]# ./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre \
--with-debug \
* 编译
[root@localhost nginx-1.6.2]# make -j24 //使用多核编译
[root@localhost nginx-1.6.2]# mkdir -p /temp/fpm_install/
* 安装到指定的目录中:
[root@localhost nginx-1.6.2]# make -j24 install DESTDIR=/temp/fpm_install/
[root@localhost nginx-1.6.2]# ll /temp/fpm_install/
total 16
drwxr-xr-x. 4 root root 4096 Jan 11 08:22 etc
drwxr-xr-x. 2 root root 4096 Jan 11 08:37 tmp
drwxr-xr-x. 4 root root 4096 Jan 11 08:20 usr
drwxr-xr-x. 4 root root 4096 Jan 11 08:20 var
[root@localhost nginx-1.6.2]# cd /temp/fpm_install/tmp
[root@localhost nginx-1.6.2]# cat install_after.sh
#!/bin/bash

source /etc/rc.d/init.d/functions
groupadd -r nginx
useradd -r -g nginx nginx

exit $?
[root@localhost nginx-1.6.2]# cat remove_after.sh
#!/bin/bash

source /etc/rc.d/init.d/functions
rm -rf /usr/local/nginx
rm -rf /etc/nginx
userdel nginx
exit $?
* 制作RPM包:
[root@localhost nginx-1.6.2]# fpm -f -s dir \
-t rpm \
-n nginx \
-v 1.6.2 \
--iteration 1.el6 \
-C /temp/fpm_install/ \
-p /temp/nginx_rpm/ \
--description 'nginx1.6.2_rpm' \
--url 'http://nginx.org/en/' \
--after-install /temp/fpm_install/tmp/install_after.sh \
--after-remove /temp/fpm_install/tmp/remove_after.sh

[root@localhost nginx-1.6.2]# cd /temp/nginx_rpm/;ls
nginx-1.6.2-1.el6.x86_64.rpm

简单软件包的制作

    * 创建测试目录:
[root@localhost install_rpm]# mkdir /temp/install_rpm
[root@localhost install_rpm]# cd /temp/install_rpm
* 创建测试用的文件和脚本:
[root@localhost install_rpm]# tree roach
roach
├── roach_test
└── tmp
├── mk_dir.sh
└── rm_dir.sh
[root@localhost install_rpm]# cat roach/roach_test
this is the test file!
[root@localhost tmp]# cat mk_dir.sh
#!/bin/bash

source /etc/rc.d/init.d/functions

mkdir -p /temp/roach
exit $?

[root@localhost install_rpm]# cat roach/tmp/mk_file.sh //安装rpm后执行的脚本
#!/bin/bash

source /etc/rc.d/init.d/functions

cd /temp/roach/
touch {1..10}
exit $?

[root@localhost install_rpm]# cat roach/tmp/rm_dir.sh //卸载完后执行的脚本
#!/bin/bash

source /etc/rc.d/init.d/functions
rm -rf /temp/roach
rm -rf /roach
exit $?

* 制作一个属于自己的RPM包:
[root@localhost install_rpm]# mkdir -p /temp/roach_rpm
[root@localhost install_rpm]# fpm -f -s dir \
-t rpm \
-n roach \
-v 1.0.1 \
--iteration 1.el6 \
-C /temp/install_rpm/ \
-p /temp/roach_rpm/ \
--description 'roach_test_1.0.1.rpm' \
--url 'http://www.cnblogs.com/roach57' \
--before-install /temp/install_rpm/roach/tmp/mk_dir.sh \
--after-install /temp/install_rpm/roach/tmp/mk_file.sh \
--after-remove /temp/install_rpm/roach/tmp/rm_dir.sh \
--license 'BSD' \
--verbose \
--category 'Application/File' \
-m 'roach' \
--vendor 'roach57@163.com'

* 查看生成的RPM包的信息:
[root@localhost install_rpm]# cd /temp/roach_rpm;ls
roach-1.0.1-1.el6.x86_64.rpm
[root@localhost roach_rpm]# rpm -qp --scripts roach-1.0.1-1.el6.x86_64.rpm
preinstall scriptlet (using /bin/sh):
#!/bin/bash

source /etc/rc.d/init.d/functions

mkdir -p /temp/roach
exit $?
postinstall scriptlet (using /bin/sh):
#!/bin/bash

source /etc/rc.d/init.d/functions

cd /temp/roach/
touch {1..10}
exit $?
postuninstall scriptlet (using /bin/sh):
#!/bin/bash

source /etc/rc.d/init.d/functions
rm -rf /temp/roach
rm -rf /roach
exit $?

[root@localhost roach_rpm]# rpm -qpi roach-1.0.1-1.el6.x86_64.rpm
Name : roach Relocations: /
Version : 1.0.1 Vendor: roach57@163.com
Release : 1.el6 Build Date: Thu 14 Jan 2016 05:44:17 AM EST
Install Date: (not installed) Build Host: localhost
Group : Application/File Source RPM: roach-1.0.1-1.el6.src.rpm
Size : 259 License: BSD
Signature : (none)
Packager : roach
URL : http://www.cnblogs.com/roach57
Summary : roach_test_1.0.1.rpm
Description :
roach_test_1.0.1.rpm
* 安装rpm包:
[root@localhost roach_rpm]# rpm -ivh roach-1.0.1-1.el6.x86_64.rpm
Preparing... ########################################### [100%]
1:roach ########################################### [100%]

[root@localhost roach_rpm]# tree /roach; tree /temp/roach
/roach
├── roach_test
└── tmp
├── mk_dir.sh
├── mk_file.sh
└── rm_dir.sh

1 directory, 4 files
/temp/roach
├── 1
├── 10
├── 2
├── 3
├── 4
├── 5
├── 6
├── 7
├── 8
└── 9

0 directories, 10 files
* 指定目录安装RPM:
[root@localhost roach_rpm]# rpm -ivh roach-1.0.1-1.el6.x86_64.rpm --prefix=/temp
Preparing... ########################################### [100%]
1:roach ########################################### [100%]

[root@localhost tmp]# tree /temp/roach
/temp/roach
├── 1
├── 10
├── 2
├── 3
├── 4
├── 5
├── 6
├── 7
├── 8
├── 9
├── roach_test
└── tmp
├── mk_dir.sh
├── mk_file.sh
└── rm_dir.sh

1 directory, 14 files
* 卸载rpm包:
[root@localhost roach_rpm]# rpm -e roach-1.0.1-1.el6.x86_64

RPM包信息

查看RPM包中的脚本信息

  [root@localhost ~]# rpm -qp --scripts roach-1.0.1-57.el6.x86_64.rpm
postinstall scriptlet (using /bin/sh):
#!/bin/bash

source /etc/rc.d/init.d/functions

cd temp/roach/

for i in {1..10};do
touch $i
done
exit 0
postuninstall scriptlet (using /bin/sh):
#!/bin/bash

source /etc/rc.d/init.d/functions
rm -rf temp/roach
exit 0

查看RPM包的目录结构

 [root@localhost ~]# rpm -qpl roach-1.0.1-57.el6.x86_64.rpm
/temp/roach/tmp/mk_dir.sh
/temp/roach/tmp/rm_dir.sh
/temp/roach/roach_test

RPM包验证

 [root@localhost ~]# rpm -K roach-1.0.1-57.el6.x86_64.rpm
roach-1.0.1-57.el6.x86_64.rpm: sha1 md5 OK

查看RPM包的属性

  [root@localhost ~]# rpm -qpi roach-1.0.1-57.el6.x86_64.rpm
Name : roach Relocations: /
Version : 1.0.1 Vendor: roach57@163.com
Release : 57.el6 Build Date: Thu 14 Jan 2016 01:16:33 AM EST
Install Date: (not installed) Build Host: localhost
Group : Application/File Source RPM: roach-1.0.1-57.el6.src.rpm
Size : 205 License: BSD
Signature : (none)
Packager : roach
URL : http://www.cnblogs.com/roach57
Summary : roach5.7_rpm
Description :
roach5.7_rpm