天天看點

rpm軟體包制作

簡單制作RPM二進包執行個體

有好多朋友問到怎麼制作rpm包,可不可把其它伺服器上編譯好的軟體目錄複雜到其它伺服器上直接應用等等。這裡通過案例由簡單到進階來一一講解。

此方法是通過編寫spec檔案,使用rpmbuild來完成一個rpm的打包。

分别以libmad、nginx、apache為例進行介紹

制作平台:CentOS 6.x X86_64

實施思路:1、準備必須的軟體。

          2、建立rpmbuild目錄結構,将源碼包放入指定目錄。

  3、在指定的目錄中編寫*.spec配置檔案。

  4、根據*.spec配置檔案生成rpm軟體包。

四步走:

準備工作:安裝軟體

安裝rpm制作工作:yum -y install gcc  gcc-c++  rpm* rpm-build  rpmdev*

安裝nginx依賴的包:yum install -y  openssl openssl-devel  pcre  pcre-devel  zlib  zlib-devel

第一步:建立目錄結構

centos 6.x目錄:mkdir -pv ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}

相關目錄介紹:

--檢視rpmbuild環境參數:rpmbuild  --showrc | grep  _topdir

~/rpmbuild/SOURCES    #存放源代碼、更新檔等檔案 

~/rpmbuild/SPECS      #存放用于管理rpm制作程序的spec檔案 

~/rpmbuild/BUILD      #解壓後的檔案存放目錄 

~/rpmbuild/RPMS       #存放由rpmbuild制作好的二進制包 

~/rpmbuild/SRPMS      #存放由rpmbuild制作好的源碼包 

修改rpmbuild環境參數:用下面的指令生成rpmbuild所需要的宏檔案,這個檔案裡包含的是.spec中要引用的相對路徑。檔案裡的内容可以手動配置和編寫,格式符合要求即可。

echo '%_topdir  %(echo $HOME)/rpmbuild' > ~/.rpmmacros

##需求:建立libmad-0.15.1b.tar.gz源碼包的rpm軟體包。(已測試OK金)

第二步:把源碼包放在SOURCES目錄下

cd  ~/rpmbuild/SOURCES

wget  http://downloads.sourceforge.net/mad/libmad-0.15.1b.tar.gz

第三步:生成nginx.spec檔案

cd  ~/rpmbuild/SPECS 

rpmdev-newspec -o libmad-0.15.1b.spec     //生成模闆檔案

vi   libmad-0.15.1b.spec     //修改模闆檔案的完整代碼

#以下依次為軟體名、版本号、系統版本、功能描述。

Name:           libmad

Version:        0.15.1b

Release:        1%{?dist}

Summary:        This is a free MP3 Codec.

#以下依次是分組資訊、協定、軟體網址、軟體包名變量、制作的根目錄環境。

Group:          System Environment/Libraries

License:        GPL

URL:            http://downloads.net/libmad.tar.gz

Source0:        %{name}-%{version}.tar.gz

BuildRoot:      %{_topdir}/BUILDROOT

#必備的軟體包、依賴的軟體包。

BuildRequires:  gcc,gcc-c++

Requires:       gcc,gcc-c++

#軟體的基本描述資訊。

%description

#軟體包說明

%package        devel

Summary:        Development files for %{name}

Group:          Development/Libraries

Requires:       %{name}-%{version}-%{release}

#軟體devel開發的描述資訊

%description    devel

The %{name}-devel package contains libraries and header files for

developing applications that use %{name}.

#安裝或者更新軟體前要做的事情,比如停止服務、備份相關檔案等都在這裡做。

%prep

%setup -q

#編譯階段的參數設定

%build

sed -i '/-fforce-mem/d' configure

%configure --enable-shared

make %{?_smp_mflags}

#安裝階段的參數設定。

%install

rm -rf $RPM_BUILD_ROOT

make install DESTDIR=$RPM_BUILD_ROOT

#清除階段的參數設定。

%clean

#檔案和目錄參數設定。

%files

%defattr(-,root,root,-)

%doc

