转自:https://www.zyops.com/autodeploy-rpm/

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

  1. 编译安装软件,优点是可以定制化安装目录、按需开启功能等,缺点是需要查找并实验出适合的编译参数,诸如 MySQL 之类的软件编译耗时过长。
  2. yum 安装软件,优点是全自动化安装,不需要为依赖问题发愁了,缺点是自主性太差,软件的功能、存放位置都已经固定好了,不易变更。 ===> 如果你现在还为是使用编译安装软件还是使用 yum 安装软件发愁,那你就 out 了。
  3. 编译源码,根据自己的需求做成定制 RPM 包–> 搭建内网 yum 仓库–yum 安装。结合前两者的优点,暂未发现什么缺点。可能的缺点就是 RPM 包的通用性差,只能适用于本公司的环境。另外一般人不会定制 RPM 包。这是中大型互联网企业运维自动化的必要技能。

这里也不介绍 rpm 的概念,想了解的朋友可以查看 http://www.ibm.com/developerworks/cn/linux/l-rpm/

这里也不介绍 rpmbuild 这个打包工具了,想了解的朋友自行谷歌百度。但我不建议大家花太多的时间去学习这个命令,比较晦涩,而且我会在下面介绍更简单的命令。

FPM 的作者是 jordansissel FPM 的 github:https://github.com/jordansissel/fpm FPM 功能简单说就是将一种类型的包转换成另一种类型。

  1. 支持的源类型包

    1. dir 将目录打包成所需要的类型,可以用于源码编译安装的软件包
    2. rpm rpm进行转换
    3. gem rubygem包进行转换
    4. python python模块打包成相应的类型
  2. 支持的目标类型包

    rpm         转换为rpm包
    deb         转换为deb包
    solaris     转换为solaris包
    puppet      转换为puppet模块
    
  3. FPM 安装 ```shell

    安装ruby模块

    yum -y install ruby rubygems ruby-devel

    查看当前使用的rubygems仓库

    gem sources list

    添加淘宝的Rubygems仓库,外国的源慢,移除原生的Ruby仓库

    gem sources —add https://gems.ruby-china.com/ —remove https://rubygems.org/

    安装fpm,gem从rubygem仓库安装软件类似yum从yum仓库安装软件。首先安装低版本的json,高版本的json需要ruby2.0以上,然后安装低版本的fpm,够用。

    gem install fpm

上面的2步安装仅适合CentOS6系统,CentOS7系统一步搞定,即gem install fpm


4. FPM 参数

常用参数

```shell
-s          指定源类型
-t          指定目标类型,即想要制作为什么包
-n          指定包的名字
-v          指定包的版本号
-C          指定打包的相对路径
-d          指定依赖于哪些包
-f          第二次打包时目录下如果有同名安装包存在,则覆盖它
-p          输出的安装包的目录,不想放在当前目录下就需要指定
--post-install      软件包安装完成之后所要运行的脚本;同--after-install
--pre-install       软件包安装完成之前所要运行的脚本;同--before-install
--post-uninstall    软件包卸载完成之后所要运行的脚本;同--after-remove
--pre-uninstall     软件包卸载完成之前所要运行的脚本;同--before-remove
  1. 安装 nginx

    yum -y install pcre-devel openssl-devel
    useradd nginx -M -s /sbin/nologin
    cd /application
    wget https://nginx.org/download/nginx-1.18.0.tar.gz
    tar xf nginx-1.18.0.tar.gz
    cd nginx-1.18.0
    ./configure  --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
    make -j 4 && make install
    
  2. 编写脚本

    mkdir -p /server/scripts
    cd /server/scripts/
    vim nginx_rpm.sh                      # 这是安装完rpm包要执行的脚本
    #!/bin/bash
    useradd nginx -M -s /sbin/nologin
    
  3. 打包 ```shell fpm -s dir -t rpm -n nginx -v 1.18.0 -d ‘pcre-devel,openssl-devel’ —post-install /server/scripts/nginx_rpm.sh -f /usr/local/nginx/
    Created package {:path=>”nginx-1.18.0.x86_64.rpm”}

ll -h nginx-1.18.0-1.x86_64.rpm -rw-r—r— 1 root root 6.7M Nov 1 10:02 nginx-1.18.0-1.x86_64.rpm


4. 安装 rpm 包

安装 rpm 包的两种方法:

- rpm 命令安装

rpm -ivh nginx-1.18.0-1.x86_64.rpm error: Failed dependencies: pcre-devel is needed by nginx-1.18.0-1.x86_64.rpm openssl-devel is needed by nginx-1.18.0-1.x86_64.rpm 但会报如上依赖错误,需要先yum安装依赖才能安装rpm包。


- yum 命令安装 rpm 包

yum -y localinstall nginx-1.18.0-1.x86_64.rpm 这个命令会自动先安装rpm包的依赖,然后再安装rpm包。


- 搭建内网 yum 仓库  [YUM 仓库搭建](https://www.zyops.com/autodeploy-yum/)

1. 相对路径问题

---

相对路径

[root@oldboy nginx]# fpm -s dir -t rpm -n nginx -v 1.6.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.6.2-1.x86_64.rpm”} [root@oldboy nginx]# rpm -qpl nginx-1.6.2-1.x86_64.rpm
/client_body_temp /conf/extra/dynamic_pools /conf/extra/static_pools …………

绝对路径

[root@oldboy ~]# fpm -s dir -t rpm -n nginx -v 1.6.2 /application/nginx-1.6.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.6.2-1.x86_64.rpm”} [root@oldboy ~]# rpm -qpl nginx-1.6.2-1.x86_64.rpm /application/nginx-1.6.2/client_body_temp /application/nginx-1.6.2/conf/extra/dynamic_pools /application/nginx-1.6.2/conf/extra/static_pools /application/nginx-1.6.2/conf/fastcgi.conf /application/nginx-1.6.2/conf/fastcgi.conf.default ………… 使用rpm -qpl 命令可以查看rpm包的内容。 注:fpm类似tar打包一样,只是fpm打的包能够被yum命令识别而已。


2. 软链接问题

---

[root@oldboy ~]# fpm -s dir -t rpm -n nginx -v 1.6.2 /application/nginx no value for epoch is set, defaulting to nil {:level=>:warn} File already exists, refusing to continue: nginx-1.6.2-1.x86_64.rpm {:level=>:fatal}

报错是因为当前目录存在同名的rpm包,可以使用-f参数强制覆盖。

[root@oldboy ~]# fpm -s dir -t rpm -n nginx -v 1.6.2 -f /application/nginx no value for epoch is set, defaulting to nil {:level=>:warn} Force flag given. Overwriting package at nginx-1.6.2-1.x86_64.rpm {:level=>:warn} no value for epoch is set, defaulting to nil {:level=>:warn} Created package {:path=>”nginx-1.6.2-1.x86_64.rpm”} 打包看似成功,但查看包的内容,只是这一个软链接文件。 [root@oldboy ~]# rpm -qpl nginx-1.6.2-1.x86_64.rpm /application/nginx 原因:目录结尾的/问题,类似rm删除软链接目录


编译安装好 nginx,mysql,php,此处有个问题,就是 php 的大部分依赖环境是通过 yum 安装的,但有一个 libiconv-1.14.tar.gz 包需要编译安装, 安装时已经指定了安装目录,只需一同打包即可。

还有一个问题,就是 mysql 这个目录比较大,用 fpm 打包耗时长。平时我们有可能需要对 nginx 或 php 做优化,这样又得重新打包。因此我们可以将 mysql 分离出来,分别打包。只需在制作 nginx+php 的 rpm 包时添加 mysql 的依赖即可。

参考命令

[root@web2 ~]# fpm -s dir -t rpm -n web2 -v 1.1 \ —description ‘lnmp.cms,bbs.blog’ \ -d ‘libxslt-devel,nfs-utils,rpcbind,mysql,libmcrypt-devel,mhash,mhash-devel,mcrypt’ \ —post-install /server/scripts/lnmp-init.sh \ /application /usr/local/libiconv/ /app/logs/ /data0/ /server/ ```