最近有好多朋友問到怎麼制作rpm包,可不可把其它服務器上編譯好的軟件目錄復雜到其它服務器上直接應用等等。。。這裡做個簡單的介紹,高級復雜的不會。
此方法是通過編寫spec文件,使用rpmbuild來完成一個rpm的打包。
以nginx為例進行介紹
制作平台:centos 5.x X86_64
四步走:
第一步:建立目錄結構
mkdir /usr/src/redhat/{SOURCES,SPECS,BUILD,RPMS,SRPMS} -p
相關目錄介紹:
/usr/src/redhat/SOURCES #存放源代碼、補丁等文件 /usr/src/redhat/SPECS #存放用於管理rpm制作進程的spec文件 /usr/src/redhat/BUILD #解壓後的文件存放目錄 /usr/src/redhat/RPMS #存放由rpmbuild制作好的二進制包 /usr/src/redhat/SRPMS #存放由rpmbuild制作好的源碼包
第二步:把源碼包放在SOURCES目錄下
cd /usr/src/redhat/SOURCES
wget http://nginx.org/download/nginx-1.2.0.tar.gz
第三步:生成nginx.spec文件
cd /usr/src/redhat/SPECS
cat nginx.spec
#
# spec file for nginx
# Build 2012-07-17
# By opsren
#
Summary: High performance web server
Name: Nginx
Version: 1.2
Release: 0.el5.ngx
License: 2-clause BSD-like license
Group: Applications/Server
Source: http://nginx.org/download/nginx-1.2.0.tar.gz
URL: http://nginx.org
Distribution: Centos/Redhat
Packager: qiuzhijun <250621008@qq.com>
%description
Nginx ("engine x") is a high performance HTTP and reverse proxy server, as well as a mail(IMAP/POP3/SMTP) proxy server.
%prep
tar zxf $RPM_SOURCE_DIR/nginx-1.2.0.tar.gz
%build
cd nginx-1.2.0
./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre --lock-path=/var/run/nginx.lock --pid-path=/var/run/nginx.pid
make
%install
cd nginx-1.2.0
make install
%preun
if [ -z "`ps aux | grep nginx | grep -v grep`" ];then
pkill nginx >/dev/null
exit 0
fi
%files
/usr/local/webserver/nginx
第四步:RPM包制作
首先系統要安裝好必備的制作工具:gcc、rpmbuild等
yum -y install gcc rpm-build pcre-devel cd /usr/src/redhat/SPECS/ rpmbuild -bb nginx.spec
通過上面這條命令,會在/usr/src/redhat/RPMS/x86_64/下面生成nginx-1.2.0-1.el5.ngx.x86_64.rpm這個文件
-bb 這個選項就是制作二進制包(build binary package only from <specfile>)
對spec文件內容進行簡單說明:
spec文件是制作rpm包的核心!
以#開頭的是注釋信息;
Summary:對相關軟件進行簡單描述說明
Name:定義rpm包的名稱
Version:定義軟件的版本號
Release:發行版本
License:定義許可證
Group:說明軟件屬於哪種應用類型
Source:軟件源碼下載地址
URL:軟件相關官方站點
Distribution: 發行版系列
Packager: 制作人的簡單信息
%description:軟件詳細描述信息
%prep:軟件編譯之前的處理
%build:編譯軟件
%install:安裝軟件
%preun:定義卸載之前的動作
%files:指定要打包的軟件包,這裡是/usr/local/webserver/nginx
對於更詳細的說明請參考官方資料:http://www.rpm.org/max-rpm/ch-rpm-inside.html
#
# spec file for apache
# Build 2012-07-17
# By opsren
#
Summary: High stability web server
Name: Apache
Version: 2.2
Release: 22.el5
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: qiuzhijun <250621008@qq.com>
%description
Apache is a first web server
%prep
tar zxf $RPM_SOURCE_DIR/httpd-2.2.22.tar.gz
%build
cd httpd-2.2.22
./configure --prefix=/usr/local/webserver/apache --enable-so --enable-deflate --enable-headers --enable-mods-shared=all --enable-rewrite
make
%install
cd httpd-2.2.22
make install
%preun
if [ -z "`ps aux | grep httpd | grep -v grep`" ];then
pkill httpd >/dev/null
exit 0
fi
%files
/usr/local/webserver/apache
下面是apache的spec文件實例:
以後對於相同或類似平台可以到其它服務器上進行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
大家可以去官方站點參考!