FPM 定制 Nginx rpm 包


:Mr.zhou  阅读: 2,523 次

  安装 ruby rubygems :

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

  使用阿里 rubygems 源 :

[root@local ~]# gem source list
*** CURRENT SOURCES ***

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

http://rubygems.org/
http://mirrors.aliyun.com/rubygems/
[root@local ~]# gem source -r http://rubygems.org/
http://rubygems.org/ removed from sources
[root@local ~]# gem source list
*** CURRENT SOURCES ***

http://mirrors.aliyun.com/rubygems/
[root@local ~]# 

  安装 fpm :

[root@local ~]# gem install fpm

  从官网下载Nginx-1.8.0源码包 :

[root@local ~]# cd /usr/local/src
[root@local src]# wget -q http://nginx.org/download/nginx-1.8.0.tar.gz
[root@local src]# ls
nginx-1.8.0.tar.gz
[root@local src]# tar -xf nginx-1.8.0.tar.gz
[root@local src]# ls
nginx-1.8.0  nginx-1.8.0.tar.gz
[root@local src]#

  约定 使用 nginx rpm 包安装后的效果

  Nginx 安装路径为 /application/nginx-1.8.0 并且做软链接到 /application/nginx 。将 nginx 命令加入到环境变量中。启动 Nginx 的用户和组为 www 用户,若系统中没有该用户则添加。

  卸载 nginx rpm 包后的效果

  删除前判断 Nginx 是否运行,若正在运行则将其停止。将 nginx 命令从环境变量中删除,删除 Nginx 的安装目录,及软链接。

  在执行 fpm 命令进行打包之前,需要先将要打包的软件编译安装到指定的目录。安装之前要手动解决依赖问题。rpm 包安装卸载的效果,用脚本实现。

  安装 Nginx 依赖包 :

[root@local src]# yum -y install openssl-devel pcre-devel
......
Installed:
  openssl-devel.x86_64 0:1.0.1e-42.el6       pcre-devel.x86_64 0:7.8-7.el6

Dependency Installed:
  keyutils-libs-devel.x86_64 0:1.4-5.el6
  krb5-devel.x86_64 0:1.10.3-42.el6
  libcom_err-devel.x86_64 0:1.41.12-22.el6
  libselinux-devel.x86_64 0:2.0.94-5.8.el6
  libsepol-devel.x86_64 0:2.0.41-4.el6
  zlib-devel.x86_64 0:1.2.3-29.el6

Dependency Updated:
  e2fsprogs.x86_64 0:1.41.12-22.el6    e2fsprogs-libs.x86_64 0:1.41.12-22.el6
  krb5-libs.x86_64 0:1.10.3-42.el6     libcom_err.x86_64 0:1.41.12-22.el6
  libss.x86_64 0:1.41.12-22.el6        openssl.x86_64 0:1.0.1e-42.el6
  pcre.x86_64 0:7.8-7.el6

Complete!
[root@local src]# 

  编译 :

[root@local src]# cd nginx-1.8.0
[root@local nginx-1.8.0]# ./configure --prefix=/application/nginx-1.8.0 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
......
[root@local nginx-1.8.0]# make
......
[root@local nginx-1.8.0]# 

  使用 DESTDIR= 选项将 Nginx 安装到指定的目录 /root/fpm-root :

[root@local nginx-1.8.0]# mkdir /root/fpm-root
[root@local nginx-1.8.0]# make install DESTDIR=/root/fpm-root
[root@local nginx-1.8.0]# ls /application
ls: cannot access /application: No such file or directory
[root@local nginx-1.8.0]# ls /root/fpm-root/
application
[root@local nginx-1.8.0]# tree -L 3 /root/fpm-root/
/root/fpm-root/
└── application
    └── nginx-1.8.0
        ├── conf
        ├── html
        ├── logs
        └── sbin

6 directories, 0 files
[root@local nginx-1.8.0]#

  在使用 fpm 对 /root/fpm-root 进行打包之前可以修改 Nginx 的相关配置文件进行配置优化,以达到在制作好 rpm 后,进行 rpm 包安装,已是最优的配置文件。

  下面只对默认主页内容进行修改,以达到演示的目的。

