通過定制nginx的rpm包學習如何制作rpm安裝包


      RPM是RedHat Package Manager(RedHat軟件包管理工具)的縮寫,是一種用於互聯網下載包的打包及安裝工具,它包含在某些Linux分發版中。它生成具有.RPM擴展名的文件。使用rpm安裝軟件和管理軟件非常的方便。

1.安裝rpm-build

#yum -y install rpm-build redhat-rpm-config

2.建立目錄結構

#mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
# tree -n ~/rpmbuild/
/root/rpmbuild/
├── BUILD        存放源代碼
├── RPMS         存放用於管理rpm制作進程的spec文件
├── SOURCES      解壓后的文件存放在這里
├── SPECS        存放由rpmbuild制作好的二進制包
└── SRPMS        存放由rpmbuild制作好的源碼包
5 directories, 0 files

3.下載源碼包

將nginx、nginx-rtmp-module放在SOURCE目錄下

SOURCES]# ll
總用量 71320
-rw-r--r--.  1 root root 69195965 8月  15 09:09 nginx-1.15.3.tar.gz
-rw-r--r--.  1 root root     4876 8月  15 09:59 nginx.conf-rw-r--r--.  1 root root  3821160 8月  14 16:39 nginx-rtmp-module.tar.gz

4.制作.spec文件

在SPECS下執行:vim nginx.spec

# cat nginx.spec
Name: nginx
Version: 1.15.3
Release: 1%{?dist}
Summary: nginx rmp package production
Group: Applications/Archiving
License: GPLv2
URL: http://www.baijiayun.com/
Packager: lizhenqi <lizhenqi@baijiayun.com>
Vendor: 百家雲
Source0: %{name}-%{version}.tar.gz
Source1: nginx-rtmp-module.tar.gz
Source2: nginx.conf
BuildRoot: %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX)
BuildRequires: gcc
Requires: openssl,openssl-devel,pcre-devel,pcre

%description
              Custom nginx rpm package. 
              Nginx: 
                    Github: https://github.com/nginx/nginx 
                    Branch: master  
                    Date  : 2018.08.08
              Nginx-Rtmp-Module: 
                    Github: https://github.com/arut/nginx-rtmp-module 
                    Branch: master 
                    Date  : 2018.07.30

%prep
rm -rf $RPM_BUILD_DIR/nginx-1.15.3
rm -rf $RPM_BUILD_DIR/nginx-rtmp-module
tar fx $RPM_SOURCE_DIR/nginx-1.15.3.tar.gz
tar fx $RPM_SOURCE_DIR/nginx-rtmp-module.tar.gz

%build
cd nginx-1.15.3
./configure \
--prefix=/usr/local/nginx \
--with-openssl=/usr/local/openssl \
--with-pcre \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-stream \
--add-module=../nginx-rtmp-module
#--with-http_perl_module \
make %{?_smp_mflags}

%install
rm -rf %{buildroot}
cd nginx-1.15.3
make install DESTDIR=%{buildroot}
%{__install} -p -D %{SOURCE2} %{buildroot}/usr/local/nginx/conf/nginx.conf

%pre
if [ $1 == 1 ];then                                              # $1有3個值,代表動作,安裝類型,處理類型
    /usr/sbin/useradd -r www -s /sbin/nologin 2> /dev/null       # 1:表示安裝
fi                                                               # 2:表示升級      
                                                                 # 0:表示卸載
%preun
if [ $1 == 0 ];then
    /usr/sbin/userdel -r www 2> /dev/null
    /etc/init.d/nginx stop > /dev/null 2>&1
fi

%postun

%clean
rm -rf %{buildroot}

%files
%defattr(-,root,root,-)
/usr/local/nginx
%config(noreplace) /usr/local/nginx/conf/nginx.conf

5.spec文件解釋

#:以#開頭是注釋,rpm會忽略它。
Summary:   簡單描述軟件。
Name :    定義rpm的名稱。
Version:   定義軟件版本
Release:   發行版本
License:   定義許可證
Group:    軟件分類
Source:   源碼下載地址
URL:      源碼相關網站
Distribution: 發行版系列
Packager: 打包人的信息
%description:軟件詳細描述,可多行
%prep :軟件編譯之前的處理,如解壓。
%build :開始編譯軟件,如make
%install :開始安裝軟件,如make install
%files :指定哪些文件需要被打包,如/usr/local/nginx
%preun :定義卸載之前的動作,如殺掉進程。
這里只介紹了幾個常用的tag,更詳細的請參考:http://www.rpm.org/max-rpm/ch-rpm-inside.html

6.開始RPM制作

# rpmbuild -bb nginx.spec

 

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格式包

7.測試RPM包

x86_64]# yum reinstall nginx-1.15.3-1.el7.centos.x86_64.rpm

8.查看軟件包信息

x86_64]# rpm -qi nginx
Name        : nginx
Version     : 1.15.3
Release     : 1.el7.centos
Architecture: x86_64
Install Date: 2018年08月15日 星期三 11時45分05秒
Group       : Applications/Archiving
Size        : 4344453
License     : GPLv2
Signature   : (none)   # rpm包未簽名狀態
Source RPM  : nginx-1.15.3-1.el7.centos.src.rpm
Build Date  : 2018年08月15日 星期三 11時43分15秒
Build Host  : baijiayun
Relocations : (not relocatable)
Packager    : lizhenqi <lizhenqi@baijiayun.com>
Vendor      : 百家雲
URL         : http://www.baijiayun.com/
Summary     : nginx rmp package production
Description :
              Custom nginx rpm package.
              Nginx:
                    Github: https://github.com/nginx/nginx
                    Branch: master
                    Date  : 2018.08.08
              Nginx-Rtmp-Module:
                    Github: https://github.com/arut/nginx-rtmp-module
                    Branch: master
                    Date  : 2018.07.30

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

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

以下未驗證

9.生成密鑰並驗證

9.1.使用gpg方式生成簽名密鑰

# 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: 353025240@qq.com<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) <353025240@qq.com>
sub   2048R/263FB359 2014-11-26

9.2.查看生成的密鑰

# gpg --list-keys
/root/.gnupg/pubring.gpg
------------------------
pub   2048R/DF63EDFB 2014-11-26
uid                  nmshuishui (gen-key) <353025240@qq.com>
sub   2048R/263FB359 2014-11-26

9.3.導出公鑰以供驗證

# gpg --export -a "nmshuishui" > RPM-GPG-KEY-nmshuishui

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

# vim ~/.rpmmacros
%_gpg_name nmshuishui

9.5.為rpm包簽名

# 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:

9.6.將公鑰導入rpm包

# rpm --import RPM-GPG-KEY-nmshuishui

9.7.驗證

# 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.8.重新安裝nginx,驗證安裝包的簽名信息

# rpm -ivh /home/hero/rpmbuild/RPMS/x86_64/nginx-1.7.7-3.el6.x86_64.rpm 
Preparing...                ########################################### [100%]
   1:nginx                  ########################################### [100%]
[root@localhost ~]# 
[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: Thu 27 Nov 2014 10:58:44 AM CST      Build Host: localhost
Group       : Applications/Archiving        Source RPM: nginx-1.7.7-3.el6.src.rpm
Size        : 793593                           License: GPLv2
Signature   : RSA/SHA1, Thu 27 Nov 2014 10:40:02 AM CST, Key ID 6f731e81df63edfb   # 與 1 比起來,多了簽名信息
Packager    : nmshuishui <353025240@qq.com>
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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM