qq70191 发表于 2018-11-8 08:48:54

使用rpm-build制作nginx的rpm包

$ cd rpmbuild/SPECS/  
$ vim nginx.spec   #此时,里面就是一个模板,直接填就可以了
  

  
### 1.The introduction section
  

  
Name: nginx          # 软件包名称
  
Version: 1.7.7       # 版本号,(不能使用-)
  
Release: 3%{?dist}   # release号,对应下面的changelog,如 nginx-1.7.7-3.el6.x86_64.rpm
  
Summary: nginx-1.7.7.tar.gz to nginx-1.7.7.rpm   # 简要描述信息,最好不要超过50个字符,如要详述,使用下面的%description
  
Group: Applications/Archiving      # 要全用这里面的一个组:less /usr/share/doc/rpm-version/GROUPS
  
License: GPLv2                     # 一定带上(最好是对方源码包的License)BSD,GPL,GPLv2
  
URL: http://nmshuishui.blog.51cto.com/
  
Packager: nmshuishui
  
Vendor: nmshuishui
  
Source0: %{name}-%{version}.tar.gz   # source主要是引用一下自己定义好的脚本,配置文件之类的内容。
  
Source1: init.nginx                  # nginx在主配置文件里面做了很多优化,包括cpu抢占,各种缓存策略,tcp,进程数等。
  
Source2: nginx.conf                  # 每增加一个 Source ,都需要在 %install 段和 %files 段做相应配置,如果是启动脚本的话,最好在脚本段配置一下
  
Source3: fastcgi_params
  
BuildRoot: %_topdir/BUILDROOT
  

  
BuildRequires: gcc
  
Requires: openssl,openssl-devel,pcre-devel,pcre# 定义nginx依赖的包,需要yum安装
  

  
%description            # 软件包详述
  
Custom a rpm by yourself!Build nginx-1.7.7.tar.gz to nginx-1.7.7.rpm
  

  
###2.The Prep section 准备阶段,主要就是把源码包解压到build目录下,设置一下环境变量,并cd进去
  

  
%prep
  
%setup -q    # 这个宏的作用静默模式解压并cd
  

  
###3.The Build Section 编译制作阶段,这一节主要用于编译源码
  

  
%build
  

  
%configure          #在 RMP 创建时候, 由于 nginx 不按照常规定义, 不可以定义 %{_prefix} 之类参数, 也不可以使用 %configure 这个参数进行 rpm 编译
  
                  #一旦定义该参数, 会导致编译自动增加下面参数, 导致报错
  
                  # + ./configure --build=x86_64-redhat-linux-gnu --host=x86_64-redhat-linux-gnu --target=x86_64-redhat-linux-gnu --program-prefix=
  
                  #因此,这里需要 ./configure,且需把%configure删掉
  
                  #而且这里需要安装 pcre-devel包,如果没有的话,会提示关于pcre的错误,直接安装此包就可以了
  
./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}            # make后面的意思是:如果就多处理器的话make时并行编译
  

  
###4.Install section这一节主要用于完成实际安装软件必须执行的命令,可包含4种类型脚本
  

  
%install
  
rm -rf %{buildroot}
  
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                                                         # $1有3个值,代表动作,安装类型,处理类型
  
      /usr/sbin/useradd -r www -s /sbin/nologin 2> /dev/null            # 1:表示安装
  
fi                                                                        # 2:表示升级
  
                                                                            # 0:表示卸载
  
%post
  
if [ $1 == 1 ];then
  
      /sbin/chkconfig --add %{name}
  
      /sbin/chkconfig %{name} on
  
      echo '# Add#下面主要是内核参数的优化,包括tcp的快速释放和重利用等。
  
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 = 102465535' >> /etc/sysctl.conf
  
sysctl -p 2>&1 /dev/null
  
fi
  

  
%preun
  
if [ $1 == 0 ];then
  
      /usr/sbin/userdel -r www 2> /dev/null
  
      /etc/init.d/nginx stop > /dev/null 2>&1
  
fi
  
%postun
  

  
###5.clean section 清理段,clean的主要作用就是删除BUILD
  

  
%clean
  
rm -rf %{buildroot}
  

  
###6.file section 文件列表段,这个阶段是把前面已经编译好的内容要打包了,其中exclude是指要排除什么不打包进来。
  

  
%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
  

  
###7.chagelog section日志改变段, 这一段主要描述软件的开发记录
  

  
%changelog
  
*Thu Wed 26 2014 nmshuishui- 1.7.7-3
  
- Initial version


页: [1]
查看完整版本: 使用rpm-build制作nginx的rpm包