RPMBUILD 打包总结
1.打包准备
制作原理图有时候为了方便源码包的安装,和我们自己订制软件包的需求,我们会把一些源码包按照我们的需求来做成rpm包,当有了源码包就可以直接编译得到二进制安装包和其他任意包。spec file是制作rpm包最核心的部分,rpm包的制作就是根据spec file来实现的。在制作自定义rpm包的时候最好不要使用管理员进行,因为管理员权限过大,如果一个命令写错了,结果可能是灾难性的,而制件一个rpm包普通用户完全可以实现
1. 定义工作车间(可以通过rpmbuild --showrc | grep topdir查看当前的工作车间)
vim ~/.rpmmacros
%_topdir /root/myrpmbuild
wq!
2. 创建打包目录
mkdir -pv {BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}
3. 安装打包工具
yum -y install rpm-build
2.打包过程(以nginx打包实例来说明打包过程)
1. 下载源码包到~/SOURCES
cd ~/myrpmbuild/SOURCES
wget http://nginx.org/download/nginx-1.8.1.tar.gz
2. 编写nginx启动脚本(nginx.init放到~/myrpmbuild/SOURCES)
#!/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
#END Of nginx.init
3. fastcgi(同上)
fastcgi_params 参数
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
[root@localhost SOURCES]# ls
fastcgi_params init.nginx nginx-1.7.7.tar.gz nginx.conf
4. spec文件
cd rpmbuild/SPECS/
vim nginx.spec #此时,里面就是一个模板,直接填就可以了
Name: nginx
Version: 1.8.1
Release: 6%{?dist}
Summary: nginx-1.8.1.tar.gz rpmbuild to nginx.1.8.1.rpm
License: GPLv2
URL: http://nginx.org
Source0: %{name}-%{version}.tar.gz
Source1: init.nginx
Source2: nginx.conf # 每增加一个 Source ,都需要在 %install 段和 %files 段做相应配置,如果是启动脚本的话,最好在脚本段配置一下
Source3: fastcgi_params
Packager: zlx{chulinx@163.com}
Vendor: zlx
BuildRequires: gcc
Requires: openssl,openssl-devel,pcre-devel,pcre
%description
Custom a rpm by yourself!Build nginx-1.8.1.tar.gz to nginx-1.8.1.rpm
%prep
%setup -q
%build
./configure
./configure \
--prefix=/usr/local/nginx \
--user=www \
--group=www \
--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
make %{?_smp_mflags}
%install
rm -rf $RPM_BUILD_ROOT
%make_install DESTDIR=%{buildroot}
%{__install} -p -D -m 0755 %{SOURCE1} %{buildroot}/etc/rc.d/init.d/nginx
%{__install} -p -D %{SOURCE2} %{buildroot}/usr/local/nginx/conf/nginx.conf
%{__install} -p -D %{SOURCE3} %{buildroot}/usr/local/nginx/conf/fastcgi_params
%pre
if [ $1 == 1 ];then
/usr/sbin/useradd -s /bin/false -r nginx 2>/dev/null || :
fi
%post
if [ $1 == 1 ];then
/sbin/chkconfig --add %{name}
/sbin/chkconfig %{name} on
echo '
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000927000000
net.ipv4.tcp_max_orphans = 3276800
#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 1024 65535' >> /etc/sysctl.conf
sysctl -p 2>&1 /dev/null
fi
%preun
if [ $1 == 0 ];then
/etc/init.d/%{name} stop > /dev/null 2>&1
/sbin/chkconfig --del %{name}
/usr/sbin/userdel nginx 2>/dev/null || :
fi
%postun
%clean
rm -rf %{buildroot}
%files
%defattr(-,root,root,0755)
/usr/local/nginx/
%attr(0755,root,root) /etc/rc.d/init.d/nginx
%config(noreplace) /usr/local/nginx/conf/nginx.conf
%config(noreplace) /usr/local/nginx/conf/fastcgi_params
%doc
%changelog
* Tue Nov 29 2016 zlx <chulinx@163.com> - 1.8.1
- Initial version
5. rpm包制作
rpmbuild -ba nginx.spec 表示既制作二进制包又制作src格式包
6. 参考
网友评论