FPM打包步骤
一、我们来先安装FPM:
# FPM是Ruby模块
yum -y install ruby rubygems ruby-devel
# 添加腾讯的Ruby仓库
gem sources -a http://gems.ruby-china.org/ #gem sources --add https://ruby.taobao.org/
# 移除原生的Ruby仓库
gem sources --remove http://rubygems.org/
# 安装fpm
gem install fpm #gem install fpm -v 1.3.0 -V
#开启yum缓存
[root@loaclhost~]#sed -i 's#keepcache=0#keepcache=1#g' /etc/yum.conf
==============================================
FPM常用参数:
-s:指定源类型
-t:指定目标类型,即想要制作为什么包
-n:指定包的名字
-v:指定包的版本号
-C:指定打包的相对路径
-d:指定依赖于哪些包
-f:第二次包时目录下如果有同名安装包存在,则覆盖它
-p:输出的安装包的目录,不想放在当前目录下就需要指定
--post-install:软件包安装完成之后所要运行的脚本; 同--offer-install
--pre-install:软件包安装完成之前所要运行的脚本; 同--before-install
--post-uninstall:软件包卸载完成之后所要运行的脚本;同--offer-remove
--pre-uninstall:软件包卸载完成之前所要运行的脚本; 同—before-remove
小技巧:'-e' 参数支持打包之前手动修改FPM自动生成的SPEC文件,然后再打包。不过注意CentOS5和CentOS6之间的rpm包不能混用,因为他们之间的glibc版本不同,动态库链接不上。
==============================================
二、fpm打包过程:
###Installing the nginx
wget http://nginx.org/download/nginx-1.11.8.tar.gz
# 1.创建用户
groupadd -r nginx
useradd -r -g nginx -s /bin/false -M nginx
# 2.解压
tar zxvf nginx-1.11.8.tar.gz -C /u01
# 3.进入源码目录
cd /u01/nginx-1.11.8
# 4.脚本制作
创建放置服务启动脚本的目录:
mkdir -pv /tmp/installdir/etc/rc.d/init.d
为nginx提供SysV init脚本:
vim /tmp/installdir/etc/rc.d/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
*FPM会实现脚本功能,有自定义选项需要启用该脚本
准备rpm安装后及卸载后所需要运行的脚本:
mkdir -pv /tmp/installdir/tmp
vim install_after.sh
#!/bin/bash
#add user nginx
source /etc/rc.d/init.d/functions
getent group nginx > /dev/null || groupadd -r nginx
getent passwd nginx > /dev/null || useradd -r -g nginx -s /sbin/nologin nginx
exit 0
#此脚本是在nginx安装好后可以检测系统上是否有nginx组和用户,如果没有就创建。
vim remove_after.sh
#!/bin/bash
#
source /etc/rc.d/init.d/functions
rm -rf /usr/local/nginx
rm -rf /etc/nginx
userdel nginx
exit 0
#此脚本是在卸载nginx时删除在安装时生成的各个目录及创建的用户
# 5.指定配置参数
./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre
# 6.使用多核编译
make -j24
mkdir /tmp/installdir
# 7.指定安装路径
make install DESTDIR=/tmp/installdir/
# 8.生成RPM
fpm -s dir -t rpm --vendor CentOS6.5 -n nginx -v 1.11.8 --iteration 1.el6 -d 'pcre-devel,openssl-devel' \
--category 'Development/Languages' -m 'rongxinxxxx' --url 'http://www.lnredone.com' --description 'nginx server' --license 'GPL.GPLv2+,GPLv' \
--post-install /tmp/installdir/tmp/install_after.sh --post-uninstall /tmp/installdir/tmp/remove_after.sh -C /tmp/installdir/ -p /u01 -e
三、查看打包信息
查看RPM包信息
rpm -qpi nginx-1.11.8-release.x86_64.rpm
查看rpm包中的内容:
rpm -qpl nginx-1.11.8-release.x86_64.rpm
查看rpm包的依赖
rpm -qpR nginx-1.11.8-release.x86_64.rpm
查看rpm执行的脚本
rpm -qp --scripts nginx-1.11.8-release.x86_64.rpm
================================================
fpm打包实例:
1.fpm打包Nginx
[root@localhost opt]# fpm -s dir -t rpm -n nginx -v 1.11.8 -d 'pcre-devel,openssl-devel' --post-install /opt/nginx_rpm.sh -f /opt/nginx/
no value for epoch is set, defaulting to nil {:level=>:warn}
no value for epoch is set, defaulting to nil {:level=>:warn}
Created package {:path=>"nginx-1.6.1-1.x86_64.rpm"}
2.fpm相对路径打包:
[root@localhost~]# fpm -s dir -t rpm -n opt -v 1.1.1.1 -d 'gcc,gcc+' -C ../opt/
no value for epoch is set, defaulting to nil {:level=>:warn}
no value for epoch is set, defaulting to nil {:level=>:warn}
Created package {:path=>"opt-1.1.1.1-1.x86_64.rpm"}
3.使用fpm将生成包指定到/tmp下:
[root@localhost ~]# fpm -s dir -t rpm -n ansible-v 1.1.1.1 -d 'gcc,gcc+' -f ansible -p /tmp/
no value for epoch is set, defaulting to nil {:level=>:warn}
no value for epoch is set, defaulting to nil {:level=>:warn}
Created package {:path=>"/tmp/ansible-1.1.1.1-1.x86_64.rpm"}
镜像同步公网YUM源:
centos官方标准源:
rsync://mirrors.ustc.edu.cn/centos/6/os/x86_64/
rsync://mirrors.ustc.edu.cn/centos/6/extras/x86_64/
rsync://mirrors.ustc.edu.cn/centos/6/updates/x86_64/
epel源:
rsync://mirrors.ustc.edu.cn/epel/6/x86_64/
命令:reposync
###报错
1.Need executable 'rpmbuild' to convert dir to rpm {:level=>:error}
解决方法:
[root@localhost]# yum install -y rpm-build
2.如果里面有gcc make的错误.
解决方法:
yum install -y gcc