暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

干货:史上最全RPM打包教程之FPM

赛里运维 2019-04-22
2077

一、安装fpm工具

fpm工具其实是对rpmbuild进行封装,目的是使打包变成容易。由于此工具是ruby语言编写的,所以系统需要安装ruby,且ruby版本号大于1.8.5。


fpm开源项目:

https://github.com/jordansissel/fpm


1、安装ruby环境和gem命令(gem命令是从rubygem仓库安装软件,类似yum从yum仓库安装软件)

    [root@localhost ~]# yum -y install ruby rubygems ruby-devel gcc


    2、查看gem默认源

      [root@localhost ~]# gem source list
      *** CURRENT SOURCES ***
      https://rubygems.org/


      3、添加阿里云rubygems仓库

        [root@localhost ~]# gem sources -a http://mirrors.aliyun.com/rubygems/
        http://mirrors.aliyun.com/rubygems/ added to sources


        4、移除默认的国外源

          [root@localhost ~]# gem sources --remove https://rubygems.org/
          https://rubygems.org/ removed from sources


          5、查看gem源是否为阿里云

            [root@localhost ~]# gem source list
            *** CURRENT SOURCES ***
            http://mirrors.aliyun.com/rubygems/


            6、安装fpm工具

              [root@localhost ~]# gem install fpm
              Fetching: cabin-0.9.0.gem (100%)
              Successfully installed cabin-0.9.0
              Fetching: backports-3.13.0.gem (100%)
              Successfully installed backports-3.13.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.10.0.gem (100%)
              Building native extensions. This could take a while...
              Successfully installed ffi-1.10.0
              Fetching: childprocess-0.9.0.gem (100%)
              Successfully installed childprocess-0.9.0
              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.23.gem (100%)
              Successfully installed stud-0.0.23
              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.7.2.gem (100%)
              Successfully installed dotenv-2.7.2
              Fetching: pleaserun-0.0.30.gem (100%)
              Successfully installed pleaserun-0.0.30
              Fetching: fpm-1.11.0.gem (100%)
              Successfully installed fpm-1.11.0
              Parsing documentation for cabin-0.9.0
              Installing ri documentation for cabin-0.9.0
              Parsing documentation for backports-3.13.0
              Installing ri documentation for backports-3.13.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.10.0
              Installing ri documentation for ffi-1.10.0
              Parsing documentation for childprocess-0.9.0
              Installing ri documentation for childprocess-0.9.0
              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.23
              Installing ri documentation for stud-0.0.23
              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.7.2
              Installing ri documentation for dotenv-2.7.2
              Parsing documentation for pleaserun-0.0.30
              Installing ri documentation for pleaserun-0.0.30
              Parsing documentation for fpm-1.11.0
              Installing ri documentation for fpm-1.11.0
              14 gems installed

              题外话:如果操作系统为CentOS6,可能会提示ruby版本问题,这个时候我们指定安装老版本(因为升级到新版本很麻烦)

                [root@localhost ~]# gem install fpm
                Building native extensions. This could take a while...
                Building native extensions. This could take a while...
                ERROR: Error installing fpm:
                ruby-xz requires Ruby version >= 1.9.3.
                [root@localhost ~]# gem install fpm -v 1.4.0


                7、查看版本:

                  [root@localhost ~]# gem -v
                  2.0.14.1


                  二、编译安装nginx

                    [root@localhost ~]# groupadd nginx
                    [root@localhost ~]# useradd -s sbin/nologin -g nginx -M nginx
                    [root@localhost ~]# mkdir -p tmp/installdir
                    [root@localhost ~]# mkdir -p root/soft
                    [root@localhost ~]# cd root/soft/




                    [root@localhost soft]# yum install gcc gcc-c++ glibc automake pcre zlip zlib-devel openssl-devel pcre-devel
                    [root@localhost soft]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
                    [root@localhost soft]# tar -zxvf nginx-1.14.2.tar.gz
                    [root@localhost soft]# cd nginx-1.14.2
                    [root@localhost nginx-1.14.2]# ./configure \
                    --prefix=/usr/local/nginx \
                    --conf-path=/etc/nginx/nginx.conf \
                    --http-log-path=/var/log/nginx/access.log \
                    --error-log-path=/var/log/nginx/error.log \
                    --user=nginx \
                    --group=nginx \
                    --with-http_ssl_module \
                    --with-http_flv_module \
                    --with-http_stub_status_module \
                    --with-http_gzip_static_module \
                    --with-http_gunzip_module \
                    --with-http_random_index_module \
                    --with-http_secure_link_module \
                    --with-http_auth_request_module \
                    --with-http_mp4_module \
                    --with-http_sub_module \
                    --with-http_realip_module \
                    --with-http_dav_module \
                    --with-file-aio \
                    --with-pcre \
                    --with-stream




                    [root@localhost nginx-1.14.2]# make
                    [root@localhost nginx-1.14.2]# make install DESTDIR=/tmp/installdir


                    配置服务:

                      [root@localhost nginx-1.14.2]# mkdir -p tmp/installdir/usr/lib/systemd/system
                      [root@localhost nginx-1.14.2]# vi tmp/installdir/usr/lib/systemd/system/nginx.service
                      [Unit]
                      Description=The nginx HTTP and reverse proxy server
                      After=network.target remote-fs.target nss-lookup.target
                      [Service]
                      Type=forking
                      PIDFile=/usr/local/nginx/logs/nginx.pid
                      ExecStartPre=/usr/bin/rm -f run/nginx.pid
                      ExecStartPre=/usr/local/nginx/sbin/nginx -t
                      ExecStart=/usr/local/nginx/sbin/nginx
                      ExecReload=/bin/kill -s HUP $MAINPID
                      KillMode=process
                      KillSignal=SIGQUIT
                      TimeoutStopSec=5
                      PrivateTmp=true
                      [Install]
                      WantedBy=multi-user.target


                      三、nginx脚本

                      创建安装前的脚本:

                        [root@localhost ~]# vim root/soft/nginx-1.14.2/pre_install.sh
                        #!/bin/bash
                        groupadd nginx
                        useradd -s /sbin/nologin -g nginx -M nginx


                        创建安装后的脚本:

                          [root@localhost ~]# vim root/soft/nginx-1.14.2/post_install.sh
                          #!/bin/bash
                          chmod a+x usr/lib/systemd/system/nginx.service
                          systemctl start nginx
                          systemctl enable nginx


                          创建卸载前的脚本:

                            [root@localhost ~]# vim root/soft/nginx-1.14.2/pre_uninstall.sh
                            #!/bin/bash
                            systemctl stop nginx
                            pkill -9 nginx


                            创建卸载后的脚本:

                              [root@localhost ~]# vim root/soft/nginx-1.14.2/post_uninstall.sh
                              #!/bin/bash
                              userdel -r nginx
                              rm -rf /usr/lib/systemd/system/nginx.service
                              rm -rf /usr/local/nginx
                              rm -rf /etc/nginx
                              rm -rf /var/log/nginx


                              四、打RPM包

                              fpm帮助:

                                [root@localhost installdir]# fpm -help


                                安装rpm-build工具(fpm依赖rpm-build):

                                  [root@localhost ~]# yum -y install rpm-build


                                  fpm打包命令:

                                    #el6:
                                    [root@localhost ~]# fpm -f -s dir -t rpm -n nginx --epoch 0 -v 1.14.2 -C /tmp/installdir -p /tmp/ -d 'gcc' -d 'gcc-c++' -d 'glibc' -d 'automake' -d 'pcre' -d 'zlip' -d 'zlib-devel' -d 'openssl-devel' -d 'pcre-devel' --verbose --category 'Applications/Internet' --description 'nginx' --url 'http://nginx.org/' --license 'BSD' -m 'nginx' --iteration '1.el6' --pre-install /root/soft/nginx-1.14.2/pre_install.sh --post-install /root/soft/nginx-1.14.2/post_install.sh --pre-uninstall /root/soft/nginx-1.14.2/pre_uninstall.sh --post-uninstall /root/soft/nginx-1.14.2/post_uninstall.sh --no-rpm-sign
                                    #el7:
                                    [root@localhost ~]# fpm -f -s dir -t rpm -n nginx --epoch 0 -v 1.14.2 -C /tmp/installdir  -p /tmp/ -d 'gcc' -d 'gcc-c++' -d 'glibc' -d 'automake' -d 'pcre' -d 'zlip' -d 'zlib-devel' -d 'openssl-devel' -d 'pcre-devel' --verbose --category 'Applications/Internet' --description 'nginx' --url 'http://nginx.org/' --license 'BSD' -m 'nginx' --iteration '1.el7' --pre-install /root/soft/nginx-1.14.2/pre_install.sh --post-install /root/soft/nginx-1.14.2/post_install.sh --pre-uninstall /root/soft/nginx-1.14.2/pre_uninstall.sh --post-uninstall /root/soft/nginx-1.14.2/post_uninstall.sh --no-rpm-sign
                                    No args, but -s dir and -C are given, assuming '.' as input {:level=>:info}
                                    Setting workdir {:workdir=>"/tmp", :level=>:info}
                                    Setting from flags: category=Applications/Internet {:level=>:info}
                                    Setting from flags: description=nginx {:level=>:info}
                                    Setting from flags: epoch=0 {:level=>:info}
                                    Setting from flags: iteration=1.el7 {:level=>:info}
                                    Setting from flags: license=BSD {:level=>:info}
                                    Setting from flags: maintainer=nginx {:level=>:info}
                                    Setting from flags: name=nginx {:level=>:info}
                                    Setting from flags: url=http://nginx.org/ {:level=>:info}
                                    Setting from flags: version=1.14.2 {:level=>:info}
                                    Converting dir to rpm {:level=>:info}
                                    Reading template {:path=>"/usr/local/share/gems/gems/fpm-1.11.0/templates/rpm.erb", :level=>:info}
                                    Running rpmbuild {:args=>["rpmbuild", "-bb", "--define", "buildroot /tmp/package-rpm-build-5937791a53acf5aef0d8eecc9468d9a8f6d73f2224f1b0871d40a12b0b8d/BUILD", "--define", "_topdir /tmp/package-rpm-build-5937791a53acf5aef0d8eecc9468d9a8f6d73f2224f1b0871d40a12b0b8d", "--define", "_sourcedir /tmp/package-rpm-build-5937791a53acf5aef0d8eecc9468d9a8f6d73f2224f1b0871d40a12b0b8d", "--define", "_rpmdir /tmp/package-rpm-build-5937791a53acf5aef0d8eecc9468d9a8f6d73f2224f1b0871d40a12b0b8d/RPMS", "--define", "_tmppath /tmp", "/tmp/package-rpm-build-5937791a53acf5aef0d8eecc9468d9a8f6d73f2224f1b0871d40a12b0b8d/SPECS/nginx.spec"], :level=>:info}
                                    执行(%prep): /bin/sh -e /tmp/rpm-tmp.ksQwsK {:level=>:info}
                                    执行(%build): /bin/sh -e /tmp/rpm-tmp.IcrzeQ {:level=>:info}
                                    执行(%install): /bin/sh -e /tmp/rpm-tmp.yppD1V {:level=>:info}
                                    处理文件:nginx-1.14.2-1.el7.x86_64 {:level=>:info}
                                    Provides: nginx = 0:1.14.2-1.el7 nginx(x86-64) = 0:1.14.2-1.el7 {:level=>:info}
                                    Requires(interp): /bin/sh /bin/sh /bin/sh /bin/sh {:level=>:info}
                                    Requires(rpmlib): rpmlib(PayloadFilesHavePrefix) <= 4.0-1 rpmlib(CompressedFileNames) <= 3.0.4-1 {:level=>:info}
                                    Requires(pre): /bin/sh {:level=>:info}
                                    Requires(post): /bin/sh {:level=>:info}
                                    Requires(preun): /bin/sh {:level=>:info}
                                    Requires(postun): /bin/sh {:level=>:info}
                                    写道:/tmp/package-rpm-build-5937791a53acf5aef0d8eecc9468d9a8f6d73f2224f1b0871d40a12b0b8d/RPMS/x86_64/nginx-1.14.2-1.el7.x86_64.rpm {:level=>:info}
                                    执行(%clean): /bin/sh -e /tmp/rpm-tmp.P8JPPe {:level=>:info}
                                    Created package {:path=>"/tmp/nginx-1.14.2-1.el7.x86_64.rpm"}


                                    五、验证rpm包

                                      [root@localhost ~]# cd /tmp
                                      [root@localhost tmp]# rpm -qpi nginx-1.14.2-1.el7.x86_64.rpm
                                      Name : nginx
                                      Epoch : 0
                                      Version : 1.14.2
                                      Release : 1.el7
                                      Architecture: x86_64
                                      Install Date: (not installed)
                                      Group : Applications/Internet
                                      Size : 7430970
                                      License : BSD
                                      Signature : (none)
                                      Source RPM : nginx-1.14.2-1.el7.src.rpm
                                      Build Date : 2019年04月17日 星期三 22时51分54秒
                                      Build Host : localhost
                                      Relocations : /
                                      Packager : nginx
                                      Vendor : root@localhost.localdomain
                                      URL : http://nginx.org/
                                      Summary : nginx
                                      Description :
                                      nginx




                                      [root@localhost tmp]# rpm -qpl nginx-1.14.2-1.el7.x86_64.rpm
                                      /etc/nginx/fastcgi.conf
                                      /etc/nginx/fastcgi.conf.default
                                      /etc/nginx/fastcgi_params
                                      /etc/nginx/fastcgi_params.default
                                      /etc/nginx/koi-utf
                                      /etc/nginx/koi-win
                                      /etc/nginx/mime.types
                                      /etc/nginx/mime.types.default
                                      /etc/nginx/nginx.conf
                                      /etc/nginx/nginx.conf.default
                                      /etc/nginx/scgi_params
                                      /etc/nginx/scgi_params.default
                                      /etc/nginx/uwsgi_params
                                      /etc/nginx/uwsgi_params.default
                                      /etc/nginx/win-utf
                                      /usr/lib/systemd/system/nginx.service
                                      /usr/local/nginx/html/50x.html
                                      /usr/local/nginx/html/index.html
                                      /usr/local/nginx/logs
                                      /usr/local/nginx/sbin/nginx
                                      /var/log/nginx




                                      [root@localhost tmp]# rpm -qp --scripts nginx-1.14.2-1.el7.x86_64.rpm
                                      preinstall scriptlet (using /bin/sh):
                                      #!/bin/bash
                                      groupadd nginx
                                      useradd -s /sbin/nologin -g nginx -M nginx
                                      postinstall scriptlet (using /bin/sh):
                                      #!/bin/bash
                                      chmod a+x /usr/lib/systemd/system/nginx.service
                                      systemctl start nginx
                                      systemctl enable nginx
                                      preuninstall scriptlet (using /bin/sh):
                                      #!/bin/bash
                                      systemctl stop nginx
                                      pkill -9 nginx
                                      postuninstall scriptlet (using /bin/sh):
                                      #!/bin/bash
                                      userdel -r nginx
                                      rm -rf /usr/lib/systemd/system/nginx.service
                                      rm -rf /usr/local/nginx
                                      rm -rf /etc/nginx
                                      rm -rf /var/log/nginx


                                      六、安装rpm包

                                      方法1:

                                        [root@localhost ~]# rpm -ivh nginx-1.14.2-1.el7.x86_64.rpm

                                        可能提示缺失包


                                        方法2:

                                          [root@localhost ~]# yum -y localinstall nginx-1.14.2-1.el7.x86_64.rpm

                                          会先安装rpm打包说明的依赖包,然后再安装自己

                                          文章转载自赛里运维,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                                          评论