CentOS 下如何制作 apache2.2 RPM 包

系统环境:CentOS 6.8 x64

apache 版本:2.2.32

.

安装工具包

[root@rpmbuild ~]# yum -y install rpm-build rpmdevtools

.

安装 apache 编译时可能需要用到的依赖包

[root@rpmbuild ~]# yum -y install zlib pcre pcre-devel openssl-devel gcc gcc-c++ apr apr-devel make

初始化一个目录结构

[root@rpmbuild ~]# rpmdev-setuptree

[root@rpmbuild ~]# tree rpmbuild/

rpmbuild/

├── BUILD # 编译 rpm包的临时目录

├── RPMS # 存放由 rpmbuild最终制作好的二进制包

├── SOURCES # 所有源代码和补丁文件的存放目录

├── SPECS # 存放 SPEC文件的目录(重要)

└── SRPMS # 最终生成的二进制源码包所在目录

.

准备要制作的源码包和所需的一些额外文件

[root@rpmbuild ~]# cd rpmbuild/SOURCES/

[root@rpmbuild SOURCES]# ll

总用量 7548

-rw-r--r-- 1 root root 7684420 9 月 29 12:20 httpd-2.2.32.tar.gz

-rw-r--r-- 1 root root 14147 9 月 29 12:21 httpd.conf

-rw-r--r-- 1 root root 12188 9 月 29 13:58 httpd-ssl.conf

-rw-r--r-- 1 root root 87 9 月 30 10:25 index.html

-rw-r--r-- 1 root root 1407 9 月 29 13:59 test.crt

-rw-r--r-- 1 root root 1675 9 月 29 13:59 test.key

.

进入 SPECS 目录并创建 httpd.spec 文件

[root@rpmbuild SOURCES]# cd ../SPECS/

[root@rpmbuild SPECS]# rpmdev-newspec httpd.spec

Skeleton specfile (minimal) has been created to "httpd.spec".

[root@rpmbuild SPECS]# ls

httpd.spec

[root@rpmbuild SPECS]# vim httpd.spec

====================================================

Name: httpd

Version: 2.2.32

Release: 1%{?dist}

Summary: Made from httpd-2.2.32.tar.gz # 简单描述信息,最好不超过 50 个字符

Group: Applications/Archiving # 用"less /usr/share/doc/rpm-4.8.0/GROUPS"里的一组

License: GPLv2 #最好是对方源码包的 LICENSE,一般为 GPLv2

URL: http://rpmbuild.test.org

Packager: CentOS

Vendor: CentOS

Source0: %{name}-%{version}.tar.gz # 源码包,这行必须要有

Source1: httpd.conf # 自定义主配置文件

Source2: httpd-ssl.conf # 自定义 ssl 配置文件

Source3: test.key # 自定义 ssl 密钥

Source4: test.crt

Source5: index.html # 自定义首页

BuildRequires: gcc # 编译代码需要的软件。

Requires: zlib,pcre,pcre-devel,openssl,openssl-devel,apr,apr-devel # 定义 rpm 包安装时依赖的包

%description # 软件包的描述,可多行编写,段中间空行隔开

Custom a rpm by yourself! Build httpd-2.2.32.tar.gz to httpd-2.2.32-1.el6.x86_64.rpm

%prep # 编译之前的处理

%setup -q

%build # 开始编译

./configure \

--prefix=/usr/local/apache2.32 \

--sysconfdir=/etc/httpd \

--enable-so \

--enable-ssl \

--enable-cgi \

--enable-rewrite \

--with-zlib \

--with-pcre \

--enable-modules=most \

--with-mpm=event \

--with-z \

--with-included-apr \

--enable-deflate=shared \

--enable-expires=shared \

--enable-static-support \

--enable-proxy

make %{?_smp_mflags}

%install # 开始安装

rm -rf $RPM_BUILD_ROOT

make install DESTDIR=$RPM_BUILD_ROOT # 安装到临时安装目录

%{__install} -p -D %{SOURCE1} $RPM_BUILD_ROOT/etc/httpd/httpd.conf # 替换自定义文件

%{__install} -p -D %{SOURCE2} $RPM_BUILD_ROOT/etc/httpd/extra/httpd-ssl.conf

%{__install} -p -D %{SOURCE3} $RPM_BUILD_ROOT/etc/httpd/test.key

%{__install} -p -D %{SOURCE4} $RPM_BUILD_ROOT/etc/httpd/test.crt

%{__install} -p -D %{SOURCE5} $RPM_BUILD_ROOT/usr/local/apache2.32/htdocs/index.html

%pre # 安装前执行的动作

%post # 安装后执行的动作,如果是新安装才执行

if [ $1 == 1 ];then

cp -f /usr/local/apache2.32/bin/apachectl /etc/init.d/httpd 2> /dev/null

