平常工做中,当咱们须要安装一些较新的软件包时,每每官方只提供了源码而没有提供针对咱们所使用的操做系统的安装包。nginx
在企业中须要大规模的安装这个软件时,就须要在每一次安装都编译安装一次。c++
固然除了编译安装外你还有一个选择,那就是本身制做一个安装包!git
可是通常状况下(以centos为例)使用rpmbuild
须要修改大量配置,过于繁琐了。github
若是长期须要打包你能够集成到Jenkins内。web
但若是只是临时或者偶尔须要打包,你就能够考虑使用FPM
。docker
本文将解说如何安装FPM
工具。并将以nginx为例使用工具FPM
在centos操做系统中打包rpm安装包。编程
最后教你如何将FPM
工具封装为docker镜像,以便于快速的搭建打包的环境。centos
FPM的官方项目地址:github.com/jordansisse…ruby
FPM支持建立除rpm、deb等等的多种类型的安装包bash
CentOS 7
Nginx-1.16.0
使用FPM打包以前,须要将被打包软件在被安装的环境下编译安装一次,以保证安装包适应环境。 因此打包流程大体是这样的:
1. 编译安装被打包软件
2. 将编译安装好的文件移动到打包上下文目录
3. 建立安装脚本、卸载脚本
4. 安装FPM工具
5. 打包
复制代码
本文以Nginx-1.16.0做为样例
$ wget https://nginx.org/download/nginx-1.16.0.tar.gz
复制代码
$ yum install -y epel-release
$ yum install -y \
gcc geoip-devel geoip libxslt-devel libxml2-devel gd-devel glibc-devel make openssl-devel pcre-devel zlib-devel jemalloc-devel curl gcc-c++ perl
复制代码
#先建立一个目录用于打包rpm,将nginx安装在这个目录下并保持文件结构
$ mkdir /fpm-build
$ tar zxf nginx-1.16.0.tar.gz
$ cd nginx-1.16.0
#开始编译安装
$ ./configure \
--user=www \
--group=www \
--build=CentOS7 \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_gzip_static_module \
--with-http_sub_module \
--with-ld-opt=-ljemalloc \
--with-http_flv_module \
--with-http_xslt_module \
--with-http_xslt_module=dynamic \
--with-http_geoip_module \
--with-http_geoip_module=dynamic \
--with-http_sub_module \
--with-http_dav_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_realip_module \
--with-http_addition_module \
--prefix=/fpm-build/etc/nginx \
--sbin-path=/fpm-build/usr/sbin/nginx \
--modules-path=/fpm-build/usr/lib64/nginx/modules \
--conf-path=/fpm-build/etc/nginx/nginx.conf \
--http-log-path=/fpm-build/var/log/nginx/access.log \
--error-log-path=/fpm-build/var/log/nginx/error.log \
--pid-path=/fpm-build/var/run/nginx.pid \
--lock-path=/fpm-build/var/run/nginx.lock \
--http-client-body-temp-path=/fpm-build/var/cache/nginx/client_temp \
--http-proxy-temp-path=/fpm-build/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/fpm-build/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/fpm-build/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/fpm-build/var/cache/nginx/scgi_temp \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-stream \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_geoip_module \
--with-stream_geoip_module=dynamic \
--with-stream_ssl_preread_module \
--with-compat \
--with-select_module \
--with-poll_module \
--with-threads \
--with-file-aio \
--with-http_image_filter_module=dynamic \
--with-pcre-jit
$ make && make install
#接下来建立给systemd读取的启动脚本
$ mkdir /fpm-build/usr/lib/systemd/system
$ cat /fpm-build/usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
#装完之后若是没有报错的话,就能在咱们指定的目录里看到nginx的文件了
$ ll /fpm-build
总用量 0
drwxr-xr-x 3 root root 19 7月 23 18:07 etc
drwxr-xr-x 4 root root 31 7月 23 18:07 usr
drwxr-xr-x 4 root root 28 7月 23 18:07 var
复制代码
#建立用于放置安装脚本与卸载脚本的目录
$mkdir /fpm-build/tmp
$ cd /fpm-build/tmp
复制代码
install_after.sh
#安装脚本的主要做用是建立运行应用所需的用户等等
$ cat install_after.sh
#!/usr/bin/env bash
#add user www
source /etc/rc.d/init.d/functions
getent group www > /dev/null || groupadd -r www
getent passwd www > /dev/null || useradd -r -g www -s /sbin/nologin www
复制代码
remove_after.sh
cat remove_after.sh
#!/usr/bin/env bash
source /etc/rc.d/init.d/functions
/usr/bin/rm -rf \
/usr/lib/systemd/system/nginx.service \
/usr/lib64/nginx/modules \
/usr/sbin/nginx
复制代码
FPM是一个用Ruby编程语言构建的工具,因此在安装FPM以前须要安装ruby
$ yum install rubygems ruby-devel rubugems-devel gcc rpm-build make -y
复制代码
$ gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
$ gem sources -l
https://gems.ruby-china.com
#更新gem版本
$ gem update --system
复制代码
$ gem install --no-ri --no-rdoc fpm
复制代码
$ fpm -s dir
-t rpm \ #选择打包的类型,能够是deb或rpm等等等等
-n nginx-wangsu \ #设定软件包的名称
-v 1.16.0 \ #设定软件包的版本
--iteration 1.el7 \ #设定rpm包的版本
-C /fpm-build \ #指定上下文目录
-p /root \ #指定rpm包生成的路径
--description 'Wangsu Nginx rpm For Centos7' \
--url 'www.wangsucloud.com' \
-d 'jemalloc >= 3.5.0,glibc >= 2.16' \
#-d用于设定nginx依赖的软件包,在使用yum安装的时候会根据这个配置解决依赖关系
-m 'laihehui<laihh@wangsu.com>' \ #设定打包人的我的信息
--post-install /root/ng/tmp/install_after.sh \ #设定rpm安装脚本
--post-uninstall /root/ng/tmp/remove_after.sh #设定rpm卸载脚本
#运行命令后出现下面的提示就表明成功打包完毕了
Created package {:path=>"/root/nginx-wangsu-1.16.0-1.el7.x86_64.rpm"}
复制代码
为了方便在任何环境都能快速打包,避免重复安装FPM,咱们能够把FPM作成一个docker镜像。
Dockerfile 请见: github.com/aosolao/FPM…
构建好的镜像请见: hub.docker.com/r/aosolao/f…
首先建立两个目录,一个目录存放了构建好的nginx,另一个是空目录用于存放构建好的rpm包。
例以下面的演示中,
/fpm-build/ng
中存放了编译后的nginx,
/fpm-build/rpm
为一个空目录
将两个目录挂载到镜像中,用镜像中的FPM来构建rpm包,
接下来开始构建
$ docker run -d \
-v /fpm-build/ng:/fpm-build/ng \
-v /fpm-build/rpm:/fpm-build/rpm \
aosolao/fpm:v1 \
fpm -s dir \
-t rpm \
-n nginx-wangsu \
-v 1.16.0 \
--iteration 1.el7 \
-C /fpm-build/ng \
-p /fpm-build/rpm \
--description 'Wangsu Nginx rpm For Centos7' \
--url 'www.wangsucloud.com' \
-d 'jemalloc >= 3.5.0,glibc >= 2.16' \
-m 'laihehui<laihh@wangsu.com>' \
--post-install /fpm-build/ng/tmp/install_after.sh \
--post-uninstall /fpm-build/ng/tmp/remove_after.sh
复制代码
最后查看宿主机的/fpm-build/rpm
路径就能看到构建好的rpm包了
$ ll /fpm-build/rpm
总用量 3.9M
-rw-r--r-- 1 root root 3.9M 7月 24 11:04 nginx-wangsu-1.16.0-1.el7.x86_64.rpm
复制代码
这样就大功告成啦