Nginx   发布时间:2022-05-10  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了RPM包制作之Spec文件详解大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

1.1 起手势 安装rpm-build

yum -y install rpm-build

 
新建一个新用户并切换到用户上(避免权限的问题)、建立工作车间目录(官网的说法。。。),并写进环境变量里。

[root]# useradd devops    #新建用户
[root]# su - devops       #切换用户
[devops]$ echo "%_topdir %(echo $HOME)/rpmbuild" >>  ~/.rpmmacros
[devops]$ mkdir -p ~/rpmbuild/{BUILD,RPMS,sourcES,Specs,SRPMS}

 
注意: 这几个新建的文件夹必须要求全部大写!全部大写!全部大写!
  
以下为目录所对应存放文件的解释:

 
Spec文件的宏定义:

 
rpmbuild --showrc显示所有的宏,以下划线开头:
 

  • 一个下划线:定义环境的使用情况,
  • 二个下划线:通常定义的是命令,
    为什么要定义宏,因为不同的系统,命令的存放位置可能不同,所以通过宏的定义找到命令的真正存放位置
     

1.2 理解Spec脚本中各个变量

rpm的配置文档还算是比较有调理性的,按照标准的格式整理一些信息,包括:软件基础信息,以及安装、卸载前后执行的脚本,对源码包解压、打补丁、编译,安装路径和文件引用等,其中需要注意的地方为:虚拟路径的位置,以及宏的定义。
 
spec脚本包括很多关键字,主要有:

  

1.3 制作开始RPM包

 

所需要打包的文件都放在如下目录:

[devops]$ pwd
$HOME/rpmbuild/sourcES

 
编写SPEC文件目录如下:

[devops]$ pwd
$HOME/rpmbuild/SPEC

 
以下为Nginx Spec file的实例:

cat Nginx.spec
%define _prefix /usr/local/Nginx    //预定义的prefix目录
%define _logpath /var/log/weblog    //预定义日志目录
Name: Nginx 
Version: 1.12.1
Release: 1%{?dist}
SumMary: The Nginx http and reverse proxy server
Group: Applications/System
License: GPLv2
URL: https://Nginx.org
Packager: Atlantis <XXX@XXX.com>
vendor: XXX-XXX
source0: %{name}-%{version}.tar.gz  //引用的源码文件
source1: Nginx.conf                 //引用配置文件
source2: Nginx                      //引用System-V风格的service服务
source3: Nginx.logrotate            //引用日志轮转的配置文件
buildroot: %_topdir/buildrOOT       //虚拟根目录
requires: libxslt-devel,openssl-devel,pcre-devel    //所依赖的软件包

%description
Nginx is the heart of the modern web, powering half of the world’s busiest sites and applications. The company's comprehensive application delivery platform combines load balancing, content caching, web serving, security controls, and monitoring in one easy-to-use software package.

%prep                               //编译前准备工作,这里指定的就是Setup,有条件也可以指定编译器
%setup -q

%build                              //编译参数,这个看到这里的人基本都懂,没啥讲的,最后一个参数可以使用并行编译: make -j 6
./configure \
  --user=Nginx \
  --group=Nginx \
  --prefix=%{_prefix} \
  --http-log-path=%{_logpath}/access.log \
  --error-log-path=%{_logpath}/error.log \
  --pid-path=/var/run/Nginx.pid \
  --with-http_dav_module \
  --with-http_flv_module \
  --with-http_realip_module \
      --with-http_addition_module \
  --with-http_xslt_module \
  --with-http_sub_module \
  --with-http_random_index_module \
  --with-http_degradation_module \
  --with-http_secure_link_module \
  --with-http_gzip_static_module \
  --with-http_ssl_module \
  --with-http_stub_status_module \
  --with-pcre \
  --with-threads \
  --with-stream \
  --with-ld-opt=-Wl,-E
make %{?_smp_mflags}

%install                            //安装步骤
rm -rf %{Buildroot}                 //保证虚拟根的干净
make install DESTDIR=%{Buildroot}   //install 到虚拟根
%{__install} -p -d -m 0755 %{Buildroot}%{_logpath}  //定义一个日志目录并赋予其权限,这个文件会在编译时自动生成,因此要声明
%{__install} -p -D -m 0644 %{sourcE1} %{Buildroot}%{_prefix}/conf/Nginx.conf //复制sourcE1中的文件到虚拟根中
%{__install} -p -D -m 0755 %{sourcE2} %{Buildroot}/etc/rc.d/init.d/Nginx //复制sourcE2中的文件到虚拟根中
%{__install} -p -D -m 0644 %{sourcE3} %{Buildroot}%{_prefix}/conf/Nginx.logrotate //复制sourcE3中的文件到虚拟根中

%pre                                //安装前准备操作
if [ $1 == 1 ]; then                // 这里的1为安装;0为卸载
        /usr/sbin/useradd -r Nginx -s /sbin/nologin 2> /dev/null
fi

%post                               //安装后准备操作
if [ $1 == 1 ]; then
        echo "export PATH=/usr/local/Nginx/sbin:$PATH" >> /etc/profile
        source /etc/profile
        cp %{_prefix}/conf/Nginx.logrotate /etc/logrotate.d/Nginx
fi

%preun                              //卸载前准备操作
if [ $1 == 0 ]; then
        /etc/init.d/Nginx stop 2>&1 /dev/null
        /usr/sbin/userdel -r Nginx 2> /dev/null
fi

%postun
if [ $1 == 0 ]; then                //卸载后准备操作
        rm -f /etc/logrotate.d/Nginx
fi

%clean
rm -rf %{Buildroot}

%files                              //定义rpm包安装时创建的相关目录及文件。在该选项中%defattr (-,root,root)一定要注意。它是指定安装文件的属性,分别是(mode,owner,group),-表示默认值,对文本文件是0644,可执行文件是0755。
%defattr(-,root,root,0755)
%{_prefix}
%dir /var/log/weblog
%attr(644,root,root) %{_prefix}/conf/Nginx.conf
%attr(755,root,root) /etc/rc.d/init.d/Nginx

%changelog
* Fri Feb 22 2019 <XXX@XXX> - 1.12.1-3
- Initial Version
- update Installtion
- Add Logrotate Feature
- Fix Uninstall Bug With logrotate

# End Of Nginx.spec

大佬总结

以上是大佬教程为你收集整理的RPM包制作之Spec文件详解全部内容,希望文章能够帮你解决RPM包制作之Spec文件详解所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: