fpm介绍
项目地址:https://github.com/jordansissel/fpm
fpm的目标是使构建软件包(如rpm,deb,OSX软件包等)变得容易且快速。
简单说就是将一种类型的包转换成另一种类型。
安装fpm
-
fpm是ruby写的,因此系统环境需要ruby,CentOS 7需要ruby版本号大于2.2。
-
安装ruby模块
yum -y install ruby rubygems ruby-devel rpm-build
-
查看当前使用的rubygems仓库
gem sources list
-
添加淘宝的Rubygems仓库,外国的源慢,移除原生的Ruby仓库
gem sources --add https://ruby.taobao.org/ --remove http://rubygems.org/
-
安装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实例
-
创建文件夹
mkdir -p /opt/server/{pkg,scripts}
-
创建用户
useradd -s /sbin/nologin -M www
-
下载nginx源码包,解压
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar zxvf nginx-1.16.1.tar.gz
-
yum安装依赖
yum install -y pcre-devel openssl-devel
-
编译安装
./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
- 准备安装用到的脚本
[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
- 打包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包的使用方法
-
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包。
-
yum命令安装rpm包
yum -y localinstall nginx-1.16.1-1.x86_64.rpm
这个命令会自动先安装rpm包的依赖,然后再安装rpm包。
- 离线网络下需要先搭建本地YUM源