常有人問能不能把編譯好的目錄直接複製到別台機器使用。比較正規的做法是做成 RPM 包。以下以 nginx 為例,示範在 CentOS 5.x x86_64 上透過 spec 檔打包。
mkdir -p /usr/src/redhat/{SOURCES,SPECS,BUILD,RPMS,SRPMS}
SOURCES 放原始碼與補丁、SPECS 放 spec 檔、BUILD 是解壓後的工作目錄、RPMS 存二進位包、SRPMS 存原始碼包。
cd /usr/src/redhat/SOURCES wget https://nginx.org/download/nginx-1.2.0.tar.gz
Summary: High performance web server
Name: Nginx
Version: 1.2
Release: 0.el5.ngx
License: 2-clause BSD-like license
Group: Applications/Server
Source: https://nginx.org/download/nginx-1.2.0.tar.gz
URL: https://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
spec 是打包的核心:%description 描述、%prep 編譯前處理、%build 編譯、%install 安裝、%preun 卸載前動作、%files 指定要打包的內容。
yum -y install gcc rpm-build pcre-devel cd /usr/src/redhat/SPECS/ rpmbuild -bb nginx.spec
-bb 表示只產生二進位包,完成後可在 RPMS/x86_64/ 找到 nginx-1.2.0-0.el5.ngx.x86_64.rpm,之後就能在其他同平台機器直接安裝部署。把 spec 中的名稱、下載網址與 configure 參數換掉,就能改做成 apache 等其他套件。
另外也有 rpm_create 這類工具,不需要撰寫 spec,只要會 shell 指令就能打包,官方網站為 code.google.com/p/rpmcreate。