[TOC]
# fpm介紹
項目地址:[https://github.com/jordansissel/fpm](https://github.com/jordansissel/fpm)
fpm的目標是使構建軟件包(如rpm,deb,OSX軟件包等)變得容易且快速。
簡單說就是將一種類型的包轉換成另一種類型。
# 安裝fpm
1. fpm是ruby寫的,因此系統環境需要ruby,CentOS 7需要ruby版本號大于2.2。
2. 安裝ruby模塊
yum -y install ruby rubygems ruby-devel rpm-build
3. 查看當前使用的rubygems倉庫
gem sources list
4. 添加淘寶的Rubygems倉庫,外國的源慢,移除原生的Ruby倉庫
gem sources --add https://ruby.taobao.org/ --remove http://rubygems.org/
5. 安裝fpm,gem從rubygem倉庫安裝軟件類似yum從yum倉庫安裝軟件。
gem install fpm
ps: 遇到安裝錯誤請檢查ruby版本
# 使用fpm
## 常用參數
```
-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
```
## 打包rpm實例
1. 創建文件夾
mkdir -p /opt/server/{pkg,scripts}
2. 創建用戶
useradd -s /sbin/nologin -M www
3. 下載nginx源碼包,解壓
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar zxvf nginx-1.16.1.tar.gz
4. yum安裝依賴
yum install -y pcre-devel openssl-devel
5. 編譯安裝
```
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module
make
make install
```
6. 準備安裝用到的腳本
```
[root@localhost scripts]# cat /opt/server/scripts/nginx_install_before.sh
#!/bin/bash
useradd www -s /sbin/nologin -M
mkdir -p /etc/nginx
[root@localhost scripts]# cat /opt/server/scripts/nginx_install_after.sh
#!/bin/bash
ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
ln -s /usr/local/nginx/conf/nginx.conf /etc/nginx
```
7. 打包rpm,默認輸出到當前路徑
```
fpm -s dir -t rpm -n nginx -v 1.16.1 -d 'pcre-devel,openssl-devel' --before-install /opt/server/scripts/nginx_install_pre.sh --after-install /opt/server/scripts/nginx_install_post.sh -f /usr/local/nginx/
```
## rpm包的使用方法
1. rpm命令安裝
[root@localhost~]# rpm -ivh nginx-1.16.1-1.x86_64.rpm
error: Failed dependencies:
pcre-devel is needed by nginx-1.16.1-1.x86_64
openssl-devel is needed nginx-1.16.1-1.x86_64
但會報如上依賴錯誤,需要先yum安裝依賴才能安裝rpm包。
2. yum命令安裝rpm包
yum -y localinstall nginx-1.16.1-1.x86_64.rpm
這個命令會自動先安裝rpm包的依賴,然后再安裝rpm包。
3. 離線網絡下需要先搭建本地YUM源