利用rpmbuild打包定製屬於你自己的RPM包
阿新 • • 發佈:2019-01-08
這個是自己寫在CSDN上寫的第一個技術部落格, 因為最近自己在研究rpm打包. 今天把它分享出來哈~~ rpm的優勢就是安裝和後期升級方便快捷,無需再次手動編譯原始碼。節省很多時間。. 這裡已Nginx 伺服器安裝包為例項進行試驗.
環境:
1. 系統: CentOS release 6.5 (Final) x86_64
2.硬體: KVM虛擬機器, 雙核CPU,2G記憶體,50G硬碟
3.user: 使用普通使用者,這裡登陸方式是: user+key
4.許可權: 使用sudo 可使用root許可權
前期準備:
1. nginx 官網下載最新壓縮包: wget http://nginx.org/download/nginx-1.6.0.tar.gz
2. 安裝 rpmbuild 編譯必備工具(群組安裝方式): yum groupinstall "Development Tools"
開始定製:
1. 建好工作目錄(這裡也可以不建,預設工具安裝會自動建立)
[[email protected] ~]$ mkdir rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}
[[email protected] ~]$ cd rpmbuild/SPECS/
2. 開始編寫定製包用的spec檔案(檔案內容如下)
[huzhong@s186 SPECS]$ cat nginx.spec %define _localstatedir /home %define nginx_user nginx %define nginx_group nginx # distribution specific definitions
%define use_systemd (0%{?fedora} && 0%{?fedora} >= 18) || (0%{?rhel} && 0%{?rhel} >= 7)
%if 0%{?rhel} == 5
Group: System Environment/Daemons
Requires(pre): shadow-utils
Requires: initscripts >= 8.36
Requires(post): chkconfig
Requires: openssl
BuildRequires: openssl-devel
%endif
% if 0%{?rhel} == 6
Group: System Environment/Daemons
Requires(pre): shadow-utils
Requires: initscripts >= 8.36
Requires(post): chkconfig
Requires: openssl >= 1.0.1
BuildRequires: openssl-devel >= 1.0.1
%define with_spdy 1
%endif
%if 0%{?rhel} == 7
Group: System Environment/Daemons
Requires(pre): shadow-utils
Requires: systemd
Requires: openssl >= 1.0.1
BuildRequires: systemd
BuildRequires: openssl-devel >= 1.0.1
%define with_spdy 1
%endif
%if 0%{?suse_version}
Group: Productivity/Networking/Web/Servers
BuildRequires: libopenssl-devel
Requires(pre): pwdutils
%endif
# end of distribution specific definitions
Summary: High performance web server
Name: nginx
Version: 1.6.0
Release: 1%{?dist}.ngx
Vendor: nginx inc.
URL: http://nginx.org/
Source0: http://nginx.org/download/%{name}-%{version}.tar.gz
Source1: run.sh
Source2: nginx.conf
License: 2-clause BSD-like license
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
BuildRequires: zlib-devel
BuildRequires: pcre-devel
Provides: webserver
%description
nginx [engine x] is an HTTP and reverse proxy server, as well as a mail proxy server.
configure arguments:
%prep
%setup -q
%build
./configure \
--prefix=%{_localstatedir}/nginx \
--conf-path=%{_localstatedir}/nginx/conf/nginx.conf \
--http-client-body-temp-path=%{_localstatedir}/nginx/cache/client_temp \
--http-proxy-temp-path=%{_localstatedir}/nginx/cache/proxy_temp \
--http-fastcgi-temp-path=%{_localstatedir}/nginx/cache/fastcgi_temp \
--http-uwsgi-temp-path=%{_localstatedir}/nginx/cache/uwsgi_temp \
--http-scgi-temp-path=%{_localstatedir}/nginx/cache/scgi_temp \
--user=%{nginx_user} \
--group=%{nginx_group} \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
make %{?_smp_mflags}
%install
%{__rm} -rf $RPM_BUILD_ROOT
%{__make} DESTDIR=$RPM_BUILD_ROOT install
%{__mkdir} -p $RPM_BUILD_ROOT%{_localstatedir}/nginx/cache
%{__mkdir} -p $RPM_BUILD_ROOT%{_localstatedir}/nginx/conf/conf.d
%{__install} -m 755 -p %{SOURCE1} \
$RPM_BUILD_ROOT%{_localstatedir}/nginx/run.sh
%{__install} -m 644 -p %{SOURCE2} \
$RPM_BUILD_ROOT%{_localstatedir}/nginx/conf/nginx.conf
%{__mkdir} -p $RPM_BUILD_ROOT%{_localstatedir}/nginx/cache
%clean
%{__rm} -rf $RPM_BUILD_ROOT
%pre
# Add the "nginx" user
/usr/bin/id -g nginx >/dev/null 2>&1 || /usr/sbin/groupadd nginx
/usr/bin/id -u nginx >/dev/null 2>&1 || /usr/sbin/useradd -M -g nginx nginx
if [ -r /home/nginx/sbin/nginx ] ;then
/bin/cp -f /home/nginx/sbin/nginx{,.old} >/dev/null 2>&1 || :
fi
%post
# print site info
cat <<BANNER
----------------------------------------------------------------------
Thanks for using nginx!
Please find the official documentation for nginx here:
* http://nginx.org/en/docs/
Commercial subscriptions for nginx are available on:
* http://nginx.com/products/
----------------------------------------------------------------------
BANNER
%preun
if [ "$1" = "0" ] ;then
/home/nginx/run.sh stop >/dev/null 2>&1 || :
fi
%postun
#/home/nginx/run.sh softupdate >/dev/null 2>&1 || :
%files
%defattr(-,nginx,nginx)
%config(noreplace) /home/nginx/conf/nginx.conf
/home/nginx/conf/fastcgi_params.default
/home/nginx/conf/mime.types
/home/nginx/conf/nginx.conf.default
/home/nginx/html/50x.html
/home/nginx/html/index.html
/home/nginx/sbin/nginx
/home/nginx/logs
/home/nginx/run.sh
%exclude /home/nginx/conf/koi-utf
%exclude /home/nginx/conf/koi-win
%exclude /home/nginx/conf/win-utf
%exclude /home/nginx/conf/fastcgi_params
%exclude /home/nginx/conf/mime.types.default
%exclude /home/nginx/conf/fastcgi.conf
%exclude /home/nginx/conf/fastcgi.conf.default
/home/nginx/conf/scgi_params
/home/nginx/conf/scgi_params.default
/home/nginx/conf/uwsgi_params
/home/nginx/conf/uwsgi_params.default
%changelog
* Thu Apr 28 2014 Viman <zhong.hu@baoyugame.com>
- 1.6.0.1
* Thu Apr 24 2014 Konstantin Pavlov <thresh@nginx.com>
- 1.6.0
3. 定製預設配置檔案(這裡根據你自己的需求來定製) nginx.conf
這裡原始碼就不貼出來了,你也可以用nginx官方的nginx.conf
4. 開始編譯rpm包和srpm 原始碼包
[huzhong@s186 SPECS]$ rpmbuild -ba nginx.spec 5. 編譯成功後系統返回值應該是0 (echo $? 列印檢視), 編譯好的rpm包,放置在 ~/rpmbuild/RPMS/x86_64/ 目錄下 (這裡系統會根據你是i386 還是x86_64 位系統來建立不同的包和目錄)
詳情:
[huzhong@s186 ~]$ ls rpmbuild/RPMS/x86_64/ nginx-1.6.0-1.el6.ngx.x86_64.rpm nginx-debuginfo-1.6.0-1.el6.ngx.x86_64.rpm
6. 安裝rpm包(我這裡已經安裝成功了,所以加--test引數測試下)
[huzhong@s186 x86_64]$ sudo rpm -ivh nginx-1.6.0-1.el6.ngx.x86_64.rpm --test Preparing... ########################################### [100%] package nginx-1.6.0-1.el6.ngx.x86_64 is already installed 7. 最好來一張Nginx目錄架構圖
備註:
1. nginx.spec 檔案 前面我定製了 _localstatedir 變數,方便以後修改和目錄移植
2. nginx 所有安裝目錄在/home/nginx 下. 這麼做的優點就是資源統一管理備份更方便. (官方的包安裝是根據linux核心系統來的)
3. %define use_systemd 這裡還可以根據不同系統來自動匹配安裝(未完成,可以根據需求定製)
4. 在最好一步編譯的時候,如出現報錯,可根據系統提示來修改。
環境:
1. 系統: CentOS release 6.5 (Final) x86_64
2.硬體: KVM虛擬機器, 雙核CPU,2G記憶體,50G硬碟
3.user: 使用普通使用者,這裡登陸方式是: user+key
4.許可權: 使用sudo 可使用root許可權
前期準備:
1. nginx 官網下載最新壓縮包: wget http://nginx.org/download/nginx-1.6.0.tar.gz
2. 安裝 rpmbuild 編譯必備工具(群組安裝方式): yum groupinstall "Development Tools"
開始定製:
1. 建好工作目錄(這裡也可以不建,預設工具安裝會自動建立)
[[email protected] ~]$ cd rpmbuild/SPECS/
2. 開始編寫定製包用的spec檔案(檔案內容如下)
[huzhong@s186 SPECS]$ cat nginx.spec %define _localstatedir /home %define nginx_user nginx %define nginx_group nginx # distribution specific definitions
3. 定製預設配置檔案(這裡根據你自己的需求來定製) nginx.conf
這裡原始碼就不貼出來了,你也可以用nginx官方的nginx.conf
4. 開始編譯rpm包和srpm 原始碼包
[huzhong@s186 SPECS]$ rpmbuild -ba nginx.spec 5. 編譯成功後系統返回值應該是0 (echo $? 列印檢視), 編譯好的rpm包,放置在 ~/rpmbuild/RPMS/x86_64/ 目錄下 (這裡系統會根據你是i386 還是x86_64 位系統來建立不同的包和目錄)
詳情:
[huzhong@s186 ~]$ ls rpmbuild/RPMS/x86_64/ nginx-1.6.0-1.el6.ngx.x86_64.rpm nginx-debuginfo-1.6.0-1.el6.ngx.x86_64.rpm
6. 安裝rpm包(我這裡已經安裝成功了,所以加--test引數測試下)
[huzhong@s186 x86_64]$ sudo rpm -ivh nginx-1.6.0-1.el6.ngx.x86_64.rpm --test Preparing... ########################################### [100%] package nginx-1.6.0-1.el6.ngx.x86_64 is already installed 7. 最好來一張Nginx目錄架構圖
備註:
1. nginx.spec 檔案 前面我定製了 _localstatedir 變數,方便以後修改和目錄移植
2. nginx 所有安裝目錄在/home/nginx 下. 這麼做的優點就是資源統一管理備份更方便. (官方的包安裝是根據linux核心系統來的)
3. %define use_systemd 這裡還可以根據不同系統來自動匹配安裝(未完成,可以根據需求定製)
4. 在最好一步編譯的時候,如出現報錯,可根據系統提示來修改。