fpm定制化RPM包之nginx rpm包的制作

Posted reblue520

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了fpm定制化RPM包之nginx rpm包的制作相关的知识,希望对你有一定的参考价值。

fpm定制化RPM包之nginx rpm包的制作

1.安装ruby模块

# yum -y install ruby rubygems ruby-devel

 

2.添加阿里云的Rubygems仓库,国外资源会影响下载速度

gem sources -a http://mirrors.aliyun.com/rubygems/ 
http://mirrors.aliyun.com/rubygems/ added to sources

移除原生的ruby仓库

gem sources --remove http://rubygems.org/

3.安装fpm

centos6:
gem install json -v 1.8.3
gem install fpm -v 1.3.3

 

centos7直接使用如下命令:

[[email protected] ~]# gem install fpm

Fetching: cabin-0.9.0.gem (100%)
Successfully installed cabin-0.9.0
Fetching: backports-3.8.0.gem (100%)
Successfully installed backports-3.8.0
Fetching: arr-pm-0.0.10.gem (100%)
Successfully installed arr-pm-0.0.10
Fetching: clamp-1.0.1.gem (100%)
Successfully installed clamp-1.0.1
Fetching: ffi-1.9.18.gem (100%)
Building native extensions. This could take a while...
Successfully installed ffi-1.9.18
Fetching: childprocess-0.7.0.gem (100%)
Successfully installed childprocess-0.7.0
Fetching: archive-tar-minitar-0.5.2.gem (100%)
Successfully installed archive-tar-minitar-0.5.2
Fetching: io-like-0.3.0.gem (100%)
Successfully installed io-like-0.3.0
Fetching: ruby-xz-0.2.3.gem (100%)
Successfully installed ruby-xz-0.2.3
Fetching: stud-0.0.22.gem (100%)
Successfully installed stud-0.0.22
Fetching: mustache-0.99.8.gem (100%)
Successfully installed mustache-0.99.8
Fetching: insist-1.0.0.gem (100%)
Successfully installed insist-1.0.0
Fetching: dotenv-2.2.1.gem (100%)
Successfully installed dotenv-2.2.1
Fetching: pleaserun-0.0.29.gem (100%)
Successfully installed pleaserun-0.0.29
Fetching: fpm-1.8.1.gem (100%)
Successfully installed fpm-1.8.1
Parsing documentation for cabin-0.9.0
Installing ri documentation for cabin-0.9.0
Parsing documentation for backports-3.8.0
Installing ri documentation for backports-3.8.0
Parsing documentation for arr-pm-0.0.10
Installing ri documentation for arr-pm-0.0.10
Parsing documentation for clamp-1.0.1
Installing ri documentation for clamp-1.0.1
Parsing documentation for ffi-1.9.18
Installing ri documentation for ffi-1.9.18
Parsing documentation for childprocess-0.7.0
Installing ri documentation for childprocess-0.7.0
Parsing documentation for archive-tar-minitar-0.5.2
Installing ri documentation for archive-tar-minitar-0.5.2
Parsing documentation for io-like-0.3.0
Installing ri documentation for io-like-0.3.0
Parsing documentation for ruby-xz-0.2.3
Installing ri documentation for ruby-xz-0.2.3
Parsing documentation for stud-0.0.22
Installing ri documentation for stud-0.0.22
Parsing documentation for mustache-0.99.8
Installing ri documentation for mustache-0.99.8
Parsing documentation for insist-1.0.0
Installing ri documentation for insist-1.0.0
Parsing documentation for dotenv-2.2.1
Installing ri documentation for dotenv-2.2.1
Parsing documentation for pleaserun-0.0.29
Installing ri documentation for pleaserun-0.0.29
Parsing documentation for fpm-1.8.1
Installing ri documentation for fpm-1.8.1
15 gems installed

 

4.在打包机器上先安装一次nginx

yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel pcre pcre-devel glib glib-devel
useradd nginx -M -s /sbin/nologin
tar xf nginx-1.12.0.tar.gz

cd nginx-1.12.0

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-file-aio --with-http_dav_module --with-pcre

make && make install

 

5.编写脚本
软件包卸载前、卸载后的脚本,可以根据情况是否编写,不编写问题也不大。但是rpm安装后的脚本是必须的。

mkdir /data/scripts/ -p
cd /data/scripts/

 

# 编写一个rpm安装后需要执行的脚本
vim nginx_post_install.sh

