利用rpmbuild打包定制属于你自己的RPM包

       这个是自己写在CSDN上写的第一个技术博客, 因为最近自己在研究rpm打包. 今天把它分享出来哈~~ rpm的优势就是安装和后期升级方便快捷,无需再次手动编译源代码。节省很多时间。. 这里已Nginx 服务器安装包为实例进行试验.

环境:
1. 系统:  CentOS release 6.5 (Final) x86_64
2.硬件:   KVM虚拟机, 双核CPU,2G内存,50G硬盘
3.user:     使用普通用户,这里登陆方式是: user+key
4.权限:     使用sudo 可使用root权限

前期准备:
1. nginx 官网下载最新压缩包:  wget   http://nginx.org/download/nginx-1.6.0.tar.gz
2. 安装 rpmbuild 编译必备工具(群组安装方式): yum groupinstall "Development Tools"

开始定制:
1.  建好工作目录(这里也可以不建,默认工具安装会自动建立)
[huzhong@s186 ~]$ mkdir rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}
[huzhong@s186 ~]$ cd rpmbuild/SPECS/
2. 开始编写定制包用的spec文件(文件内容如下)
[ huzhong@ s186  SPECS ]cat  nginx. spec
% define  _localstatedir  / home
% define  nginx_user  nginx
% define  nginx_group  nginx
# distribution specific definitions
% define  use_systemd  ( 0 % {? fedora }  &&  0 % {? fedora }  >=  18 )  ||  ( 0 % {? rhel }  &&  0 % {? rhel }  >=  7 )
% if  0 % {? rhel }   ==  5
GroupSystem  Environment / Daemons
Requires ( pre )shadow - utils
Requiresinitscripts  >=  8.36
Requires ( post )chkconfig
Requiresopenssl
BuildRequiresopenssl - devel
% endif
% if  0 % {? rhel }   ==  6
GroupSystem  Environment / Daemons
Requires ( pre )shadow - utils
Requiresinitscripts  >=  8.36
Requires ( post )chkconfig
Requiresopenssl  >=  1.0.1
BuildRequiresopenssl - devel  >=  1.0.1
% define  with_spdy  1
% endif
% if  0 % {? rhel }   ==  7
GroupSystem  Environment / Daemons
Requires ( pre )shadow - utils
Requiressystemd
Requiresopenssl  >=  1.0.1
BuildRequiressystemd
BuildRequiresopenssl - devel  >=  1.0.1
% define  with_spdy  1
% endif
% if  0 % {? suse_version }
GroupProductivity / Networking / Web / Servers
BuildRequireslibopenssl - devel
Requires ( pre )pwdutils
% endif
# end of distribution specific definitions
SummaryHigh  performance  web  server
Namenginx
Version1.6.0
Release1 % {? dist }. ngx
Vendornginx  inc.
URLhttp: // nginx. org /
Source0http: // nginx. org / download /% { name } -% { version }. tar. gz
Source1run. sh
Source2nginx. conf
License2 - clause  BSD - like  license
BuildRoot% { _tmppath } /% { name } -% { version } -% { release } - root
BuildRequireszlib - devel
BuildRequirespcre - devel
Provideswebserver
% description
nginx  [ engine  x ]  is  an  HTTP  and  reverse  proxy  serveras  well  as  a  mail  proxy  server.
configure  arguments
% prep
% setup  - q
% build
. / configure \
         -- prefix =% { _localstatedir } / nginx \
         -- conf - path =% { _localstatedir } / nginx / conf / nginx. conf \
         -- http - client - body - temp - path =% { _localstatedir } / nginx / cache / client_temp \
         -- http - proxy - temp - path =% { _localstatedir } / nginx / cache / proxy_temp \
         -- http - fastcgi - temp - path =% { _localstatedir } / nginx / cache / fastcgi_temp \
         -- http - uwsgi - temp - path =% { _localstatedir } / nginx / cache / uwsgi_temp \
         -- http - scgi - temp - path =% { _localstatedir } / nginx / cache / scgi_temp \
         -- user =% { nginx_user } \
         -- group =% { nginx_group } \
         -- with - http_ssl_module \
         -- with - http_realip_module \
         -- with - http_addition_module \
         -- with - http_sub_module \
         -- with - http_dav_module \
         -- with - http_flv_module \
         -- with - http_gunzip_module \
         -- with - http_gzip_static_module \
         -- with - http_random_index_module \
         -- with - http_secure_link_module \
         -- with - http_stub_status_module \
         -- with - http_auth_request_module \
         -- with - mail \
         -- with - mail_ssl_module \
         -- with - file - aio \
make  % {? _smp_mflags }
% install
% { __rm }  - rf  $RPM_BUILD_ROOT
% { __make }  DESTDIR = $RPM_BUILD_ROOT  install
% { __mkdir }  - p  $RPM_BUILD_ROOT % { _localstatedir } / nginx / cache
% { __mkdir }  - p  $RPM_BUILD_ROOT % { _localstatedir } / nginx / conf / conf. d
% { __install }  - m  755  - p  % { SOURCE1 } \
     $RPM_BUILD_ROOT % { _localstatedir } / nginx / run. sh
% { __install }  - m  644  - p  % { SOURCE2 } \
     $RPM_BUILD_ROOT % { _localstatedir } / nginx / conf / nginx. conf
% { __mkdir }  - p  $RPM_BUILD_ROOT % { _localstatedir } / nginx / cache
% clean
% { __rm }  - rf  $RPM_BUILD_ROOT
% pre
# Add the "nginx" user
/ usr / bin / id  - g  nginx  >/ dev / null  2 >& 1  ||  / usr / sbin / groupadd  nginx
/ usr / bin / id  - u  nginx  >/ dev / null  2 >& 1  ||  / usr / sbin / useradd  - M  - g  nginx  nginx
if  [  - r  / home / nginx / sbin / nginx  ] ; then
     / bin / cp  - f  / home / nginx / sbin / nginx {,. old }  >/ dev / null  2 >& 1  || :
fi
% post
# print site info
     cat  << BANNER
----------------------------------------------------------------------
Thanks  for  using  nginx!
Please  find  the  official  documentation  for  nginx  here:
*  http: // nginx. org / en / docs /
Commercial  subscriptions  for  nginx  are  available  on:
*  http: // nginx. com / products /
----------------------------------------------------------------------
BANNER
% preun
if  [  "$1"  =  "0"  ] ; then
     / home / nginx / run. sh  stop   >/ dev / null  2 >& 1  || :
fi
% postun
#/home/nginx/run.sh softupdate  >/dev/null 2>&1 || :
% files
% defattr ( -, nginx, nginx )
% config ( noreplace )  / home / nginx / conf / nginx. conf
/ home / nginx / conf / fastcgi_params. default
/ home / nginx / conf / mime. types
/ home / nginx / conf / nginx. conf. default
/ home / nginx / html /50 x. html
/ home / nginx / html / index. html
/ home / nginx / sbin / nginx
/ home / nginx / logs
/ home / nginx / run. sh
% exclude  / home / nginx / conf / koi - utf
% exclude  / home / nginx / conf / koi - win
% exclude  / home / nginx / conf / win - utf
% exclude  / home / nginx / conf / fastcgi_params
% exclude  / home / nginx / conf / mime. types. default
% exclude  / home / nginx / conf / fastcgi. conf
% exclude  / home / nginx / conf / fastcgi. conf. default
/ home / nginx / conf / scgi_params
/ home / nginx / conf / scgi_params. default
/ home / nginx / conf / uwsgi_params
/ home / nginx / conf / uwsgi_params. default
% changelog
*  Thu  Apr  28  2014  Viman  < zhong. hu@ baoyugame. com >
-  1.6.0.1
*  Thu  Apr  24  2014  Konstantin  Pavlov  < thresh@ nginx. com >
-  1.6.0
 
3. 定制默认配置文件(这里根据你自己的需求来定制) nginx.conf
   这里源代码就不贴出来了,你也可以用nginx官方的nginx.conf

4. 开始编译rpm包和srpm 源码包
 
[ huzhong@ s186  SPECS ]rpmbuild  - ba  nginx. spec
 
5.  编译成功后系统返回值应该是0 (echo $?  打印查看), 编译好的rpm包,放置在 ~/rpmbuild/RPMS/x86_64/  目录下 (这里系统会根据你是i386 还是x86_64 位系统来建立不同的包和目录)
 详情:
 
[ huzhong@ s186  ~ ] $  ls  rpmbuild / RPMS / x86_64 /
nginx -1.6 . 0-1. el6 . ngx . x86_64 . rpm   nginx - debuginfo -1.6 . 0-1. el6 . ngx . x86_64 . rpm
 
6.  安装rpm包(我这里已经安装成功了,所以加--test参数测试下)
[ huzhong@ s186  x86_64 ]sudo  rpm  - ivh  nginx - 1.6.0 - 1. el6. ngx. x86_64. rpm  -- test
Preparing...                 ########################################### [100%]
     package  nginx - 1.6.0 - 1. el6. ngx. x86_64  is  already  installed
 
7. 最好来一张Nginx目录架构图
利用rpmbuild打包定制属于你自己的RPM包_第1张图片

备注:
1.  nginx.spec 文件 前面我定制了 
_localstatedir 变量,方便以后修改和目录移植
2.  nginx 所有安装目录在/home/nginx  下. 这么做的优点就是资源统一管理备份更方便.  (官方的包安装是根据linux内核系统来的)
3.  %define use_systemd 这里还可以根据不同系统来自动匹配安装(未完成,可以根据需求定制)

4.  在最好一步编译的时候,如出现报错,可根据系统提示来修改。

你可能感兴趣的:(利用rpmbuild打包定制属于你自己的RPM包)