[root@local nginx-1.8.0]# echo 'www.z-dig.com'>/root/fpm-root/application/nginx-1.8.0/html/index.html
[root@local nginx-1.8.0]# cat /root/fpm-root/application/nginx-1.8.0/html/index.html
www.z-dig.com
[root@local nginx-1.8.0]#

  在使用 fpm 打包时可以使用 –after-install FILE ,–before-install FILE ,–after-remove FILE ,–before-remove FILE ,–after-upgrade FILE ,–before-upgrade 选项来分别制定安装后、安装前、删除后、删除前、升级后、升级前要执行的脚本。

  编写脚本以实现安装前、安装后、删除前、删除后的需求。

[root@local nginx-1.8.0]# mkdir /root/fpm-scripts
[root@local nginx-1.8.0]# vim /root/fpm-scripts/before-install.sh
[root@local nginx-1.8.0]# vim /root/fpm-scripts/after-install.sh
[root@local nginx-1.8.0]# vim /root/fpm-scripts/before-remove.sh
[root@local nginx-1.8.0]# vim /root/fpm-scripts/after-remove.sh
[root@local nginx-1.8.0]#


[root@local nginx-1.8.0]# cat /root/fpm-scripts/before-install.sh
/usr/bin/id www &>/dev/null
if [ $? -ne 0 ] ; then
  /usr/sbin/useradd -M -s /sbin/nologin www
fi
[root@local nginx-1.8.0]#


[root@local nginx-1.8.0]# cat /root/fpm-scripts/after-install.sh
/bin/ln -s /application/nginx-1.8.0 /application/nginx &>/dev/null
for i in $(/bin/ls /application/nginx/sbin/)
do
  /bin/ln -s /application/nginx/sbin/$i /usr/local/sbin/ &>/dev/null
done
[root@local nginx-1.8.0]#


[root@local nginx-1.8.0]# cat /root/fpm-scripts/before-remove.sh
for i in $(/bin/ls /application/nginx/sbin/)
do
  /bin/rm -f /usr/local/sbin/$i &>/dev/null
done
PS=$(/bin/ps -ef|/bin/grep 'nginx:'|/bin/grep -v 'grep'|/usr/bin/wc -l)
if [  $PS -ne 0 ] ; then
  /application/nginx/sbin/nginx -s stop &>/dev/null
fi
[root@local nginx-1.8.0]#


[root@local nginx-1.8.0]# cat /root/fpm-scripts/after-remove.sh
/bin/rm -f /application/nginx
/bin/rm -rf /application/nginx-1.8.0
[root@local nginx-1.8.0]#

  用 fpm 命令进行打包。使用 -p 选项将生成的 rpm 包存放在 /root/fpm-rpm 下,-d 选项为指定该软件的依赖。其他选项使用 fpm –help 查看帮助.

[root@local nginx-1.8.0]# mkdir /root/fpm-rpm

[root@local nginx-1.8.0]# fpm -f -t rpm -n nginx -v 1.8.0 --iteration 1.el6 --epoch 1  -s dir -C /root/fpm-root -p /root/fpm-rpm --license 'GPL' -d 'openssl-devel' -d 'pcre-devel' --before-install /root/fpm-scripts/before-install.sh --after-install /root/fpm-scripts/after-install.sh --before-remove /root/fpm-scripts/before-remove.sh --after-remove /root/fpm-scripts/after-remove.sh --description 'Nginx Http Server 1.8.0' --url 'http://www.nginx.org' -m 'Mr.zhou'
Created package {:path=>"/root/fpm-rpm/nginx-1.8.0-1.el6.x86_64.rpm"}
[root@local nginx-1.8.0]#

[root@local nginx-1.8.0]# ls /root/fpm-rpm/
nginx-1.8.0-1.el6.x86_64.rpm
[root@local nginx-1.8.0]#

  在新机器进行测试 :

