環境說明 nginx
系統版本 CentOS 6.9 x86_64 c++
軟件版本 fpm-1.4.0 編程
一、安裝ruby環境 vim
fpm利用ruby編程語言開發,先安裝ruby的環境 centos
[root@m01 ~]# yum -y install ruby rubygems ruby-devel ruby
二、更換Ruby Gems源 bash
將官方的源更換爲國內的源 app
gem sources --add https://mirrors.tuna.tsinghua.edu.cn/rubygems/ --remove http://rubygems.org/ 編程語言
驗證:gem sources -l 工具
https://mirrors.tuna.tsinghua.edu.cn/rubygems/
三、使用gem安裝fpm
gem install fpm
若出現如下報錯
yum install gcc gcc-c++ glibc -y先安裝gcc編譯器
再次安裝gem install fpm
出現報錯
出現這個報錯,是由於fpm最新版只支持centos7,對於centos6只能使用舊版的fpm
官方網站:https://rubygems.org/
官方中文網站:https://gems.ruby-china.org/
gem install fpm -v 1.4.0 安裝舊版本,-v指定版本爲2015年最後一個版本
出現報錯
[root@m01 ~]# gem install fpm -v 1.4.0
ERROR: Error installing fpm:
ffi requires Ruby version >= 1.9.
說明fpm的依賴包ffi默認也是安裝最新版本
[root@m01 ~]# gem install ffi -v 1.9.10
Building native extensions. This could take a while...
Successfully installed ffi-1.9.10
1 gem installed
Installing ri documentation for ffi-1.9.10...
Installing RDoc documentation for ffi-1.9.10...
再次安裝gem install fpm -v 1.4.0
使用gem dependency -r fpm 查看依賴
四、fpm的使用
4.1 參數說明
fpm (rpm包,deb包)
-s 指定源類型
-t 指定目標類型,即想要製做爲何包
-n 指定包的名字
-v 指定包的版本號
-C 指定打包的相對路徑 Change directory to here before searching forfiles
-d 指定依賴於哪些包
-f 第二次打包時目錄下若是有同名安裝包存在,則覆蓋它
-p 輸出的安裝包的目錄,不想放在當前目錄下就須要指定
--post-install 軟件包安裝完成以後所要運行的腳本;同--after-install
--pre-install 軟件包安裝完成以前所要運行的腳本;同--before-install
--post-uninstall 軟件包卸載完成以後所要運行的腳本;同--after-remove
--pre-uninstall 軟件包卸載完成以前所要運行的腳本;同--before-remove
4.2 製做nginx-1.12.2的rpm包
4.2.1 本地編譯安裝nginx-1.12.2
mkdir -p /service/tools
mkdir /application
cd /service/tools
wget http://nginx.org/download/nginx-1.12.2.tar.gz
tar zxvf nginx-1.12.2.tar.gz
yum install gcc gcc-c++ glibc pcre-devel zlib-devel openssl-devel -y
cd nginx-1.12.2
./configure --prefix=/application/nginx-1.12.2 --pid-path=/var/run/nginx.pid --user=nginx --group=nginx --with-http_ssl_module
make && make install
ln -s /application/nginx-1.12.2 /application/nginx
ln -s /application/nginx/sbin/nginx /usr/bin/
useradd -M -s /sbin/nologin -r -u 88 nginx
cd /application/nginx/conf/
grep -Ev '^$|#' nginx.conf.default >nginx.conf
4.2.2 編寫腳本
安裝後腳本:
[root@m01 scripts]# vim nginx_rpm.sh
#!/bin/bash
useradd -M -s /sbin/nologin -r -u 88 nginx
ln -s /application/nginx-1.12.2 /application/nginx
ln -s /application/nginx/sbin/nginx /usr/bin/
卸載後腳本
[root@m01 scripts]# vim nginx_remove.sh
#!/usr/bin
nginx -s stop
rm -fr /application/nginx1.12.2
rm -fr /application/nginx
rm -fr /usr/bin/nginx
4.2.3 打包
打包以前須要安裝rpmbuild工具才能使用fpm進行打包
yum install rpm-build -y
開始打包
fpm -s dir -t rpm -n nginx -v 1.12.2 -d 'pcre-devel,openssl-devel' --post-install /service/scripts/nginx_rpm.sh --post-uninstall /service/scripts/nginx_remove.sh -f /application/nginx-1.12.2/
4.2.4 製做nginx-1.12.2的rpm包(帶日誌輪詢和高亮顯示)
下載官方nginx的rpm包
https://mirrors.aliyun.com/epel/6/x86_64/Packages/n/nginx-1.10.2-1.el6.x86_64.rpm
查看rpm包的內容(解壓rpm包)
[root@m01 nginx]# rpm2cpio nginx-1.10.2-1.el6.x86_64.rpm | cpio -div #解壓官方的rpm包
[root@m01 nginx]# ls
etc nginx-1.10.2-1.el6.x86_64.rpm usr var
將官方的etc/logrotate.d/nginx
和usr/share/vim/vimfiles/{ftdetect,indent,syntax}/nginx.vim拷貝到本地系統中
[root@m01 nginx]# cp etc/logrotate.d/nginx /etc/logrotate.d/
[root@m01 nginx]# cp usr/share/vim/vimfiles/ftdetect/nginx.vim /usr/share/vim/vimfiles/ftdetect/
[root@m01 nginx]# cp usr/share/vim/vimfiles/indent/nginx.vim /usr/share/vim/vimfiles/indent/
[root@m01 nginx]# cp usr/share/vim/vimfiles/syntax/nginx.vim /usr/share/vim/vimfiles/syntax/
修改卸載後腳本
[root@m01 scripts]# vim nginx_remove.sh
#!/usr/bin
nginx -s stop
rm -fr /application/nginx1.12.2
rm -fr /application/nginx
rm -fr /usr/bin/nginx
rm -fr /usr/share/vim/vimfiles/ftdetect/nginx.vim
rm -fr /usr/share/vim/vimfiles/indent/nginx.vim
rm -fr /usr/share/vim/vimfiles/syntax/nginx.vim
rm -fr /etc/logrotate.d/nginx
打包
[root@m01 scripts]# fpm -s dir -t rpm -n nginx -v 1.12.2 -d ' pcre-devel,openssl-devel ' --post-install /service/scripts/nginx_rpm.sh --post-uninstall /service/scripts/nginx_remove.sh -f /application/nginx-1.12.2/ /etc/logrotate.d/nginx /usr/share/vim/vimfiles/{ftdetect,indent,syntax}/nginx.vim
附:
一、查看nginx命令所須要的庫文件並依賴於哪些rpm包
ldd /application/nginx/sbin/nginx|awk -F "[ ]+" 'NR>1{print $3}'|sed '/^$/d'|xargs rpm -qf|sort -n|uniq|sed -r 's#\-[1-9][0-9.]*.*$##g'
二、查看rpm包中的腳本
rpm -qp --scripts nginx-1.12.2-1.x86_64.rpm
三、查看rpm包中的內容
rpm -qlp xxx.rpm
四、解壓rpm包
rpm2cpio xxx.rpm | cpio -div
博主原創文章,轉載請務必註明出處