设为首页 收藏本站
查看: 1567|回复: 0

[经验分享] FPM打包工具制作Tengine为RPM包详解

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-6-5 08:41:42 | 显示全部楼层 |阅读模式
目录
1、Tengine编译安装
2、FPM制作Tengine为RPM包
3、总结
1、Tengine编译安装

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



请确保系统安装了"Development tools"和”Server Platform Development”两个开发包组,如果没有安装请用yum工具安装即可。
所需要的软件包:
1
2
3
4
[iyunv@php tengine]# pwd
/root/software/tengine
[iyunv@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
解压各软件:
1
2
3
4
5
6
[iyunv@php tengine]# unzip -q pcre-8.33.zip
[iyunv@php tengine]# tar xf jemalloc-3.6.0.tar.bz2
[iyunv@php tengine]# tar xf tengine-2.1.0.tar.gz
[iyunv@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



安装两个依赖包:
1
2
3
4
5
6
7
[iyunv@php tengine]# cd pcre-8.33
[iyunv@php pcre-8.33]# ./configure --prefix=/usr/local/pcre8.33
[iyunv@php pcre-8.33]# make && make install
[iyunv@php openssl-1.0.2]# make && make install
[iyunv@php tengine]# cd jemalloc-3.6.0
[iyunv@php jemalloc-3.6.0]# ./configure --prefix=/usr/local/jemalloc
[iyunv@php jemalloc-3.6.0]# make && make install



上边的这pcre与jemalloc两个包可以不安装,因为在tengine编译时是引用的两个包的源文件,并不是安装后的目录。
tengine编译安装:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[iyunv@php jemalloc-3.6.0]# cd ../tengine-2.1.0
[iyunv@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在不指定编译进哪些模块时默认安装了哪些模块。



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
[iyunv@php tengine-2.1.0]# make
[iyunv@php tengine-2.1.0]# make install
[iyunv@php tengine-2.1.0]# /usr/local/tengine/sbin/nginx -v
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
[iyunv@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提供服务启动脚本:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
[iyunv@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
[iyunv@php tengine-2.1.0]# chmod +x /etc/rc.d/init.d/nginx



测试tengine的启动与停止:
1
2
3
4
[iyunv@php tengine-2.1.0]# service nginx start
Starting nginx:                                            [  OK  ]
[iyunv@php tengine-2.1.0]# service nginx stop
Stopping nginx:                                            [  OK  ]



在编译安装tengine时可能会遇到如下错误:
1
2
3
4
5
6
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



处理方法:
1
2
3
4
5
6
7
8
9
10
11
[iyunv@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
[iyunv@php tengine-2.1.0]# yum -y install perl-Test-Harness  #安装相应的包即可



2、FPM制作Tengine为RPM包
FPM是ruby的模块,先安装FPM依赖的包:
1
[iyunv@php tengine]# yum -y install ruby rubygems ruby-devel rpm-build



因国内网络环境,访问http://rubygems.org/站点时不稳定,所以增加国内toabao提供的一个镜像站点,所原来的站点移除:
1
2
[iyunv@php tengine]# gem sources -a http://ruby.taobao.org/
[iyunv@php tengine]# gem sources -r http://rubygems.org/



安装FPM:
1
[iyunv@php tengine]# gem install fpm



转换到上边编译tengine的工作目录,先做一些打包前的准备工作:
1
2
[iyunv@php tengine-2.1.0]# pwd
/root/software/tengine/tengine-2.1.0



创建一个安装tengine的临时目录,fpm可以从这个目录是读取信息:
[iyunv@php tengine-2.1.0]# mkdir /tmp/installdir   
把tengine安装到此临时目录中:
[iyunv@php tengine-2.1.0]# make install DESTDIR=/tmp/installdir/   
[iyunv@php tengine-2.1.0]# cd /tmp/installdir/
查看一下目录结构:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[iyunv@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
[iyunv@php installdir]# pwd
/tmp/installdir
[iyunv@php installdir]# ls
etc  usr  var



创建放置服务启动脚本的目录:
1
2
3
4
[iyunv@php installdir]# mkdir -pv etc/rc.d/init.d   
mkdir: created directory `etc/rc.d'
mkdir: created directory `etc/rc.d/init.d'
[iyunv@php installdir]# cp /etc/rc.d/init.d/nginx ./etc/rc.d/init.d  #把服务启动脚本拷贝过来



准备rpm安装后及卸载后所需要运行的脚本:
1
2
3
4
5
6
7
8
9
10
[iyunv@php installdir]# mkdir tmp;cd tmp  #这个目录是放置以下两个脚本的目录
[iyunv@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组和用户,如果没有就创建。



1
2
3
4
5
6
7
8
9
[iyunv@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包:
1
2
3
4
5
6
7
[iyunv@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"查看
[iyunv@php ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog  software  tengine-2.1.0-1.el6.x86_64.rpm



rpm文件生成后就可以拷贝到其他服务器进行测试:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[iyunv@php ~]# scp tengine-2.1.0-1.el6.x86_64.rpm 192.168.0.203:/root
[iyunv@nod3 ~]# rpm -ivh tengine-2.1.0-1.el6.x86_64.rpm
Preparing...                ########################################### [100%]
   1:tengine                ########################################### [100%]
[iyunv@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安装好后来验证各个目录及用户是否生成:
1
2
3
4
5
6
7
8
9
10
11
[iyunv@nod3 ~]# id nginx
uid=496(nginx) gid=496(nginx) groups=496(nginx)
[iyunv@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
[iyunv@nod3 ~]# ls /usr/local/tengine/
html  include  modules  sbin  scgi_temp
[iyunv@nod3 ~]# ls /etc/rc.d/init.d/nginx
/etc/rc.d/init.d/nginx



启动服务器来检测tengine是否能正常运行:
1
2
3
4
[iyunv@nod3 ~]# service nginx start
Starting nginx:                                            [  OK  ]
[iyunv@nod3 ~]# netstat -tnlp | grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      2257/nginx



再来测试一下卸载tengine后各个目录是否能删除:
1
2
3
4
5
6
7
[iyunv@nod3 ~]# rpm -e tengine
[iyunv@nod3 ~]# id nginx
id: nginx: No such user
[iyunv@nod3 ~]# ls /etc/ | grep tengine
[iyunv@nod3 ~]# ls /usr/local/ | grep tengine
[iyunv@nod3 ~]# ls /etc/rc.d/init.d/ | grep nginx
# 软件卸载后各个目录都已被成功删除了。



3、总结
    FPM非常易用,有了此工具再也不用担心rpm包的制作,在用此命令时可以把rpm包的安装、卸载做得更加优雅,在安装前可以做一些准备工作,安装后可以做一些收尾工作,在卸载前也可以做一些准备,比如检测一下相应的服务是否停止了,在卸载软件再做一些扫尾的工作,只要把这些定义成一个个脚本,fpm中指定相应的选项即可轻松实现。这里涉及的参数是:
--pre-install FILE:表示安装之前所要运行的脚本
--post-install FILE:表示安装之后所要运行的脚本
--pre-uninstall FILE:表示卸载之前所要运行的脚本
--post-uninstall FILE:表示卸载之后所要运行的脚本


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-74049-1-1.html 上篇帖子: linux 建立域环境nis 下篇帖子: Centos 6.4 安装scp和lrzsz 打包工具 制作
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表