利用FPM打包工具定制RPM包

1.FPM安装

    #FPM安装
    yum -y install ruby rubygems ruby-devel
    #外国的源慢,换国内源
    gem sources --add https://gems.ruby-china.com/ --remove http://rubygems.org/
    # 安装fpm,gem从rubygem仓库安装软件类似yum从yum仓库安装软件。首先安装低版本的json,高版本的json需要ruby2.0以上,然后安装低版本的fpm,够用。
    #Centos6安装
    gem install json -v 1.8.3
    gem install fpm -v 1.3.3
    #Centos7安装
    gem install fpm

2.FPM参数

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

3.实战:编译安装nginx,利用FPM打包

    #安装nginx
    yum install gcc pcre-devel openssl-devel -y
    useradd nginx -M -s /sbin/nologin
    
    mkdir /application /src 
    cd /src
    wget http://nginx.org/download/nginx-1.6.3.tar.gz
    tar xf nginx-1.6.3.tar.gz
    cd nginx-1.6.3
    ./configure --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
    make && make install
    ln -s /application/nginx-1.6.3/ /application/nginx
    
    mkdir /scripts /my_rpm
    cat>>/scripts/nginx_rpm.sh<<EOF
    #!/bin/bash
    useradd nginx -M -s /sbin/nologin
    ln -s /application/nginx-1.6.3/ /application/nginx
    EOF
    cd /my_rpm
    fpm -s dir -t rpm -n nginx -v 1.6.3 -d 'pcre-devel,openssl-devel' --post-install /scripts/nginx_rpm.sh -f /application/nginx-1.6.3/ 
    #报错:Need executable 'rpmbuild' to convert dir to rpm {:level=>:error}
    yum install rpm-build -y
    [root@zon161 my_rpm]# ll -h 
    total 2.3M
    -rw-r--r-- 1 root root 2.3M Oct 25 01:05 nginx-1.6.3-1.x86_64.rpm

4.测试安装rpm包

    #rpm命令安装
    [root@zon162 ~]# rpm -ivh nginx-1.6.3-1.x86_64.rpm 
    error: Failed dependencies:
    	pcre-devel is needed by nginx-1.6.3-1.x86_64
    	openssl-devel is needed by nginx-1.6.3-1.x86_64
    报如上依赖错误,需要先yum安装依赖才能安装rpm包。
    #yum命令安装rpm包
    [root@zon162 ~]# yum -y localinstall nginx-1.6.3-1.x86_64.rpm 

注意事项

1.路径问题
	# 相对路径
    [root@zon161 my_rpm]# cd /application/nginx/
    [root@zon161 nginx]# fpm -s dir -t rpm -n nginx -v 1.6.3 .
    Created package {:path=>"nginx-1.6.3-1.x86_64.rpm"}
    [root@zon161 nginx]# rpm -qpl nginx-1.6.3-1.x86_64.rpm 
    /client_body_temp
    /conf/fastcgi.conf
    /conf/fastcgi.conf.default
    /conf/fastcgi_params
    /conf/fastcgi_params.default
    /conf/koi-utf
    /conf/koi-win
    /conf/mime.types
    /conf/mime.types.default
    /conf/nginx.conf
    /conf/nginx.conf.default
    /conf/scgi_params
    /conf/scgi_params.default
    /conf/uwsgi_params
    /conf/uwsgi_params.default
    /conf/win-utf
    /fastcgi_temp
    /html/50x.html
    /html/index.html
    /logs/access.log
    /logs/error.log
    /logs/nginx.pid
    /proxy_temp
    /sbin/nginx
    /scgi_temp
    /uwsgi_temp
    ###########################
    # 绝对路径
    [root@zon161 ~]# fpm -s dir -t rpm -n nginx -v 1.6.3 /application/nginx-1.6.3
    Created package {:path=>"nginx-1.6.3-1.x86_64.rpm"}
    [root@zon161 ~]# rpm -qpl nginx-1.6.3-1.x86_64.rpm 
    /application/nginx-1.6.3/client_body_temp
    /application/nginx-1.6.3/conf/fastcgi.conf
    /application/nginx-1.6.3/conf/fastcgi.conf.default
    /application/nginx-1.6.3/conf/fastcgi_params
    /application/nginx-1.6.3/conf/fastcgi_params.default
    /application/nginx-1.6.3/conf/koi-utf
    /application/nginx-1.6.3/conf/koi-win
    /application/nginx-1.6.3/conf/mime.types
    /application/nginx-1.6.3/conf/mime.types.default
    /application/nginx-1.6.3/conf/nginx.conf
    /application/nginx-1.6.3/conf/nginx.conf.default
    /application/nginx-1.6.3/conf/scgi_params
    /application/nginx-1.6.3/conf/scgi_params.default
    /application/nginx-1.6.3/conf/uwsgi_params
    /application/nginx-1.6.3/conf/uwsgi_params.default
    /application/nginx-1.6.3/conf/win-utf
    /application/nginx-1.6.3/fastcgi_temp
    /application/nginx-1.6.3/html/50x.html
    /application/nginx-1.6.3/html/index.html
    /application/nginx-1.6.3/logs/access.log
    /application/nginx-1.6.3/logs/error.log
    /application/nginx-1.6.3/logs/nginx.pid
    /application/nginx-1.6.3/nginx-1.6.3-1.x86_64.rpm
    /application/nginx-1.6.3/proxy_temp
    /application/nginx-1.6.3/sbin/nginx
    /application/nginx-1.6.3/scgi_temp
    /application/nginx-1.6.3/uwsgi_temp
    #############################
    #使用rpm -qpl 命令可以查看rpm包的内容。
    #注:fpm类似tar打包一样,只是fpm打的包能够被yum命令识别而已。
