一、安装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 fpmFetching: cabin-0.9.0.gem (100%)Successfully installed cabin-0.9.0Fetching: backports-3.13.0.gem (100%)Successfully installed backports-3.13.0Fetching: arr-pm-0.0.10.gem (100%)Successfully installed arr-pm-0.0.10Fetching: clamp-1.0.1.gem (100%)Successfully installed clamp-1.0.1Fetching: ffi-1.10.0.gem (100%)Building native extensions. This could take a while...Successfully installed ffi-1.10.0Fetching: childprocess-0.9.0.gem (100%)Successfully installed childprocess-0.9.0Fetching: io-like-0.3.0.gem (100%)Successfully installed io-like-0.3.0Fetching: ruby-xz-0.2.3.gem (100%)Successfully installed ruby-xz-0.2.3Fetching: stud-0.0.23.gem (100%)Successfully installed stud-0.0.23Fetching: mustache-0.99.8.gem (100%)Successfully installed mustache-0.99.8Fetching: insist-1.0.0.gem (100%)Successfully installed insist-1.0.0Fetching: dotenv-2.7.2.gem (100%)Successfully installed dotenv-2.7.2Fetching: pleaserun-0.0.30.gem (100%)Successfully installed pleaserun-0.0.30Fetching: fpm-1.11.0.gem (100%)Successfully installed fpm-1.11.0Parsing documentation for cabin-0.9.0Installing ri documentation for cabin-0.9.0Parsing documentation for backports-3.13.0Installing ri documentation for backports-3.13.0Parsing documentation for arr-pm-0.0.10Installing ri documentation for arr-pm-0.0.10Parsing documentation for clamp-1.0.1Installing ri documentation for clamp-1.0.1Parsing documentation for ffi-1.10.0Installing ri documentation for ffi-1.10.0Parsing documentation for childprocess-0.9.0Installing ri documentation for childprocess-0.9.0Parsing documentation for io-like-0.3.0Installing ri documentation for io-like-0.3.0Parsing documentation for ruby-xz-0.2.3Installing ri documentation for ruby-xz-0.2.3Parsing documentation for stud-0.0.23Installing ri documentation for stud-0.0.23Parsing documentation for mustache-0.99.8Installing ri documentation for mustache-0.99.8Parsing documentation for insist-1.0.0Installing ri documentation for insist-1.0.0Parsing documentation for dotenv-2.7.2Installing ri documentation for dotenv-2.7.2Parsing documentation for pleaserun-0.0.30Installing ri documentation for pleaserun-0.0.30Parsing documentation for fpm-1.11.0Installing ri documentation for fpm-1.11.014 gems installed
题外话:如果操作系统为CentOS6,可能会提示ruby版本问题,这个时候我们指定安装老版本(因为升级到新版本很麻烦)
[root@localhost ~]# gem install fpmBuilding 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 -v2.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 serverAfter=network.target remote-fs.target nss-lookup.target[Service]Type=forkingPIDFile=/usr/local/nginx/logs/nginx.pidExecStartPre=/usr/bin/rm -f run/nginx.pidExecStartPre=/usr/local/nginx/sbin/nginx -tExecStart=/usr/local/nginx/sbin/nginxExecReload=/bin/kill -s HUP $MAINPIDKillMode=processKillSignal=SIGQUITTimeoutStopSec=5PrivateTmp=true[Install]WantedBy=multi-user.target
三、nginx脚本
创建安装前的脚本:
[root@localhost ~]# vim root/soft/nginx-1.14.2/pre_install.sh#!/bin/bashgroupadd nginxuseradd -s /sbin/nologin -g nginx -M nginx
创建安装后的脚本:
[root@localhost ~]# vim root/soft/nginx-1.14.2/post_install.sh#!/bin/bashchmod a+x usr/lib/systemd/system/nginx.servicesystemctl start nginxsystemctl enable nginx
创建卸载前的脚本:
[root@localhost ~]# vim root/soft/nginx-1.14.2/pre_uninstall.sh#!/bin/bashsystemctl stop nginxpkill -9 nginx
创建卸载后的脚本:
[root@localhost ~]# vim root/soft/nginx-1.14.2/post_uninstall.sh#!/bin/bashuserdel -r nginxrm -rf /usr/lib/systemd/system/nginx.servicerm -rf /usr/local/nginxrm -rf /etc/nginxrm -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-signNo 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.rpmName : nginxEpoch : 0Version : 1.14.2Release : 1.el7Architecture: x86_64Install Date: (not installed)Group : Applications/InternetSize : 7430970License : BSDSignature : (none)Source RPM : nginx-1.14.2-1.el7.src.rpmBuild Date : 2019年04月17日 星期三 22时51分54秒Build Host : localhostRelocations : /Packager : nginxVendor : root@localhost.localdomainURL : http://nginx.org/Summary : nginxDescription :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.rpmpreinstall scriptlet (using /bin/sh):#!/bin/bashgroupadd nginxuseradd -s /sbin/nologin -g nginx -M nginxpostinstall scriptlet (using /bin/sh):#!/bin/bashchmod a+x /usr/lib/systemd/system/nginx.servicesystemctl start nginxsystemctl enable nginxpreuninstall scriptlet (using /bin/sh):#!/bin/bashsystemctl stop nginxpkill -9 nginxpostuninstall scriptlet (using /bin/sh):#!/bin/bashuserdel -r nginxrm -rf /usr/lib/systemd/system/nginx.servicerm -rf /usr/local/nginxrm -rf /etc/nginxrm -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进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