sed -i '1a# chkconfig: 35 85 15' /etc/init.d/httpd 2> /dev/null

chkconfig --add httpd 2> /dev/null

chkconfig httpd on 2> /dev/null

fi

%preun # 卸载前的动作

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

%postun # 卸载后的动作

if [ $1 == 0 ];then

rm -rf /usr/local/apache2.32 2> /dev/null

rm -rf /etc/httpd 2> /dev/null

rm -rf /etc/init.d/httpd 2> /dev/null

fi

%clean

rm -rf $RPM_BUILD_ROOT

%files # 指定哪些文件需要被打包

%defattr(-,root,root,-)

/usr/local/apache2.32 # 表示包含此目录下的所有文件

/etc/httpd

%doc

%changelog

====================================================

[root@rpmbuild SPECS]# rpmbuild -bb httpd2.2.spec # 表示只制作二进制包

[root@rpmbuild SPECS]# rpmbuild -ba httpd2.2.spec # 表示既制作二进制包又制作 src 格式包

[root@rpmbuild SPECS]# du -sh ../RPMS/x86_64/httpd-2.2.32-1.el6.x86_64.rpm

3.7M ../RPMS/x86_64/httpd-2.2.32-1.el6.x86_64.rpm

[root@rpmbuild SPECS]# rpm -qpl ../RPMS/x86_64/ httpd-2.2.32-1.el6.x86_64.rpm

[root@rpmbuild SPECS]# rpm -ivh ../RPMS/x86_64/ httpd-2.2.32-1.el6.x86_64.rpm

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

1: httpd ########################################### [100%]

[root@rpmbuild SPECS]# rpm -qi httpd

Name : httpd Relocations: (not relocatable)

Version : 2.2.32 Vendor: CentOS

Release : 1.el6 Build Date: 2018 年 09 月 30 日 星期日 10 时 44 分 02 秒

Install Date: 2018 年 09 月 30 日 星期日 10 时 47 分 23 秒 Build Host: rpmbuild.test.org

Group : Applications/Archiving Source RPM: httpd-2.2.32-1.el6.src.rpm

Size : 19513156 License: GPL

Signature : (none)

Packager : CentOS

URL : http://rpmbuild.test.org

Summary : Made from httpd-2.2.32.tar.gz

Description :

Custom a rpm by yourself! Build httpd-2.2.32.tar.gz to httpd-2.2.32-1.el6.x86_64.rpm

.

使用 gpg 方式生成签名密钥

[root@rpmbuild ~]# gpg --gen-key # 在图形界面下操作

gpg (GnuPG) 2.0.14; Copyright (C) 2009 Free Software Foundation, Inc.

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law.

gpg: 已创建目录'/root/.gnupg'

gpg: 新的配置文件'/root/.gnupg/gpg.conf'已建立

gpg: 警告:在'/root/.gnupg/gpg.conf'里的选项于此次运行期间未被使用

gpg: 钥匙环'/root/.gnupg/secring.gpg'已建立

gpg: 钥匙环'/root/.gnupg/pubring.gpg'已建立

请选择您要使用的密钥种类:

(1) RSA and RSA (default)

(2) DSA and Elgamal

(3) DSA (仅用于签名)

(4) RSA (仅用于签名)

您的选择?

RSA 密钥长度应在 1024 位与 4096 位之间。

您想要用多大的密钥尺寸?(2048)

您所要求的密钥尺寸是 2048 位

请设定这把密钥的有效期限。

0 = 密钥永不过期

= 密钥在 n 天后过期

w = 密钥在 n 周后过期

m = 密钥在 n 月后过期

y = 密钥在 n 年后过期

密钥的有效期限是?(0)

密钥永远不会过期

以上正确吗?(y/n)y

You need a user ID to identify your key; the software constructs the user ID

from the Real Name, Comment and Email Address in this form:

"Heinrich Heine (Der Dichter) <heinrichh>"/<heinrichh>

真实姓名:rpmbuild

电子邮件地址:[email protected]

注释:GPG-RPM-KEY

您选定了这个用户标识:

"rpmbuild (GPG-RPM-KEY) <rpmbuild>"/<rpmbuild>

更改姓名(N)、注释(C)、电子邮件地址(E)或确定(O)/退出(Q)?O

您需要一个密码来保护您的私钥。

can't connect to `/root/.gnupg/S.gpg-agent': 没有那个文件或目录

gpg-agent[15055]: 已创建目录'/root/.gnupg/private-keys-v1.d'

我们需要生成大量的随机字节。这个时候您可以多做些琐事(像是敲打键盘、移动

鼠标、读写硬盘之类的),这会让随机数字发生器有更好的机会获得足够的熵数。

# 这里不需要输入东西,只需要移动鼠标即可

gpg: /root/.gnupg/trustdb.gpg:建立了信任度数据库

gpg: 密钥 D75962BF 被标记为绝对信任