%{_libdir}/*

%{_includedir}/*

#

%changelog

*  Thu Dec 01 2016 flyer <[email protected]> - libmad-0.15.1b.1.el6

- Initial version

第四步:生成rpm軟體包。

rpmbuild  -bb   libmad-0.15.1b.spec    //生成rpm軟體包,需要待編譯安裝過程。

ls  /root/rpmbuild/RPMS/x86_64/    //檢視生成的rpm軟體包。

第五步:測試rpm軟體包的安裝和查詢。

安裝:rpm -ivh /root/rpmbuild/RPMS/x86_64/libmad-0.15.1b-1.el6.x86_64.rpm

查是否已安裝:rpm  -q  libmad

查檔案:rpm  -ql  libmad

解除安裝:rpm  -e  libmad

------------------------------------

##以下是關于*.spec檔案格式解釋:

Name: test

Version:

Requires:

#==================================SPEC頭部====================================

%patch <==== 在這裡打包

%configure

要打包到rpm包裡的檔案清單

#==================================SPEC主體====================================

%pre

安裝或者更新軟體前要做的事情,比如停止服務、備份相關檔案等都在這裡做。

%post

安裝或者更新完成後要做的事情,比如執行ldconfig重構動态庫緩存、啟動服務等。

%preun

解除安裝軟體前要做的事情,比如停止相關服務、關閉程序等。

%postun

解除安裝軟體之後要做的事情,比如删除備份、配置檔案等。

-------------------------------------------

#需求:将nginx-1.2.0.tar.gz生成一個基礎版本的rpm包。

說明:此版本的rpm包安裝後,并無service啟動腳本,需要手工建立。需要手工啟動nginx服務。

準備工作:

useradd  -r  nginx

wget http://nginx.org/download/nginx-1.2.0.tar.gz

rpmdev-newspec -o nginx-1.2.0.spec

vi  nginx-1.2.0.spec   //内容如下(測試OK金,直接複制代碼)

Name:   nginx

Version:    1.2.0

Release:    1%{?dist}

Summary:    nginx_install

Group:  System Environment/Daemons 

License:    GPLv2

URL:    http://nginx.org/download

Source0:    %{name}-%{version}.tar.gz

Source1:    nginx_init

Source2:    nginx.conf

BuildRoot:  %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX)

BuildRequires:  gcc,gcc-c++,pcre-devel,zlib-devel,make,openssl-devel

Requires:   pcre-devel,zlib-devel,openssl-devel

#軟體包詳述

Build nginx-1.2.0.tar.gz to nginx-1.2.0.rpm

#建構包前的處理

#靜默模式解壓并cd

#生成:這裡主要是建構二進制包的的時候執行編譯生成二進制檔案

%build export DESTDIR=%{buildroot}

./configure --user=nginx \

--group=nginx \

--prefix=/usr/local/nginx \

--sbin-path=/usr/sbin/nginx \

--conf-path=/etc/nginx/nginx.conf \

--error-log-path=/var/log/nginx/error.log \

--http-log-path=/var/log/nginx/access.log \

--pid-path=/var/run/nginx/nginx.pid  \

--lock-path=/var/lock/nginx.lock \

--with-http_ssl_module \

--with-http_flv_module \

--with-http_stub_status_module \

--with-http_gzip_static_module \

--http-client-body-temp-path=/var/tmp/nginx/client/ \

--http-proxy-temp-path=/var/tmp/nginx/proxy/ \

--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \

--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \

--http-scgi-temp-path=/var/tmp/nginx/scgi \

--with-pcre 

# 多處理器的話,可以加 -j 參數

#建構的時候把目前檔案安裝到系統目錄$RPM_BUILD_ROOT/下,二進制安裝的時候是安裝檔案到/根目錄下    

rm -rf %{buildroot}

make install DESTDIR=%{buildroot}

#%%{__install}這個宏代表install指令

#%%{__install} -p -D -m 0755 %{SOURCE1} %{buildroot}/etc/rc.d/init.d/nginx

#%%{__install} -p -D %{SOURCE2} %{buildroot}/etc/nginx/nginx.conf

#rpm安裝前執行的腳本

#$1有3個值,代表動作,安裝類型,處理類型 1:表示安裝 2:表示更新 0:表示解除安裝

if [ $1 == 1 ];then

      /usr/sbin/useradd  -r nginx 2> /dev/null

fi

#rpm安裝後執行的腳本

    #/sbin/chkconfig --add nginx

    echo  -e  '#!/bin/sh\nPATH=$PATH:/usr/local/nginx/bin' > /etc/profile.d/nginx.sh

    source  /etc/profile.d/nginx.sh

    /sbin/install-info %{_infodir}/%{name}.info %{_infodir}/dir 2> /dev/null || :

    mkdir -p /var/log/nginx/  /var/tmp/nginx/client/

    touch /var/log/nginx/error.log /var/log/nginx/access.log

    mkdir -p /var/run/nginx/

    touch /var/run/nginx/nginx.pid

#rpm解除安裝前執行的腳本

if [ $1 == 0 ];then

    nginx  -s  stop  &>  /dev/null

    /usr/sbin/groupdel nginx 2> /dev/null

    /usr/sbin/userdel -r nginx 2> /dev/null

    #/sbin/chkconfig --del nginx 2> /dev/null

    /usr/local/nginx/bin/nginx  -s stop > /dev/null 2>&1

    /sbin/install-info --delete %{_infodir}/%{name}.info %{_infodir}/dir 2> /dev/null || :

#rpm解除安裝後執行的腳本

#清理段,clean的主要作用就是删除BUILD

#檔案清單段,這個階段是把前面已經編譯好的内容要打包了,其中exclude是指要排除什麼不打包進來

#%%defattr (-,root,root) 指定包裝檔案的屬性,分别是(mode,owner,group),-表示預設值,對文本檔案是0644,可執行檔案是0755

/etc/nginx/

#/usr/html/index.html

#/usr/html/50x.html

/usr/local/nginx/html/50x.html

/usr/local/nginx/html/index.html

/usr/sbin/nginx

#變更日志

*  Thu Dec 01 2016 flyer <[email protected]> -nginx-1.2.0-1.el6

第四步:RPM包制作

首先系統要安裝好必備的制作工具:gcc、rpmbuild等

yum -y install gcc rpm-build rpmdev*  rpm* 

cd ~/rpmbuild/SPECS/ 

rpmbuild  -bb  nginx-1.2.0.spec 

通過上面這條指令,會在~/rpmbuild/RPMS/x86_64/下面生成nginx-1.2.0-1.el6.x86_64.rpm這個檔案

-bb 這個選項就是制作二進制包(build binary package only from <specfile>)

第五步:用rpm安裝nginx測試。

安裝:rpm -ivh /root/rpmbuild/RPMS/x86_64/nginx-1.2.0-1.el6.x86_64.rpm

查詢:rpm  -q  nginx

啟動服務:nginx

停止服務:nginx  -s  stop

測試通路:curl  127.0.0.1

檢視端口:lsof  -i:80  或  netstart  -tunlp|grep  :80  或  ss  -tunlp|grep  :80

對spec檔案内容進行簡單說明:

spec檔案是制作rpm包的核心!

以#開頭的是注釋資訊; 

Summary:對相關軟體進行簡單描述說明 

Name:定義rpm包的名稱。例:nginx-1.2.0.tar.gz名稱用nginx 

Version:定義軟體的版本号。例: nginx-1.2.0.tar.gz版本用1.2.0

Release:發行版本。例:rhel6.5則用11.el6。 

License:定義許可證

Group:說明軟體屬于哪種應用類型 

Source:軟體源碼下載下傳位址 

URL:軟體相關官方站點 

Distribution: 發行版系列 

Packager: 制作人的簡單資訊 

%description:軟體較長的描述資訊 

%prep:軟體編譯之前的處理 

%build:編譯軟體

%pre:安裝時處理

%install:安裝軟體

%post:安裝後處理

%preun:定義解除安裝之前的動作 

%files:指定要打包的軟體包,這裡是/usr/local/nginx

對于更詳細的說明請參考官方資料:http://www.rpm.org/max-rpm/ch-rpm-inside.html

下面是apache的spec檔案執行個體:

yum  remove  -y  httpd

rpmdev-newspec  -o  httpd-2.2.22.spec

vi   httpd-2.2.22.spec    //參考代碼如下(測試OK金,httpd-2.2.22.tar.gz源碼包可直接複制下面的代碼,100%成功生成rpm包):

Name:           httpd

Version:        2.2.22

Summary:       this is a web server httpd

Group:          app/server

URL:            http://www.apache.org

BuildRequires:  gcc,gcc-c++,apr,apr-util,pcre,pcre-devel,openssl,openssl-devel,zlib,zlib-devel

Requires:       gcc,gcc-c++,apr,apr-util,pcre,pcre-devel,openssl,openssl-devel,zlib,zlib-devel

#description是指定軟體的描述資訊。

httpd is apache  web soft.

#prep是軟體編譯之前的處理,setup -q是靜默解壓tar包,并cd進入解壓後的目錄。

#build是指定編譯時要執行的指令。

./configure --prefix=/usr/local/apache2  --sysconfdir=/etc/apache2/conf --enable-so --enable-deflate --enable-headers --enable-mods-shared=all --enable-rewrite

#install是指定安裝時要執行的指令。

#pre是指安裝前執行指令,$ 1是參數,值為1時是安裝,值為0時是解除安裝,值為2時是更新安裝。

   useradd  -r  apache

#preun是指解除安裝時要執行的指令。

        /usr/sbin/userdel -r  apache  2> /dev/null

rm  -rf  /etc/apache2  /usr/local/apache2

#post是指定安裝後執行的指令。

        echo -e '#!/bin/sh\nPATH=$PATH:/usr/local/apache2/bin' > /etc/profile.d/apache.sh

        source  /etc/profile.d/apache.sh

        cp -pv  /usr/local/apache2/bin/apachectl  /etc/init.d/apache

        sed  -i  '2a\#chkconfig: - 83 15' /etc/init.d/apache

        /sbin/chkconfig --add apache

        /sbin/chkconfig  apache  on

sed  -i  's/^#ServerName/ServerName/'  /etc/apache2/conf/httpd.conf

#clean是清除垃圾檔案。

#files是指定安裝時要建立的目錄及權限。

/usr/local/apache2

/etc/apache2/conf

*  Thu Dec 01 2016 flyer <[email protected]> - httpd-2.2.22-3.el6.x86_64

rpmbuild  -bb  httpd-2.2.22.spec 

通過上面這條指令,會在~/rpmbuild/RPMS/x86_64/下面生成httpd-2.2.22-1.el6.x86_64.rpm這個檔案

安裝:rpm -ivh /root/rpmbuild/RPMS/x86_64/httpd-2.2.22-1.el6.x86_64.rpm

--------------------------------

以下是有錯誤的代碼,需要修複

# spec file for apache 

# Build 2017-07-17 

# By flyer 

Summary:    High stability web server 

Name:       httpd 

Version:    2.2.22

Release:    3.el6

License:    2-clause BSD-like license 

Group:      Applications/Server 

Source: http://apache.etoak.com/httpd/httpd-2.2.22.tar.gz 

URL: http://apache.org 

Distribution: Centos/Redhat 

Packager: flyer <[email protected]> 

%description 

Apache is a first web server 

%prep 

echo  "PATH=$PATH:/usr/local/apache2/bin" > /etc/profile.d/apache.sh

source  /etc/profile.d/apache.sh

        /usr/sbin/useradd -r apache 2> /dev/null

fi           

#id  apache &> /dev/null  && echo 'nginx user ok' ||  /usr/sbin/useradd -r apache &> /dev/null

%preun 

if [ -z "`ps aux | grep httpd | grep -v grep`" ];then 

pkill httpd >/dev/null 

fi 

        /usr/sbin/userdel -r apache2 2> /dev/null

echo  'ServerName  www.example.com:80'  >> /etc/apache2/conf/httpd.conf

cp  -pv  /usr/local/apache2/bin/apachectl  /etc/init.d/apache

sed  -i  '2a\#chkconfig: - 83  15'  /etc/init.d/apache

chkconfig  apache  on

chkconfig  apache  --list

#rm -rf $RPM_BUILD_ROOT

 以後對于相同或類似平台可以到其它伺服器上進行rpm安裝部署。

另外還有一種rpm打包的方法:rpm_create

這是一種新的打rpm的工具,不用spec語言,隻需要會簡單的shell指令,即可完成打包操作,非常友善,結合了spec語言和checkinstall,相比spec方法要簡單很多!

官方站點:http://code.google.com/p/rpmcreate/

下載下傳站點:wget http://rpmcreate.googlecode.com/files/rpm_create-1.7.5-9.x86_64.rpm

大家可以去官方站點參考!

--------------------------------------

#需求:将nginx-1.2.0.tar.gz生成一個進階完整優化版本的rpm包。(已測試OK金)

說明:此版本的rpm包安裝後,會自動生成service啟動腳本,能正常啟動。

第2.2步:建立fastcgi配置檔案

#cd  ~/rpmbuild/SOURCES

#vi  fastcgi_params   //配置代碼如下

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;

fastcgi_param  SERVER_SOFTWARE    nginx;

fastcgi_param  QUERY_STRING       $query_string;

fastcgi_param  REQUEST_METHOD     $request_method;

fastcgi_param  CONTENT_TYPE       $content_type;

fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;

fastcgi_param  REQUEST_URI        $request_uri;

fastcgi_param  DOCUMENT_URI       $document_uri;

fastcgi_param  DOCUMENT_ROOT      $document_root;

fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  REMOTE_ADDR        $remote_addr;

fastcgi_param  REMOTE_PORT        $remote_port;

fastcgi_param  SERVER_ADDR        $server_addr;

fastcgi_param  SERVER_PORT        $server_port;

fastcgi_param  SERVER_NAME        $server_name;

第2.3步:建立init.nginx啟動腳本

vi  init.nginx   //啟動腳本代碼如下:

#!/bin/sh

# nginx - this script starts and stops the nginx daemon

# chkconfig:   - 85 15

# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \

#               proxy and IMAP/POP3 proxy server

# processname: nginx

## config:      /etc/nginx/nginx.conf

# config:      /usr/local/nginx/conf/nginx.conf

# config:      /etc/sysconfig/nginx

## pidfile:     /var/run/nginx/nginx.pid

# pidfile:     /usr/local/nginx/logs/nginx.pid

# 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/nginx/sbin/nginx"

prog=$(basename $nginx)

#NGINX_CONF_FILE="/etc/nginx/nginx.conf"

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

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_CONF_FILE ] || exit 6

    make_dirs

    echo -n $"Starting $prog: "

    daemon $nginx -c $NGINX_CONF_FILE

    retval=$?

    echo

    [ $retval -eq 0 ] && touch $lockfile

    return $retval

stop() {

    echo -n $"Stopping $prog: "

    killproc $prog -QUIT

    [ $retval -eq 0 ] && rm -f $lockfile

restart() {

    configtest || return $?

    stop

    sleep 1

    start

reload() {

    echo -n $"Reloading $prog: "

    killproc $nginx -HUP

    RETVAL=$?

force_reload() {

    restart

configtest() {

  $nginx -t -c $NGINX_CONF_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

    restart|configtest)

    reload)

        rh_status_q || exit 7

    force-reload)

        force_reload

    status)

        rh_status

    condrestart|try-restart)

            ;;

    *)

        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"

        exit 2

esac

##腳本init.nginx編寫完畢,繼續下一步。

第三步:生成nginx.spec檔案。

vi nginx-1.2.0.spec   //内容如下

### 1.The introduction section

#以下依次為軟體名、版本号、系統版本、功能描述。用rpm  -qi  nginx檢視。

Name: nginx

Version: 1.2.0

Release: 3%{?dist}

Summary: nginx-1.2.0.tar.gz to nginx-1.2.0.rpm

Group: Applications/Archiving

License: GPLv2

URL: http://rshare.ys168.com/

Packager: flyer <[email protected]>

Vendor: flyer make

Source0: %{name}-%{version}.tar.gz

Source1: init.nginx

Source2: nginx.conf

Source3: fastcgi_params

BuildRoot: %_topdir/BUILDROOT

BuildRequires: gcc,gcc-c++

Requires: openssl,openssl-devel,pcre-devel,pcre,zlib,zlib-devel

#軟體的描述資訊。

Custom a rpm by yourself!Build nginx-1.2.0.tar.gz to nginx-1.2.0.rpm

###  2.The Prep section 準備階段,主要就是把源碼包解壓到build目錄下,設定一下環境變量,并cd進去

# 這個宏的作用靜默模式解壓并cd進入目錄。

###  3.The Build Section 編譯制作階段,這一節主要用于編譯源碼

#生成配置、編譯、安裝,nginx不允許直接用%  configure進行編譯安裝,否則會報錯。

./configure \

--user=nginx \

--with-pcre

###  4.Install section  這一節主要用于完成實際安裝軟體必須執行的指令,可包含4種類型腳本

%{__install} -p -D -m 0755 %{SOURCE1} %{buildroot}/etc/rc.d/init.d/nginx

%{__install} -p -D %{SOURCE3} %{buildroot}/usr/local/nginx/conf/fastcgi_params

#%{__install} -p -D %{SOURCE2} %{buildroot}/usr/local/nginx/conf/nginx.conf

# 下面的$1有3個值,代表動作. 1:表示安裝。2:表示更新。0:表示解除安裝。

mkdir  -pv  /var/tmp/nginx/client

        /usr/sbin/useradd -r nginx 2> /dev/null

#id  nginx &> /dev/null  && echo 'nginx user ok' ||  /usr/sbin/useradd -r nginx &> /dev/null

#安裝之後要執行的指令。

        /sbin/chkconfig --add %{name}

        /sbin/chkconfig %{name} on

        echo '

net.ipv4.tcp_max_syn_backlog = 65536

net.core.netdev_max_backlog =  32768

net.core.somaxconn = 32768

net.core.wmem_default = 8388608

net.core.rmem_default = 8388608

net.core.rmem_max = 16777216

net.core.wmem_max = 16777216

net.ipv4.tcp_timestamps = 0

net.ipv4.tcp_synack_retries = 2

net.ipv4.tcp_syn_retries = 2

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000927000000

net.ipv4.tcp_max_orphans = 3276800

#net.ipv4.tcp_fin_timeout = 30

#net.ipv4.tcp_keepalive_time = 120

net.ipv4.ip_local_port_range = 1024  65535' > /etc/sysctl.conf

sysctl -p 2>&1 /dev/null

#解除安裝軟體前要做的事情,比如停止相關服務、關閉程序等。

        /etc/init.d/nginx stop > /dev/null 2>&1

        /usr/sbin/userdel -r nginx 2> /dev/null

###  5.clean section 清理段,clean的主要作用就是删除BUILD

#清除垃圾檔案。

###  6.file section 檔案清單段,這個階段是把前面已經編譯好的内容要打包了,其中exclude是指要排除什麼不打包進來。

#檔案權限設定。

%defattr(-,root,root,0755)

/usr/local/nginx/

/usr/local/nginx/conf/nginx.conf

%attr(0755,root,root) /etc/rc.d/init.d/nginx

%config(noreplace) /usr/local/nginx/conf/fastcgi_params

#%config(noreplace) /usr/local/nginx/conf/nginx.conf

###  7.chagelog section  日志改變段, 這一段主要描述軟體的開發記錄.

*  Thu Dec 01 2016 flyer <[email protected]> - nginx-1.2.0-3.el6.x86_64

rpmbuild -bb nginx.spec 

###附加功能學習:

一、分段執行制作rpm包

rpmbuild -bp nginx.spec 制作到%prep段

rpmbuild -bc nginx.spec 制作到%build段

rpmbuild -bi nginx.spec 執行 spec 檔案的 "%install" 階段 (在執行了 %prep 和 %build 階段之後)。這通常等價于執行了一次 "make install"

rpmbuild -bb nginx.spec 制作二進制包

rpmbuild -ba nginx.spec 表示既制作二進制包又制作src格式包

二、rpm包的簽名

1、查詢軟體包資訊

[root@localhost ~]# rpm -qi nginx

Name        : nginx                        Relocations: (not relocatable)

Version     : 1.7.7                             Vendor: nmshuishui

Release     : 3.el6                         Build Date: Wed 26 Nov 2014 06:39:00 PM CST

Install Date: Wed 26 Nov 2014 06:42:19 PM CST      Build Host: localhost

Group       : Applications/Archiving        Source RPM: nginx-1.7.7-3.el6.src.rpm

Size        : 793593                           License: GPLv2

Signature   : (none)    # rpm包未簽名狀态

Packager    : nmshuishui <[email protected]>

URL         : http://nmshuishui.blog.51cto.com/

Summary     : nginx-1.7.7.tar.gz to nginx-1.7.7.rpm

Description :

Custom a rpm by yourself!Build nginx-1.7.7.tar.gz to nginx-1.7.7.rpm

2、使用gpg方式生成簽名密鑰

[root@localhost ~]# gpg --gen-key

Your selection?1<Enter>  #預設即可

What keysize do you want? (2048) 1024<Enter>  #選擇密鑰長度

Key is valid for? (0) 1y<Enter>  #有效期

Is this correct? (y/N) y<Enter>  #确認

Real name: nmshuishui<Enter>  #密鑰名稱

Email address: [email protected]<Enter>  #郵件

Comment: GPG-RPM-KEY<Enter>  #備注

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O<ENTER> #okay确認

Enter passphrase  OK <Enter>  #按Enter輸入密碼                    

<Take this one anyway> <Enter> #确認使用此密碼

#####

在生成密鑰的時候,會報這麼一個資訊:can't connect to `/root/.gnupg/S.gpg-agent': No such file or directory,可以不用理會它。

接下來就是一些随機數的說明了:We need to generate a lot of random bytes. It is a good idea to perform

some other action (type on the keyboard, move the mouse, utilize the

disks) during the prime generation; this gives the random number

generator a better chance to gain enough entropy.

就狂敲鍵盤和移動滑鼠吧,也可以連結一個僞随機數(不過不安全),接下來的活兒就是等了

生成密鑰後會是這樣的:

gpg: checking the trustdb

gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model

gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u

pub   2048R/DF63EDFB 2014-11-26

      Key fingerprint = 338D 476F 29C9 E2D6 6604  1D96 6F73 1E81 DF63 EDFB

uid                  nmshuishui (gen-key) <[email protected]>

sub   2048R/263FB359 2014-11-26

3、檢視生成的密鑰

[root@localhost ~]# gpg --list-keys

/root/.gnupg/pubring.gpg

------------------------

4、導出公鑰以供驗證

[root@localhost ~]# gpg --export -a "nmshuishui" > RPM-GPG-KEY-nmshuishui

5、在~/.rpmmacros宏中定義加密密鑰

[root@localhost ~]# vim ~/.rpmmacros

%_gpg_name nmshuishui

6、為rpm包簽名

[root@localhost ~]# rpm --addsign /home/hero/rpmbuild/RPMS/x86_64/nginx-1.7.7-3.el6.x86_64.rpm 

Enter pass phrase: 

Pass phrase is good.

/home/hero/rpmbuild/RPMS/x86_64/nginx-1.7.7-3.el6.x86_64.rpm:

7、将公鑰導入rpm包

[root@localhost ~]# rpm --import RPM-GPG-KEY-nmshuishui

8、驗證

[root@localhost ~]# rpm --checksig /home/hero/rpmbuild/RPMS/x86_64/nginx-1.7.7-3.el6.x86_64.rpm

/home/hero/rpmbuild/RPMS/x86_64/nginx-1.7.7-3.el6.x86_64.rpm: rsa sha1 (md5) pgp md5 OK

9、重新安裝nginx,驗證安裝包的簽名資訊

[root@localhost ~]# rpm -ivh /home/hero/rpmbuild/RPMS/x86_64/nginx-1.7.7-3.el6.x86_64.rpm 

Preparing...                ########################################### [100%]

   1:nginx                  ########################################### [100%]

[root@localhost ~]# 

Install Date: Thu 27 Nov 2014 10:58:44 AM CST      Build Host: localhost

Signature   : RSA/SHA1, Thu 27 Nov 2014 10:40:02 AM CST, Key ID 6f731e81df63edfb   # 與 1 比起來,多了簽名資訊

##到這裡,一個完整的 rpm 包就制作完成了!

說明:以下内容隻用于學習參數,不用分享到筆記文檔中。檔案中有些錯誤未修複,不能正常使用。

系統環境:CentOS release 6.5

目标:用軟體的tar源碼包生成rpm軟體包。

安裝rpm制作工作:yum -y install gcc rpm-build pcre-devel 

安裝nginx依賴的包:yum install -y openssl-devel  pcre  pcre-devel  zlib

用下面的指令生成rpmbuild所需要的宏檔案,這個檔案裡包含的是.spec中要引用的相對路徑。檔案裡的内容可以手動配置和編寫,格式符合要求即可。

echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros

-------------------------------

##需求1:制作asterisk的rpm軟體包。

centos 6.x目錄:mkdir -pv ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS,BUILDROOT}

第三步:建立制作rpm包配置檔案。

# vim ~/rpmbuild/SPECS/nginx.spec    //代碼内容如下:

Name:          nginx

Version:       1.2.0

Summary:        Design by flyer

Group:          Applications/Internet

URL:            rshare.ys168.com

Source0:        nginx-1.2.0.tar.gz

BuildRoot: %_topdir/rpmbuild 

Design by flyer

cd $RPM_BUILD_DIR

tar xzvf ../SOURCES/nginx-1.2.0.tar.gz

cd $RPM_BUILD_DIR/nginx-1.2.0

./configure  --prefix=/usr/local/nginxt

make

make install

%{_install} -p -D -m 0755 %{_buildrootdir}/%{name}-%{version}-%{release}.x86_64

#%{_install} -p -D -m 0755 %{SOURCE1} %{buildroot}/etc/rc.d/init.d/nginx

/usr/local/nginxt

rm -rf $RPM_BUILD_DIR/nginx-1.2.0

第四步:生成rpm包:

cd  ~/rpmbuild/SPECS/

rpmbuild  -bb  nginx.spec

##需求2:制作nginx的rpm軟體包。

# rpmdev-setuptree

檢視所有的宏

# rpmbuild –showrc

目錄結構

--BUILD #編譯之前,如解壓包後存放的路徑

--BUILDROOT #編譯後存放的路徑

--RPMS #打包完成後rpm包存放的路徑

--SOURCES #源包所放置的路徑

--SPECS #spec文檔放置的路徑

--SPRMS #源碼rpm包放置的路徑自動配置指令建立一個RPM建構根目錄結構,并配置一些必要操作。

rpmbuild指令的用法:

從spec文檔建立有以下選項:

-bp  #隻執行spec的%pre 段(解開源碼包并打更新檔,即隻做準備)

-bc  #執行spec的%pre和%build 段(準備并編譯)

-bi  #執行spec中%pre,%build與%install(準備,編譯并安裝)

-bl  #檢查spec中的%file段(檢視檔案是否齊全)

-ba  #建立源碼與二進制包(常用)

-bb  #隻建立二進制包(常用)

-bs  #隻建立源碼包

從tarball包建立,與spec類似

-tp #對應-bp

-tc #對應-bc

-ti #對應-bi

-ta #對應-ba

-tb #對應-bb

-ts #對應-bs

從源碼包建立

--rebuild  #建立二進制包,通-bb

--recompile  #同-bi

* rpmbuild的其他參數

--buildroot=DIRECTORY   #确定以root目錄建立包

--clean  #完成打包後清除BUILD下的檔案目錄

--nobuild  #不進行%build的階段

--nodeps  #不檢查建立包時的關聯檔案

--nodirtokens

--rmsource  #完成打包後清除SOURCES

--rmspec #完成打包後清除SPEC

--short-cricuit

--target=CPU-VENDOR-OS #确定包的最終使用平台

軟體包所屬類别,具體類别有:

# less /usr/share/doc/rpm-4.8.0/GROUPS

Amusements/Games (娛樂/遊戲)

Amusements/Graphics(娛樂/圖形)

Applications/Archiving (應用/文檔)

Applications/Communications(應用/通訊)

Applications/Databases (應用/資料庫)

Applications/Editors (應用/編輯器)

Applications/Emulators (應用/仿真器)

Applications/Engineering (應用/工程)

Applications/File (應用/檔案)

Applications/Internet (應用/網際網路)

Applications/Multimedia(應用/多媒體)

Applications/Productivity (應用/産品)

Applications/Publishing(應用/印刷)

Applications/System(應用/系統)

Applications/Text (應用/文本)

Development/Debuggers (開發/調試器)

Development/Languages (開發/語言)

Development/Libraries (開發/函數庫)

Development/System (開發/系統)

Development/Tools (開發/工具)

Documentation (文檔)

System Environment/Base(系統環境/基礎)

System Environment/Daemons (系統環境/守護)

System Environment/Kernel (系統環境/核心)

System Environment/Libraries (系統環境/函數庫)

System Environment/Shells (系統環境/接口)

User Interface/Desktops(使用者界面/桌面)

User Interface/X (使用者界面/X視窗)

User Interface/X Hardware Support (使用者界面/X硬體支援)

其他

make %{?_smp_mflags} 

它就自動将軟體安裝時的路徑自動設定成如下約定: 

可執行程式/usr/bin 

依賴的動态庫/usr/lib或者/usr/lib64視作業系統版本而定。 

二次開發的頭檔案/usr/include 

文檔及手冊/usr/share/man

%setup 不加任何選項,僅将軟體包打開。 

%setup -n newdir 将軟體包解壓在newdir目錄。 

%setup -c 解壓縮之前先産生目錄。 

%setup -b num 将第num個source檔案解壓縮。 

%setup -T 不使用default的解壓縮操作。 

%setup -T -b 0 将第0個源代碼檔案解壓縮。 

%setup -c -n newdir 指定目錄名稱newdir,并在此目錄産生rpm套件。 

%patch 最簡單的更新檔方式,自動指定patch level。 

%patch 0 使用第0個更新檔檔案,相當于%patch ?p 0。 

%patch -s 不顯示打更新檔時的資訊。 

%patch -T 将所有打更新檔時産生的輸出檔案删除。

/sbin/install-info –delete %{_infodir}/%{name}.info %{_infodir}/dir 2> /dev/null || : 

These two scriptlets tell install-info to add entries for the info pages to the main index file on installation and remove them at erase time. The “|| :” in this case prevents failures that would typically affect systems that have been configured not to install any %doc files, or have read-only mounted, %_netsharedpath /usr/share.

Nginx rpm

#警告請将每行後面的#中文說明删掉,否則會文法錯誤,導緻無法執行。

Name:   nginx   # 軟體包名稱

Version:    1.2.0   # 版本号,(不能使用-)

Release:    1%{?dist}   # release号,對應下面的changelog,如 nginx-1.2.0-1.el6.x86_64.rpm

Summary:    nginx_install   # 簡要描述資訊

Source0:    %{name}-%{version}.tar.gz   # source主要是引用一下自己定義好的腳本,配置檔案之類的内容

Source1:    nginx-init

#可選裝的包gd-devel

./configure --user=nginx --group=nginx \

--http-proxy-temp-path=/var/tmp/nginx/proxy/

#其他可選項:

#--with-pcre --with-http_image_filter_module

#--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/

#--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi

#--http-scgi-temp-path=/var/tmp/nginx/scgi

#--with-file-aio

#%{__install}這個宏代表install指令

%{__install} -p -D %{SOURCE2} %{buildroot}/etc/nginx/nginx.conf

    /usr/sbin/groupadd nginx 2> /dev/null

    /usr/sbin/useradd -g nginx -M -s /sbin/nologin nginx 2> /dev/null

    /sbin/chkconfig --add nginx

    mkdir -p /var/log/nginx/

    /sbin/chkconfig --del nginx 2> /dev/null

    /etc/init.d/nginx stop > /dev/null 2>&1

%config(noreplace) /etc/nginx/nginx.conf

*  Thu Dec 01 2016 tian <[email protected]> - 1.2.0-1.el6

      本文轉自rshare 51CTO部落格,原文連結:http://blog.51cto.com/1364952/1955998,如需轉載請自行聯系原作者