#!/bin/bash

useradd nginx -M -s /sbin/nologin
chmod +x /etc/init.d/nginx
chkconfig --add nginx
echo PATH=/user/local/nginx/sbin:$PATH>> /etc/profile.d/nginx.sh

 

# 卸载nginx后需要执行的脚本

# cat after_remove.sh 
#!/bin/bash
rm -rf /usr/local/nginx
rm -f /etc/rc.d/init.d/nginx

 

准备个启动脚本,如下:

vim /etc/init.d/nginx

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15 
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse #               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /usr/local/nginx/logs/nginx.pid
 
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
 
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
 
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
 
lockfile=/var/lock/subsys/nginx
 
make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" | sed s/[^*]*--user=\([^ ]*\).*/\1/g -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   options=`$nginx -V 2>&1 | grep configure arguments:`
   for opt in $options; do
       if [ `echo $opt | grep .*-temp-path` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}
 
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
 
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
 
restart() {
    configtest || return $?
    stop
    sleep 1
    start
}
 
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    $nginx -s reload
    RETVAL=$?
    echo
}
 
force_reload() {
    restart
}
 
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
 
rh_status() {
    status $prog
}
 
rh_status_q() {
    rh_status >/dev/null 2>&1
}
 
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

 

6. 打包(打包的过程其实就是将我们编译安装好的文件、目录打包,/data/scripts/是本次的打包工作目录)
# 将nginx启动脚本和注意的配置及启动文件拷贝到打包目录

mkdir -p /data/scripts/etc/rc.d/init.d
cp /etc/init.d/nginx /data/scripts/etc/rc.d/init.d/

mkdir -p /data/scripts/usr/local/nginx/
cp -r /usr/local/nginx/ /data/scripts/usr/local/nginx/

 

# /data/scripts目录结构
[[email protected] scripts]# tree

.
├── after_remove.sh
├── etc
│   └── rc.d
│   └── init.d
│   └── nginx
├── nginx-1.12.0-1.el6.x86_64.rpm
├── nginx_post_install.sh
└── usr
└── local
└── nginx
├── client_body_temp
├── conf
│   ├── fastcgi.conf
│   ├── fastcgi.conf.default
│   ├── fastcgi_params
│   ├── fastcgi_params.default
│   ├── koi-utf
│   ├── koi-win
│   ├── mime.types
│   ├── mime.types.default
│   ├── nginx.conf
│   ├── nginx.conf.default
│   ├── scgi_params
│   ├── scgi_params.default
│   ├── uwsgi_params
│   ├── uwsgi_params.default
│   └── win-utf
├── etc
│   └── rc.d
│   └── init.d
│   └── nginx
├── fastcgi_temp
├── html
│   ├── 50x.html
│   └── index.html
├── logs
│   ├── access.log
│   ├── error.log
│   └── nginx.pid
├── proxy_temp
├── sbin
│   └── nginx
├── scgi_temp
└── uwsgi_temp

 


# 打包,即将准备好的文件打包成rpm

# fpm -f -s dir -t rpm -n nginx --epoch 0 -v 1.12.0 --iteration 1.el6 -C /data/scripts/ -d pcre-devel,openssl-devel,autoconf,glib-devel --post-uninstall /data/scripts/nginx_post_install.sh --post-uninstall /data/scripts/after_remove.sh --workdir /data/scripts/ etc usr

 


Created package {:path=>"nginx-1.12.0-1.el6.x86_64.rpm"}

报错:
Need executable ‘rpmbuild‘ to convert dir to rpm {:level=>:error}

解决:
yum install rpm-build -y

# 注意:我们可以操作前修改下主机名,这样打包出来的Build Host就会跟着改变。

7. 安装rpm包

yum命令安装rpm包

yum -y localinstall nginx-1.12.0-1.x86_64.rpm

这个命令会自动先安装rpm包的依赖,然后再安装rpm包。

 

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(共享)等,一般的开发都写BSDGPL]
--vendor             :供应商名称 [ --vendor [email protected]]
--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    :扩展名

 









以上是关于fpm定制化RPM包之nginx rpm包的制作的主要内容,如果未能解决你的问题,请参考以下文章

yum仓库+fpm定制rpm包

Centos定制化RPM包

通过定制nginx的rpm包学习如何制作rpm安装包

fpm定制nginx-rpm包

FPM定制RPM包

自动化部署必备技能—定制化RPM包