FPM打包工具製做Tengine爲RPM包詳解

目錄php

一、Tengine編譯安裝html

二、FPM製做Tengine爲RPM包nginx

三、總結git

一、Tengine編譯安裝
github

[root@php ~]# cat /etc/issue
CentOS release 6.4 (Final)
Kernel \r on an \m
[root@php ~]# uname -r
2.6.32-358.el6.x86_64

請確保系統安裝了"Development tools"和」Server Platform Development」兩個開發包組,若是沒有安裝請用yum工具安裝便可。正則表達式

所須要的軟件包:vim

[root@php tengine]# pwd
/root/software/tengine
[root@php tengine]# ls
jemalloc-3.6.0.tar.bz2    pcre-8.33.zip  tengine-2.1.0.tar.gz

jemalloc-3.6.0.tar.bz2ruby

是爲tengine提供更好的內存管理的(http://www.canonware.com/jemalloc/)bash

pcre-8.33.zip  服務器

PCRE(Perl Compatible Regular Expressions)是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx rewrite依賴於PCRE庫,因此在安裝Tengine前必定要先安裝PCRE(http://www.pcre.org)

tengine-2.1.0.tar.gz  tengine主程序(http://tengine.taobao.org)

解壓各軟件:

[root@php tengine]# unzip -q pcre-8.33.zip
[root@php tengine]# tar xf jemalloc-3.6.0.tar.bz2
[root@php tengine]# tar xf tengine-2.1.0.tar.gz 
[root@php tengine]# ls
jemalloc-3.6.0                pcre-8.33      tengine-2.1.0
jemalloc-3.6.0.tar.bz2    pcre-8.33.zip  tengine-2.1.0.tar.gz

安裝兩個依賴包:

[root@php tengine]# cd pcre-8.33
[root@php pcre-8.33]# ./configure --prefix=/usr/local/pcre8.33
[root@php pcre-8.33]# make && make install
[root@php openssl-1.0.2]# make && make install
[root@php tengine]# cd jemalloc-3.6.0
[root@php jemalloc-3.6.0]# ./configure --prefix=/usr/local/jemalloc
[root@php jemalloc-3.6.0]# make && make install

上邊的這pcre與jemalloc兩個包能夠不安裝,由於在tengine編譯時是引用的兩個包的源文件,並非安裝後的目錄。

tengine編譯安裝:

[root@php jemalloc-3.6.0]# cd ../tengine-2.1.0
[root@php tengine-2.1.0]# ./configure \
--prefix=/usr/local/tengine \
--sbin-path=/usr/local/tengine/sbin/nginx \
--conf-path=/etc/tengine/nginx.conf \
--error-log-path=/var/log/tengine/error.log \
--http-log-path=/var/log/tengine/access.log \
--pid-path=/var/run/tengine.pid \
--lock-path=/var/lock/subsys/tengine \
--user=nginx \
--group=nginx \
--with-file-aio \
--with-pcre=/root/software/tengine/pcre-8.33 \
--http-client-body-temp-path=/var/tmp/tengine/client \
--http-proxy-temp-path=/var/tmp/tengine/proxy \
--http-fastcgi-temp-path=/var/tmp/tengine/fastcgi \
--http-uwsgi-temp-path=/var/tmp/tengine/uwsgi \
--dso-path=/usr/local/tengine/modules \
--dso-tool-path=/usr/local/tengine/modules/dso_tool \
--with-jemalloc \
--with-jemalloc=/root/software/tengine/jemalloc-3.6.0
#此次編譯安裝我沒有顯式的指定編譯進哪些模塊,由於在安裝好後我想看看tengine在不指定編譯進哪些模塊時默認安裝了哪些模塊。
[root@php tengine-2.1.0]# make
[root@php tengine-2.1.0]# make install
[root@php tengine-2.1.0]# /usr/local/tengine/sbin/nginx -v
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
[root@php tengine-2.1.0]# /usr/local/tengine/sbin/nginx -m
  #看看吧,下邊就是tengine默認時安裝了哪些模塊,經常使用的模塊都已經編譯進來了,因此在configure時真
的不須要指定安裝哪些模塊,若是默認模塊中沒有你須要的,那能夠經過dso的方式編譯進tengine,再動態加載進tengine便可。
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
loaded modules:
    ngx_core_module (static)
    ngx_errlog_module (static)
    ngx_conf_module (static)
    ngx_dso_module (static)
    ngx_syslog_module (static)
    ngx_events_module (static)
    ngx_event_core_module (static)
    ngx_epoll_module (static)
    ngx_procs_module (static)
    ngx_proc_core_module (static)
    ngx_openssl_module (static)
    ngx_regex_module (static)
    ngx_http_module (static)
    ngx_http_core_module (static)
    ngx_http_log_module (static)
    ngx_http_upstream_module (static)
    ngx_http_static_module (static)
    ngx_http_autoindex_module (static)
    ngx_http_index_module (static)
    ngx_http_auth_basic_module (static)
    ngx_http_access_module (static)
    ngx_http_limit_conn_module (static)
    ngx_http_limit_req_module (static)
    ngx_http_geo_module (static)
    ngx_http_map_module (static)
    ngx_http_split_clients_module (static)
    ngx_http_referer_module (static)
    ngx_http_rewrite_module (static)
    ngx_http_ssl_module (static)
    ngx_http_proxy_module (static)
    ngx_http_fastcgi_module (static)
    ngx_http_uwsgi_module (static)
    ngx_http_scgi_module (static)
    ngx_http_memcached_module (static)
    ngx_http_empty_gif_module (static)
    ngx_http_browser_module (static)
    ngx_http_user_agent_module (static)
    ngx_http_upstream_ip_hash_module (static)
    ngx_http_upstream_consistent_hash_module (static)
    ngx_http_upstream_check_module (static)
    ngx_http_upstream_least_conn_module (static)
    ngx_http_reqstat_module (static)
    ngx_http_upstream_keepalive_module (static)
    ngx_http_upstream_dynamic_module (static)
    ngx_http_stub_status_module (static)
    ngx_http_write_filter_module (static)
    ngx_http_header_filter_module (static)
    ngx_http_chunked_filter_module (static)
    ngx_http_range_header_filter_module (static)
    ngx_http_gzip_filter_module (static)
    ngx_http_postpone_filter_module (static)
    ngx_http_ssi_filter_module (static)
    ngx_http_charset_filter_module (static)
    ngx_http_userid_filter_module (static)
    ngx_http_footer_filter_module (static)
    ngx_http_trim_filter_module (static)
    ngx_http_headers_filter_module (static)
    ngx_http_upstream_session_sticky_module (static)
    ngx_http_copy_filter_module (static)
    ngx_http_range_body_filter_module (static)
    ngx_http_not_modified_filter_module (static)

爲tengine提供服務啓動腳本:

[root@php tengine-2.1.0]# vim /etc/rc.d/init.d/nginx
#!/bin/bash
##
#nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:  Tengine is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
 
# 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/tengine/sbin/nginx"
prog=$(basename $nginx)
nginx_config_file="/etc/tengine/nginx.conf"
lockfile=/var/lock/subsys/tengine
 
make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   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_config_file ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $nginx_config_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: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
 
force_reload() {
    restart
}
 
configtest() {
  $nginx -t -c $nginx_config_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
[root@php tengine-2.1.0]# chmod +x /etc/rc.d/init.d/nginx

測試tengine的啓動與中止:

[root@php tengine-2.1.0]# service nginx start
Starting nginx:                                            [  OK  ]
[root@php tengine-2.1.0]# service nginx stop
Stopping nginx:                                            [  OK  ]

在編譯安裝tengine時可能會遇到以下錯誤:

sr/local/jemalloc-3.6.0/lib/libjemalloc.a -lpthread
TEST_NGINX_BINARY=/root/software/tengine/tengine-2.1.0/objs/nginx prove -v -I /root/software/tengine/tengine-2.1.0/tests/nginx-tests/nginx-tests/lib tests/nginx-tests/nginx-tests tests/nginx-tests/cases
/bin/sh: prove: command not found
make[1]: *** [test] Error 127
make[1]: Leaving directory `/root/software/tengine/tengine-2.1.0'
make: *** [test] Error 2

處理方法:

[root@php tengine-2.1.0]# yum provides *bin/prove
  #得到此命令是哪一個包生成的
perl-Test-Harness-3.17-136.el6.x86_64 : Run Perl standard test scripts with statistics
Repo        : base
Matched from:
Filename    : /usr/bin/prove
perl-Test-Harness-3.17-136.el6_6.1.x86_64 : Run Perl standard test scripts with statistics
Repo        : updates
Matched from:
Filename    : /usr/bin/prove
[root@php tengine-2.1.0]# yum -y install perl-Test-Harness  #安裝相應的包便可

二、FPM製做Tengine爲RPM包

FPM是ruby的模塊,先安裝FPM依賴的包:

[root@php tengine]# yum -y install ruby rubygems ruby-devel rpm-build

因國內網絡環境,訪問http://rubygems.org/站點時不穩定,因此增長國內toabao提供的一個鏡像站點,所原來的站點移除:

[root@php tengine]# gem sources -a http://ruby.taobao.org/
[root@php tengine]# gem sources -r http://rubygems.org/

安裝FPM:

[root@php tengine]# gem install fpm

轉換到上邊編譯tengine的工做目錄,先作一些打包前的準備工做:

[root@php tengine-2.1.0]# pwd
/root/software/tengine/tengine-2.1.0

建立一個安裝tengine的臨時目錄,fpm能夠從這個目錄是讀取信息:

[root@php tengine-2.1.0]# mkdir /tmp/installdir    

把tengine安裝到此臨時目錄中:

[root@php tengine-2.1.0]# make install DESTDIR=/tmp/installdir/   

[root@php tengine-2.1.0]# cd /tmp/installdir/

查看一下目錄結構:

[root@php installdir]# tree -L 3
.
├── etc
│   └── tengine
│       ├── browsers
│       ├── fastcgi.conf
│       ├── fastcgi.conf.default
│       ├── fastcgi_params
│       ├── fastcgi_params.default
│       ├── koi-utf
│       ├── koi-win
│       ├── mime.types
│       ├── mime.types.default
│       ├── module_stubs
│       ├── nginx.conf
│       ├── nginx.conf.default
│       ├── scgi_params
│       ├── scgi_params.default
│       ├── uwsgi_params
│       ├── uwsgi_params.default
│       └── win-utf
├── usr
│   └── local
│       └── tengine
└── var
    ├── log
    │   └── tengine
    └── run
[root@php installdir]# pwd
/tmp/installdir
[root@php installdir]# ls
etc  usr  var

建立放置服務啓動腳本的目錄:

[root@php installdir]# mkdir -pv etc/rc.d/init.d    
mkdir: created directory `etc/rc.d'
mkdir: created directory `etc/rc.d/init.d'
[root@php installdir]# cp /etc/rc.d/init.d/nginx ./etc/rc.d/init.d  #把服務啓動腳本拷貝過來

準備rpm安裝後及卸載後所須要運行的腳本:

[root@php installdir]# mkdir tmp;cd tmp  #這個目錄是放置如下兩個腳本的目錄
[root@php tmp]# vim install_after.sh
#!/bin/bash
#
 add user nginx
source /etc/rc.d/init.d/functions
getent group nginx > /dev/null || groupadd -r nginx
getent passwd nginx > /dev/null || useradd -r -g nginx -s /sbin/nologin nginx
exit 0
#此腳本是在tengine安裝好後能夠檢測系統上是否有nginx組和用戶,若是沒有就建立。
[root@php tmp]# vim remove_after.sh
#!/bin/bash
#
source /etc/rc.d/init.d/functions 
rm -rf /usr/local/tengine
rm -rf /etc/tengine
userdel nginx
exit 0
#此腳本是在卸載tengine時刪除在安裝時生成的各個目錄及建立的用戶

製做rpm包:

[root@php ~]#  fpm -s dir -t rpm -n tengine -v 2.1.0 --iteration 1.el6 -C /tmp/installdir/ -p /root --description 'tengine rpm' --url 'tengine.taobao.org' --post-install /tmp/installdir/tmp/install_after.sh --post-uninstall /tmp/installdir/tmp/remove_after.sh  
no value for epoch is set, defaulting to nil {:level=>:warn}
no value for epoch is set, defaulting to nil {:level=>:warn}
Created package {:path=>"/root/tengine-2.1.0-1.el6.x86_64.rpm"}
#fpm命令詳細參數解釋能夠用"fpm -h"查看
[root@php ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog  software  tengine-2.1.0-1.el6.x86_64.rpm

rpm文件生成後就能夠拷貝到其餘服務器進行測試:

[root@php ~]# scp tengine-2.1.0-1.el6.x86_64.rpm 192.168.0.203:/root
[root@nod3 ~]# rpm -ivh tengine-2.1.0-1.el6.x86_64.rpm
Preparing...                ########################################### [100%]
   1:tengine                ########################################### [100%]
[root@nod3 ~]# rpm -qp --scripts tengine-2.1.0-1.el6.x86_64.rpm 
#查看rpm文件中的腳本信息
postinstall scriptlet (using /bin/sh):
#!/bin/bash
#
source /etc/rc.d/init.d/functions
getent group nginx > /dev/null || groupadd -r nginx
getent passwd nginx > /dev/null || useradd -r -g nginx -s /sbin/nologin nginx
exit 0
postuninstall scriptlet (using /bin/sh):
#!/bin/bash
#
source /etc/rc.d/init.d/functions 
rm -rf /usr/local/tengine
rm -rf /etc/tengine
rm -rf /etc/rc.d/init.d/nginx
userdel nginx
exit 0

tengine安裝好後來驗證各個目錄及用戶是否生成:

[root@nod3 ~]# id nginx
uid=496(nginx) gid=496(nginx) groups=496(nginx)
[root@nod3 ~]# ls /etc/tengine/
browsers              fastcgi_params.default  mime.types.default  scgi_params           win-utf
fastcgi.conf          koi-utf                 module_stubs        scgi_params.default
fastcgi.conf.default  koi-win                 nginx.conf          uwsgi_params
fastcgi_params        mime.types              nginx.conf.default  uwsgi_params.default
[root@nod3 ~]# ls /usr/local/tengine/
html  include  modules  sbin  scgi_temp
[root@nod3 ~]# ls /etc/rc.d/init.d/nginx
/etc/rc.d/init.d/nginx

啓動服務器來檢測tengine是否能正常運行:

[root@nod3 ~]# service nginx start
Starting nginx:                                            [  OK  ]
[root@nod3 ~]# netstat -tnlp | grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      2257/nginx

再來測試一下卸載tengine後各個目錄是否能刪除:

[root@nod3 ~]# rpm -e tengine
[root@nod3 ~]# id nginx
id: nginx: No such user
[root@nod3 ~]# ls /etc/ | grep tengine
[root@nod3 ~]# ls /usr/local/ | grep tengine
[root@nod3 ~]# ls /etc/rc.d/init.d/ | grep nginx
# 軟件卸載後各個目錄都已被成功刪除了。

三、總結

    FPM很是易用,有了此工具不再用擔憂rpm包的製做,在用此命令時能夠把rpm包的安裝、卸載作得更加優雅,在安裝前能夠作一些準備工做,安裝後能夠作一些收尾工做,在卸載前也能夠作一些準備,好比檢測一下相應的服務是否中止了,在卸載軟件再作一些掃尾的工做,只要把這些定義成一個個腳本,fpm中指定相應的選項便可輕鬆實現。這裏涉及的參數是:

--pre-install FILE:表示安裝以前所要運行的腳本

--post-install FILE:表示安裝以後所要運行的腳本

--pre-uninstall FILE:表示卸載以前所要運行的腳本

--post-uninstall FILE:表示卸載以後所要運行的腳本

參考文章:

http://www.xiaomastack.com/2014/11/02/scribe-rpm/

http://blog.sina.com.cn/s/blog_704836f40101fscj.html

https://github.com/jordansissel/fpm/wiki

http://www.iyunv.com/thread-58083-1-1.html

相關文章
相關標籤/搜索