2. 软链接问题
    [root@zon161 ~]# fpm -s dir -t rpm -n nginx -v 1.6.3 /application/nginx
    File already exists, refusing to continue: nginx-1.6.3-1.x86_64.rpm {:level=>:fatal}
    #报错是因为当前目录存在同名的rpm包,可以使用-f参数强制覆盖。
    [root@zon161 ~]# fpm -s dir -t rpm -n nginx -v 1.6.3 -f /application/nginx
    Force flag given. Overwriting package at nginx-1.6.3-1.x86_64.rpm {:level=>:warn}
    Created package {:path=>"nginx-1.6.3-1.x86_64.rpm"}
    #打包成功,但查看实际内容是个软链接文件
    [root@zon161 ~]# rpm -qpl nginx-1.6.3-1.x86_64.rpm 
    /application/nginx
    #根本原因在于结尾的/,类似rm删除软链接目录
    [root@zon161 ~]# fpm -s dir -t rpm -n nginx -v 1.6.3 -f /application/nginx/
    Force flag given. Overwriting package at nginx-1.6.3-1.x86_64.rpm {:level=>:warn}
    Created package {:path=>"nginx-1.6.3-1.x86_64.rpm"}
    [root@zon161 ~]# rpm -qpl nginx-1.6.3-1.x86_64.rpm 
    /application/nginx/client_body_temp
    /application/nginx/conf/fastcgi.conf
    /application/nginx/conf/fastcgi.conf.default
    /application/nginx/conf/fastcgi_params
    /application/nginx/conf/fastcgi_params.default
    /application/nginx/conf/koi-utf
    /application/nginx/conf/koi-win
    /application/nginx/conf/mime.types
    /application/nginx/conf/mime.types.default
    /application/nginx/conf/nginx.conf
    /application/nginx/conf/nginx.conf.default
    /application/nginx/conf/scgi_params
    /application/nginx/conf/scgi_params.default
    /application/nginx/conf/uwsgi_params
    /application/nginx/conf/uwsgi_params.default
    /application/nginx/conf/win-utf
    /application/nginx/fastcgi_temp
    /application/nginx/html/50x.html
    /application/nginx/html/index.html
    /application/nginx/logs/access.log
    /application/nginx/logs/error.log
    /application/nginx/logs/nginx.pid
    /application/nginx/nginx-1.6.3-1.x86_64.rpm
    /application/nginx/proxy_temp
    /application/nginx/sbin/nginx
    /application/nginx/scgi_temp
    /application/nginx/uwsgi_temp

定制LNMP的RPM包思路

编译安装好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/

参考:http://www.zyops.com/autodeploy-rpm/

你可能感兴趣的:(Linux运维)