[root@s1 ~]# ifconfig eth0|awk -F '[ :]+' 'NR==2{print $4}'
172.16.1.20
[root@s1 ~]# rpm -qa openssl-devel pcre-devel nginx
[root@s1 ~]#
[root@s1 ~]# lsof -i :80
[root@s1 ~]#
[root@s1 ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog  src
[root@s1 ~]#
[root@local nginx-1.8.0]# cd /root/fpm-rpm/
[root@local fpm-rpm]# ls
nginx-1.8.0-1.el6.x86_64.rpm
[root@local fpm-rpm]# rsync nginx-1.8.0-1.el6.x86_64.rpm root@172.16.1.20:~
[root@s1 ~]# ls
anaconda-ks.cfg  install.log.syslog            src
install.log      nginx-1.8.0-1.el6.x86_64.rpm
[root@s1 ~]#

  在新机器安装 Nginx :

[root@s1 ~]# yum localinstall nginx-1.8.0-1.el6.x86_64.rpm
......
Installed:
  nginx.x86_64 1:1.8.0-1.el6

Dependency Installed:
  keyutils-libs-devel.x86_64 0:1.4-5.el6
  krb5-devel.x86_64 0:1.10.3-42.el6
  libcom_err-devel.x86_64 0:1.41.12-22.el6
  libselinux-devel.x86_64 0:2.0.94-5.8.el6
  libsepol-devel.x86_64 0:2.0.41-4.el6
  openssl-devel.x86_64 0:1.0.1e-42.el6
  pcre-devel.x86_64 0:7.8-7.el6
  zlib-devel.x86_64 0:1.2.3-29.el6

Dependency Updated:
  e2fsprogs.x86_64 0:1.41.12-22.el6    e2fsprogs-libs.x86_64 0:1.41.12-22.el6
  krb5-libs.x86_64 0:1.10.3-42.el6     libcom_err.x86_64 0:1.41.12-22.el6
  libss.x86_64 0:1.41.12-22.el6        openssl.x86_64 0:1.0.1e-42.el6
  pcre.x86_64 0:7.8-7.el6

Complete!
[root@s1 ~]#

  检查安装结果 :

[root@s1 ~]# ls /application/
nginx  nginx-1.8.0
[root@s1 ~]# which nginx
/usr/local/sbin/nginx
[root@s1 ~]# nginx
[root@s1 ~]# lsof -i :80
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   1386 root    6u  IPv4  13114      0t0  TCP *:http (LISTEN)
nginx   1387  www    6u  IPv4  13114      0t0  TCP *:http (LISTEN)
[root@s1 ~]# curl -I 172.16.1.20
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Wed, 09 Sep 2015 16:22:20 GMT
Content-Type: text/html
Content-Length: 14
Last-Modified: Wed, 09 Sep 2015 16:09:37 GMT
Connection: keep-alive
ETag: "55f059c1-e"
Accept-Ranges: bytes

[root@s1 ~]# curl 172.16.1.20
www.z-dig.com
[root@s1 ~]#

[root@s1 ~]# rpm -qa nginx
nginx-1.8.0-1.el6.x86_64
[root@s1 ~]# rpm -qi nginx
Name        : nginx                        Relocations: /
Version     : 1.8.0                             Vendor: root@local
Release     : 1.el6                         Build Date: Thu 10 Sep 2015 12:09:38 AM CST
Install Date: Thu 10 Sep 2015 12:20:27 AM CST      Build Host: local
Group       : default                       Source RPM: nginx-1.8.0-1.el6.src.rpm
Size        : 5418076                          License: GPL
Signature   : (none)
Packager    : Mr.zhou
URL         : http://www.nginx.org
Summary     : Nginx Http Server 1.8.0
Description :
Nginx Http Server 1.8.0
[root@s1 ~]#

  安装前后脚本执行成功。

  删除 Nginx :

[root@s1 ~]# rpm -e nginx
[root@s1 ~]# rpm -qa nginx
[root@s1 ~]#

  检查删除后的结果:

[root@s1 ~]# lsof -i :80
[root@s1 ~]# ls /application/
[root@s1 ~]# ls /usr/local/sbin/
[root@s1 ~]# curl -I 172.16.1.20
curl: (7) couldn't connect to host
[root@s1 ~]#

  删除前后脚本执行成功。

  到此,FPM 打包测试已成功。在工作中可以根据具体的需求将多个软件,优化好配置文件后打包成一个 rpm 包,通过在局域网内部署 yum 源 ,达到快速的批量部署。注意升级软件包、打补丁时 配置文件、用户数据的相关操作。不要误删!


转载请注明原文链接:http://www.z-dig.com/fpm-custom-nginx-rpm-package.html



正文部分到此结束