http://zhaochj.blog.51cto.com/368705/1658496 

目录

1、Tengine编译安装

2、FPM制作Tengine为RPM包

3、总结

1、Tengine编译安装

[root@php ~]# cat /etc/issue
CentOS release 6.4 (Final)

[root@php ~]# uname -r
2.6.32-358.el6.x86_64

请确保系统安装了"Development tools"和”Server Platform Development”两个开发包组,如果没有安装请用yum工具安装即可。

所需要的软件包:

[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.bz2

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

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  #安装相应的包即可

2、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 --remove http://rubygems.org/ 
[root@php tengine]# gem sources -l
[root@php tengine]# gem clean

安装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
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
# 软件卸载后各个目录都已被成功删除了。

3、总结

    FPM非常易用,有了此工具再也不用担心rpm包的制作,在用此命令时可以把rpm包的安装、卸载做得更加优雅,在安装前可以做一些准备工作,安装后可以 做一些收尾工作,在卸载前也可以做一些准备,比如检测一下相应的服务是否停止了,在卸载软件再做一些扫尾的工作,只要把这些定义成一个个脚本,fpm中指 定相应的选项即可轻松实现。

fpm一些常用参数如下(用fpm –help可以才看所有参数详解)

-n 生成的package名字
-p 生成的package文件输出位置
- v 生成的package版本
-d 生成的package依赖于什么软件,通常为-d 'name' 或这 -d 'name > version'
-a 系统架构名称,如果是noarch则为 '-a all' 或者 '-a native'
--description 软件包描述
--conflicts 与其他什么软件冲突
-C 打包的根路径,后面跟要打入rpm包文件的路径
--pre- install     rpm包安装前执行的脚本
--post- install     rpm包安装后执行的脚本
--pre-uninstall     rpm包卸载前执行的脚本
--post-uninstall     rpm包卸载后执行的脚本


把rpm包放到yum源中,先更新中心yum仓库的元数据,然后再更新需要安装该rpm包机器的yum元数据。

#提供yum源服务的机器
createrepo --update /mirros/centos/6.3/x86_64/   更新repo数据
createrepo /mirros/centos/6.3/x86_64/     创建repo数据
#享受yum源服务的机器
yum clean all
yum makecache


参考文章:

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

http://flowerwrong.iteye.com/category/318219