公钥和私钥已经生成并经签名。

gpg: 正在检查信任度数据库

gpg: 需要 3 份勉强信任和 1 份完全信任,PGP 信任模型

gpg: 深度:0 有效性: 1 已签名: 0 信任度:0-,0q,0n,0m,0f,1u

pub 2048R/D75962BF 2018-09-26

密钥指纹 = 6EF5 BF25 DA5D 1216 4710 4CD7 0A95 3DE9 D759 62BF

uid rpmbuild (GPG-RPM-KEY) <rpmbuild>

sub 2048R/0C94E7EA 2018-09-26

.

查看生成的密钥

[root@rpmbuild ~]# gpg --list-key

/root/.gnupg/pubring.gpg

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

pub 2048R/D75962BF 2018-09-26

uid rpmbuild (GPG-RPM-KEY) <rpmbuild>

sub 2048R/0C94E7EA 2018-09-26

.

导出公钥以供验证

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

.

在~/.rpmmacros 宏中定义加密密钥

[root@rpmbuild ~]# cat .rpmmacros

%_topdir %(echo $HOME)/rpmbuild

%_smp_mflags -j3

#%__arch_install_post /usr/lib/rpm/check-rpaths /usr/lib/rpm/check-buildroot

%_gpg_name rpmbuild # 添加这一行

.

为 rpm 包签名

[root@rpmbuild ~]# rpm --addsign rpmbuild/RPMS/x86_64/httpd-2.2.32-1.el6.x86_64.rpm

Enter pass phrase: # 输入私钥密码

Pass phrase is good.

rpmbuild/RPMS/x86_64/httpd-2.2.32-1.el6.x86_64.rpm:

.

将公钥导入 rpm 包

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

.

验证 rpm 包密钥

[root@rpmbuild ~]# rpm --checksig rpmbuild/RPMS/x86_64/httpd-2.2.32-1.el6.x86_64.rpm

rpmbuild/RPMS/x86_64/httpd-2.2.32-1.el6.x86_64.rpm: rsa sha1 (md5) pgp md5 OK

.

重新安装 apache,验证安装包的签名信息

[root@rpmbuild ~]# rpm -qa | grep httpd

httpd-2.2.32-1.el6.x86_64

[root@rpmbuild ~]# rpm -e httpd-2.2.32-1.el6.x86_64

[root@rpmbuild ~]# rpm -ivh rpmbuild/RPMS/x86_64/httpd-2.2.32-1.el6.x86_64.rpm

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

1: httpd ########################################### [100%]

[root@rpmbuild ~]# rpm -qi httpd

Name : httpd Relocations: (not relocatable)

Version : 2.2.32 Vendor: CentOS

Release : 1.el6 Build Date: 2018 年 09 月 30 日 星期日 10 时 44 分 02 秒

Install Date: 2018 年 09 月 30 日 星期日 11 时 36 分 39 秒 Build Host: rpmbuild.test.org

Group : Applications/Archiving Source RPM: httpd-2.2.32-1.el6.src.rpm

Size : 19513156 License: GPL

Signature : RSA/SHA1, 2018 年 09 月 30 日 星期日 11 时 32 分 20 秒, Key ID 0a953de9d75962bf

Packager : CentOS

URL : http://rpmbuild.test.org

Summary : Made from httpd-2.2.32.tar.gz

Description :

Custom a rpm by yourself! Build httpd-2.2.32.tar.gz to httpd-2.2.32-1.el6.x86_64.rpm

.

[root@rpmbuild ~]# /etc/init.d/httpd configtest

Syntax OK

[root@rpmbuild ~]# ls /usr/local/apache2.32/

bin build cgi-bin error htdocs icons include lib logs man manual modules

[root@rpmbuild ~]# ls /etc/httpd/

extra httpd.conf magic mime.types original test.crt test.key

[root@rpmbuild ~]# chkconfig --list httpd

httpd 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭

[root@rpmbuild ~]# /etc/init.d/httpd start

[root@rpmbuild ~]# netstat -tnlp | grep httpd

tcp 0 0 :::80 :::* LISTEN 5724/httpd

tcp 0 0 :::443 :::* LISTEN 5724/httpd

.

问题解决:

问题 1:file '/usr/local/apache2.32/lib/libaprutil-1.so.0.5.4' contains an invalid rpath '/usr/local/apache2.32/lib' in

[/usr/local/apache2.32/lib]

解决:在~/.rpmmacros 中注释掉"%__arch_install_post /usr/lib/rpm/check-rpaths /usr/lib/rpm/check-buildroot"

.

问题 2:打包后的 rpm 没有或者缺少文件

解决:检查%files 部分是否加了相关要打包的文件或者目录

.

CentOS 下如何制作 apache2.2 RPM 包

.

CentOS 下如何制作 apache2.2 RPM 包


分享到:


相關文章: