一、前提

       谈到 RPM 包,可能我们很多人都会说,RPM 包不就是执行 yum –y insyall 命令之后安装内容么,是的,没说错,可是,我们只知道安装的时候用 yum –y install ,但是我们是否知道 RPM 包是怎么来的么?我们都清楚,当我们在 Linux 服务器中安装软件时,有两种方式,一种是用 RPM 包安装,另外一种是自己编译源码包来安装,众所周知,编译源码包安装是异常的麻烦,而我们用 RPM 安装确实很简单的事,只需一条命令就 OK 了。但我们所用的 RPM 包可都是别人封装好的,说句不好听的话,谁知道它里面有没有病毒呢?于是,在一些比较注重安全的企业中,一般都会用自己封装的 RPM 包进行安装。到这里,也许有人会问,还有这种操作?对,没错,确实有这种操作,下面,我们就来一步步的说明 RPM 包的制作过程。

二、准备工作

       俗话说,兵马未动,粮草先行,这句话同样适合与我们的运维行业,活干的漂不漂亮,得看你工具准备的是否齐全,工具齐全,就算你是刚入门的菜鸟,很多时候也会事半功倍,工具准备的不齐全,就算是深入运维大坑的老鸟,很多时候也会事倍功半。

那么制作 RPM 包呢,首先我们就得最好制作前的准备:

1、安装 rpmdevtools 开发工具

[[email protected] ~]# yum -y install rpmdevtools

2、了解 rpmbuild 的用法

既然安装了 rpmdevtools 开发工具,我们就得知道它怎么用。其实我们在制作 RPM 包的过程中能用到的命令也就是 rpmbuild ,所在此,我们了解下 rpmbuild 的用法:

①、rpmbuild 的参数选项,如下表:

详述 RPM 包的制作过程

②、查询宏

在 RPM 报纸做过程中,会涉及到很多很多的宏,那么我们怎么样知道这些宏都代表了什么呢?我们可以使用 rpmbuild –showrc 命令来查看,如下图:

详述 RPM 包的制作过程

带%的全部为宏,当然我们可能看不懂,其实没关系,大部分都没用到,我们只需要了解个别几个就行,还有知道宏是用来干什么的就行。

③、宏变量的替换

如果我们不想使用 rpmbuild 系统自带的宏变量,那么我们可以对 rpmbuild 宏进行自定义,修改家目录下的 .rpmmacros 文件来实现,如图

详述 RPM 包的制作过程

④、创建工作目录

rpmbuild 必须要在特定的目录中工作,所以,我们得先创建 rpmbuild 的工作目录,使用 rpmdev-setuptree 命令创建,会在家目录下生成名为 rpmbuild 的目录,再执行 tree rpmbuild 来查看目录结构。

[[email protected] ~]$ rpmdev-setuptree
[[email protected]alhost ~]$ tree rpmbuild
rpmbuild
├── BUILD                                    #源代码解压后的存放目录
├── BUILDROOT                                  #RPM 包制作过程中临时安装目录
├── RPMS                                     #制作完成后的普通 RPM 包的存放目录
├── SOURCES                                   #所有收集的额外软件包的存放目录(包括配置文件、补丁包等等)
├── SPECS                                    #spec文件的存放目录
└── SRPMS                                    #制作完成后的 src 格式的 RPM 包的存放目录

三、RPM 包的制作过程

制作 rpm 包一般包含下面 4 个步骤:

1、设置好制作 rpm 包的目录结构(务必不能使用管理源进行,如果制作过程中,某个命令写错了,那结果可能是灾难性的)

2、将源文件放到特定的所规划好的目录当中

3、准备制作过程中所需要的其他文件,并放进特定的所规划好的目录当中

4、创建一个 spec 文件,spec 文件将决定如何对源文件进行编译

6、编译源代码,生成 rpm 包

四、spec 文件解析

spec 文件做为我们制作 RPM 包灵魂文件,如果而欧美让你不知道 spec 文件怎么写,那我们将无法制作 RPM 包,下面,我们就来讲下 spec 文件的组成。

spec 文件分为 7 个部分

1、软件包信息说明段(The introduction section)

详述 RPM 包的制作过程

字段说明,如上图:

Name:软件包名称,可以使用 %{name} 的方式引用

Version:软件包的版本号,务必使用 xx.yy,不能使用横线,后面也可使用 %{version} 的宏来进行引入

Release: 1%{?dist} ,软件的发布版本,dist 这个宏是可以判断的,如果 dist 这个宏有定义,就会在软件包中带上,如果没有定义,就不会带

Summary:软件包的简单描述,可以写制作者个人 信息

Group:软件包的属组,这个组不能随便乱写,在不同的系统上有不同的组,可以使用 cat /usr/share/doc/rpm-4.11.3/GROUPS 来查看有哪些组,如图

详述 RPM 包的制作过程

[[email protected] SPECS]$ cat /usr/share/doc/rpm-4.11.3/GROUPS
Amusements/Games(娱乐/游戏)
Amusements/Graphics(娱乐/图形)
Applications/Archiving(应用/文档)
Applications/Communications(应用/通讯)
Applications/Databases(应用/数据库)
Applications/Editors(应用/编辑器)
Applications/Emulators(应用/仿真器)
Applications/Engineering(应用/工程)
Applications/File(应用/文件)
Applications/Internet(应用/英特网)
Applications/Multimedia(应用/多媒体)
Applications/Productivity(应用/产品)
Applications/Publishing(应用/印刷)
Applications/System(应用/系统)
Applications/Text(应用/文本)
Development/Debuggers(开发/调试器)
Development/Languages(开发/语言)
Development/Libraries(开发/函数库)
Development/System(开发/系统)
Development/Tools(开发/工具)
Documentation(文档)
System Environment/Base(系统环境/基础)
System Environment/Daemons(系统环境/守护)
System Environment/Kernel(系统环境/内核)
System Environment/Libraries(系统环境/函数库)
System Environment/Shells(系统环境/接口)
User Interface/Desktops(用户界面/桌面)
User Interface/X(用户界面/X窗口)
User Interface/X Hardware Support(用户界面/X硬件支持)

License:授权方式,通常是GPL(*软件)或者GPLv2,BSD,这里一定要写清楚,否则可能会侵权

URL:一般写这个 RPM 可以从哪个地址下载到

Source0:源代码包的名字,也就是我们制作的这个 RPM 包的原始的语言文件,格式:%{name}-%{version}.tar.gz,也可以在前面加链接地址

BuildRoot:软件的临时安装根目录,临时安装完成之后打包 RPM 包时会从此目录选择文件进行打包,打包完成之后在清理阶段会将此目录删除

BuildRequires:定义依赖关系,制作 RPM 包的过程所依赖的其他的软件包

Requires:定义依赖关系,安装这个 RPM 包所以来的其他的软件包,定义依赖关系的时候,还可以指定版本号,比如:libpng-devel >= 1.0.20 zlib、bzip2 = %{version}, bzip2-libs =%{version}、perl(Carp)>=3.2

依赖包的格式

详述 RPM 包的制作过程

Packager:软件报的制作者,一般写 作者+<邮箱>

Vendor:制作者所属的公司或者提供商

%description:详细的描述信息,可以写多段,使用空白行隔开

2、准备阶段(The prep section)

详述 RPM 包的制作过程

%prep:设定 RPM 包的宏、工作环境等等

%setup:解压软件包并进入目录,这个红还有相关的选项可用,例如,在源代码解压后,假设 %setup 宏产生的目录为 %{name}-%{version},如果当初打包的源代码中的目录不是按照这种格式命名的,可以使用-n选项来指定切换目录:

%setup -q 将 tar 命令的频繁输出关闭。

%setup -n newdir 将压源代码程序软件包在 newdir 目录下解压。

%setup -c 在解压源代码程序软件包之前先创建目录。

%setup -bnum 在包含多个源代码程序软件包时,将第num个源程序解压缩。

%setup -T 不使用缺省的解压缩操作。

3、编译阶段(The build section)

详述 RPM 包的制作过程

%configure:是系统内置的宏

make %{?_smp_mflages}:%{?_smp_mflages} 判断有没有 smp 对称的多处理器的标志位,,如果有则使用,否则就不使用

也可以是

详述 RPM 包的制作过程

自定义 configure 和使用系统自带的 make 宏

4、安装阶段(The install section)

详述 RPM 包的制作过程

直接 make install 安装

DESTDIR=%{buildroot}:表示安装到什么地方,以谁为起点

也可以是

详述 RPM 包的制作过程

%{__rm} -rf %{buildroot}:每次制作过程可能会重复多遍,大多数情况下,每一遍重复会把上次安装的删除,本次在重新安装,为了避免之前安装的影响本次安装,所以先删除

如果不需要编译,这里留空,也可以使用 install 命令或者 copy 命令,install 命令比 copy 命令功能更强大、更易用,更容易做精确控制。关于 install 命令的使用,可以参考 man install 手册。

5、脚本段(The script section)

详述 RPM 包的制作过程

%pre:安装前,在 RPM 包的安装命令执行之前就要执行的脚本

%post:安装后,在 RPM 包的安装完成之后就要执行的脚本

%preun:卸载之前,卸载工作开始之前要执行的脚本

%postun:卸载之后,卸载完成之后要执行的脚本

BUILDROOT/ :所有在 BUILDROOT/ 路径下生成的文件除了 debug 文件之外统统都必须要做进 RPM 包

6、清理阶段(The clean section)

详述 RPM 包的制作过程

该段可有可无,就是删除此前的制作过程所用到的 buildroot

7、文件段(The files section),用于明确定义 RPM 包应该包含那些文件

详述 RPM 包的制作过程

所有 files 字段都必须以 %files 开头,files 字段底下的内容为包含进来的文件,例如:

详述 RPM 包的制作过程

%defattr(-, root, root, 0755):定义这些文件的默认权限

%doc:明确说明这些文件是做为文档使用的

%config:定义配置文件的路径及名称

/usr/local/bin/mysql:定义想包含的 buildroot 路径下的文件,如果想包含目录下所有文件,则写目录即可

8、版本日志段(The cheange log)

详述 RPM 包的制作过程

*:修改时间已经版本号

-:定义修改者


五、编写 SPEC 文件(这里我们以制作 nginx 的 RPM 包为例)

[[email protected] ~]$ cd rpmbuild/SPECS
[[email protected] ~]$  vim nginx.spec

详述 RPM 包的制作过程


这里只截取部分截图,以供参考,下面贴上最终 nginx.spec 文件

Name:        nginx        
Version:     1.14.2
Release:     7%{?dist}
Summary:     Http Server.

Group:      Applications/Internet
License:    BSD
URL:        http://nginx.org
        
Source0:    http://nginx.org/download/%{name}-%{version}.tar.gz
Source1:    nginx

BuildRoot:       %{_tmppath}/%{name}-%{version}-%{release}-root
BuildRequires:   pcre-devel,zlib-devel,openssl-devel,libxslt-devel,gd-devel
Requires:        pcre,openssl,gd
Requires(pre):   shadow-utils
Requires(post):  chkconfig
Requires(preun): chkconfig,initscripts
Provides:        webserver

Packager:      situxiaoyu
Vendor:        [email protected]

%description
This guide gives a basic introduction to nginx and describes some simple tasks that can be done with it. It is supposed that nginx is already installed on the reader’s machine. If it is not, see the Installing nginx page. This guide describes how to start and stop nginx, and reload its configuration, explains the structure of the configuration file and describes how to set up nginx to serve out static content, how to configure nginx as a proxy server, and how to connect it with a FastCGI application.

%prep
%setup -q


%build
./configure \
    --prefix=/usr/local/nginx \
    --sbin-path=/usr/sbin/nginx \
    --conf-path=/usr/local/nginx/config/nginx.conf \
    --error-log-path=/var/log/wwwlogs/nginx_error.log \
    --http-log-path=/var/log/wwwlogs/nginx_access.log \
    --pid-path=/usr/local/nginx/var/run/nginx.pid \
    --lock-path=/usr/local/nginx/var/lock/nginx.lock \
    --user=nginx \
    --group=nginx \
    --with-http_ssl_module \
    --with-http_stub_status_module \
    --with-http_gzip_static_module \
    --with-http_degradation_module \
    --with-http_v2_module \
    --with-http_sub_module \
    --with-pcre
make %{?_smp_mflags}

%pre
if [ $1 == 1 ];then
    useradd -s /bin/false -r nginx 2>/dev/null || :
fi
%post
if [ $1 == 1 ];then
    chkconfig --add %{name}
    service %{name} start
fi
%preun
if [ $1 == 0 ];then
    service %{name} stop >/dev/null 2>&1
    chkconfig --del %{name}
    userdel nginx
    groupdel nginx
fi
%postun
%{__rm} -rf /usr/local/nginx
%{__rm} -rf /etc/init.f/nginx
%{__rm} -rf /var/log/wwwlogs
%{__rm} -rf /var/log/nginx
%{__rm} -rf /var/run/nginx

%install
%{__rm} -rf %{buildroot}
make install DESTDIR=%{buildroot}
install -p -d -m 0755 %{buildroot}/var/log/wwwlogs
install -p -d -m 0755 %{buildroot}/var/run/nginx
install -p -d -m 0755 %{buildroot}/var/log/nginx
install -p -d -m 0755 %{buildroot}/usr/local/nginx/var/run
install -p -d -m 0755 %{buildroot}/usr/local/nginx/var/lock
install -p -D -m 0755 %{SOURCE1} %{buildroot}/etc/rc.d/init.d/nginx
install -p -D -m 0755 %{SOURCE1} %{buildroot}/etc/init.d/nginx

%clean
%{__rm} -rf %{buildroot}


%files
%defattr(-, root, root, 0755)
%doc LICENSE CHANGES README
%{_sbindir}/%{name}
%dir /var/run/nginx
%dir /var/log/nginx
%dir /usr/local/nginx/config
%config(noreplace) /usr/local/nginx/config/fastcgi.conf
%config(noreplace) /usr/local/nginx/config/fastcgi_params
%config(noreplace) /usr/local/nginx/config/koi-utf
%config(noreplace) /usr/local/nginx/config/koi-win
%config(noreplace) /usr/local/nginx/config/mime.types
%config(noreplace) /usr/local/nginx/config/nginx.conf
%config(noreplace) /usr/local/nginx/config/scgi_params
%config(noreplace) /usr/local/nginx/config/uwsgi_params
%config(noreplace) /usr/local/nginx/config/win-utf
%config(noreplace) /usr/local/nginx/config/fastcgi.conf.default
%config(noreplace) /usr/local/nginx/config/fastcgi_params.default
%config(noreplace) /usr/local/nginx/config/mime.types.default
%config(noreplace) /usr/local/nginx/config/nginx.conf.default
%config(noreplace) /usr/local/nginx/config/scgi_params.default
%config(noreplace) /usr/local/nginx/config/uwsgi_params.default
#%attr(0755, root, root) %{_initrddir}/%{name}
%attr(0755, root, root) /etc/rc.d/init.d/nginx
%attr(0755, root, root) /etc/init.d/nginx
/usr/local/nginx/html/50x.html
/usr/local/nginx/html/index.html

%changelog
* Mon Dec 17 2018 nginx.org  <[email protected]> - 1.14.2-1
- Install version

说明:此文件可根据个人安装需求进行自定义修改

六、构建RPM包(我们先进行分段执行)

1、执行 rpmbuild -bp nginx.spec(执行到%prep段),提示我们没有安装依赖包(rpmbuild 命令会自动检查依赖关系)

详述 RPM 包的制作过程

我们根据提示,安装所需的依赖包

[[email protected] ~]# yum -y install pcre-devel zlib-devel openssl-devel libxslt-devel gd-devel

2、依赖包安装完后,在执行一次 rpmbuild -bp nginx.spec 命令,发现正常了,没有报错

[[email protected] SPECS]$ rpmbuild -bp nginx.spec

详述 RPM 包的制作过程

3、上面执行正常,我们再执行 rpmbuild -bc nginx.spec 命令(执行到%build段),结果又报错了,如下图:

[[email protected] SPECS]$ rpmbuild -bc nginx.spec

详述 RPM 包的制作过程

如上图:

一般情况下,我们不会太在意图中 1 的部分的 nginx 选项问题,而是直接查找 2、3 部分的错误原因。但是,在这里,如果我们没有认真看的话,是很难确定为什么会报错,其实仔细看一下,发现之所以报错,就是因为我们写的 nginx 编译参数无效,删掉该无效参数即可。

4、删除报错中提示的参数,我们再执行 rpmbuild -bc nginx.spec 命令,发现一切正常,如下图:

详述 RPM 包的制作过程

5、既然上一步没有问题了,我们就开始进行下一步,执行 rpmbuild -bi nginx.spec 命令

详述 RPM 包的制作过程

发现上图所示错误,同第三步一样的分析方法,只是这里是找不到文件或目录,我们再检查下 nginx.spec 文件,发现我们的 %build 段 --sbin-path 路径定义错了,改成 --sbin-path=/usr/bin/nginx 即可

6、再次执行 rpmbuild -bi nginx.spec 命令

详述 RPM 包的制作过程

如上图,又报错了,提示存在未打包的文件,根据提示,未打包的均为配置文件,我们将提示中的文件全部添加到 nginx.spec 文件的 files 段中去,如下图

详述 RPM 包的制作过程

%dir:表示后面的内容是一个目录

%config:表示该文件为配置文件,文件被覆盖为新版本文件,原修改后的文件保存为*.rpmsave

noreplace:表示文件不被覆盖,新rpm包里的文件被保存为 *.rpmnew

7、添加完毕后,再执行一次上面的 rpmbuild -bi nginx.spec 命令

详述 RPM 包的制作过程

结果如上图,提示没有打包 /usr/local/nginx/html/50x.html、/usr/local/nginx/html/index.html 这两个文件,那么我们再把这两个文件添加到 nginx.spec 文件的 files 段中去

详述 RPM 包的制作过程

如上图,添加完后保存退出,重新执行 rpmbuild -bi nginx.spec 命令

详述 RPM 包的制作过程

我滴个乖乖,这次终于成功了,不容易啊,怀着激动的小心情,我们开始进行下一步,打包 RPM 包。

既然都已经成功了,那我们就开是打包 RPM 包吧,执行 rpmbuild -bb nginx.spec 命令

详述 RPM 包的制作过程

如上图所示,顺利完成,到 RPMS 目录中查看一下是否有生成 RPM 包

[[email protected] SPECS]$ cd ../RPMS/x86_64/
[[email protected] x86_64]$ ll

详述 RPM 包的制作过程

如 上图,发现有生成两个包,一个带 debuginfo ,一个不带,其实我们能用到的是没带 debuginfo 的那个包,带 debuginfo 的包是用来调试的,我们在此可以忽略。

8、RPM 包既然已经生成了,那我们安装一下呗,看看我们自己制作的 RPM 包能否安装成功

[[email protected] x86_64]# rpm -ivh nginx-1.14.2-1.el7.x86_64.rpm

详述 RPM 包的制作过程

可惜啊,如上图,安装成功,但是启动报错,根据报错内容,我们大概能知道 2 个问题

①、没有服务启动的脚本文件

②、启动服务的命令有误(命令有误,是因为没有启动脚本)

既然知道存在这两个问题,我们就好下手了,再次编辑 nginx.spec 文件

详述 RPM 包的制作过程

详述 RPM 包的制作过程

首先,如上两图,添加 source 源,并且安装(注意:这里再引用 Source 的时候,必须要大写,这是约定的,如果不大写,则会报错。当然,安装可不必写两条,有其中一条就行。),再如下图,将两个文件添加到 files 段中。

详述 RPM 包的制作过程

%attr:表示定义单个文件的属性,如果不写,就继承了默认属性

其次切换到 SOURCES 目录下编写服务启动的脚本文件(此时,还脚本文件可以随便命名,但是安装后,必须为 nginx。)

[[email protected] SOURCES]# vim nginx
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

# Author:   licess
# website:  https://lnmp.org

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/sbin/$NAME
CONFIGFILE=/usr/local/nginx/config/$NAME.conf
PIDFILE=/usr/local/nginx/var/run/$NAME.pid
if [ -s /bin/ss ]; then
    StatBin=/bin/ss
else
    StatBin=/bin/netstat
fi


case "$1" in
    start)
        echo -n "Starting $NAME... "

        if $StatBin -tnpl | grep -q nginx;then
            echo "$NAME (pid `pidof $NAME`) already running."
            exit 1
        fi

        $NGINX_BIN -c $CONFIGFILE

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    stop)
        echo -n "Stoping $NAME... "

        if ! $StatBin -tnpl | grep -q nginx; then
            echo "$NAME is not running."
            exit 1
        fi

        $NGINX_BIN -s stop

        if [ "$?" != 0 ] ; then
            echo " failed. Use force-quit"
            exit 1
        else
            echo " done"
        fi
        ;;

    status)
        if $StatBin -tnpl | grep -q nginx; then
            PID=`pidof nginx`
            echo "$NAME (pid $PID) is running..."
        else
            echo "$NAME is stopped."
            exit 0
        fi
        ;;

    force-quit|kill)
        echo -n "Terminating $NAME... "

        if ! $StatBin -tnpl | grep -q nginx; then
            echo "$NAME is is stopped."
            exit 1
        fi

        kill `pidof $NAME`

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    restart)
        $0 stop
        sleep 1
        $0 start
        ;;

    reload)
        echo -n "Reload service $NAME... "

        if $StatBin -tnpl | grep -q nginx; then
            $NGINX_BIN -s reload
            echo " done"
        else
            echo "$NAME is not running, can't reload."
            exit 1
        fi
        ;;

    configtest)
        echo -n "Test $NAME configure files... "

        $NGINX_BIN -t
        ;;

    *)
        echo "Usage: $0 {start|stop|restart|reload|status|configtest|force-quit|kill}"
        exit 1
        ;;

esac

说明:此配置文件必须按照 spec 文件的设置来修改。

最后再切换到 SPECS 目录,执行一次 rpmbuild -bb nginx.spec 命令

9、再次安装我们上一步生成的 RPM 包

[[email protected] x86_64]# rpm -ivh nginx-1.14.2-1.el7.x86_64.rpm

详述 RPM 包的制作过程

如上图,成功了,查看下 nginx 进程和端口是否存在

详述 RPM 包的制作过程

详述 RPM 包的制作过程

如上图,端口和服务均存在,安装正常。

10、Nginx 的 RPM 包整合 php

在实际环境中,我们往往要将 nginx 和 php 搭配使用,也就是传说的 lnmp 环境,但是我们之前所制作的 nginx 的 RPM 包是不能连接 php 的,因为我们没有对 php 的环境进行相关定义,因此我们需要重新制作 RPM 包,在本次制作中我们加入 params 文件。

编辑 params 文件

[[email protected] SPECS]$ vim ../SOURCES/nginx.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;

重新编辑 nginx.spec 文件,在特定的为止加入以下内容

Source2:        nginx.params

%install
mv %{buildroot}/usr/local/nginx/config/fastcgi_params %{buildroot}/usr/local/nginx/config/fastcgi_params.oldinstall -p -D -m 0644 %{SOURCE2} %{buildroot}/usr/local/nginx/config/fastcgi_params

%files
%config(noreplace) /usr/local/nginx/config/fastcgi_params.old
%attr(0655, root, root) /usr/local/nginx/config/fastcgi_params

最终完整版的 nginx.spec 文件粘贴如下:

Name:       nginx        
Version:       1.14.2
Release:       2%{?dist}
Summary:       Http Server.

Group:      Applications/Internet
License:      BSD
URL:        http://nginx.org
        
Source0:    http://nginx.org/download/%{name}-%{version}.tar.gz
Source1:    nginx
Source2:    nginx.params

BuildRoot:      %{_tmppath}/%{name}-%{version}-%{release}-root
BuildRequires:   pcre-devel,zlib-devel,openssl-devel,libxslt-devel,gd-devel
Requires:      pcre,openssl,gd
Requires(pre):   shadow-utils
Requires(post):   chkconfig
Requires(preun):   chkconfig,initscripts
Provides:      webserver

Packager:       situxiaoyu
Vendor:       [email protected]

%description
This guide gives a basic introduction to nginx and describes some simple tasks that can be done with it. It is supposed that nginx is already installed on the reader’s machine. If it is not, see the Installing nginx page. This guide describes how to start and stop nginx, and reload its configuration, explains the structure of the configuration file and describes how to set up nginx to serve out static content, how to configure nginx as a proxy server, and how to connect it with a FastCGI application.

%prep
%setup -q


%build
./configure \
    --prefix=/usr/local/nginx \
    --sbin-path=/usr/sbin/nginx \
    --conf-path=/usr/local/nginx/config/nginx.conf \
    --error-log-path=/var/log/wwwlogs/nginx_error.log \
    --http-log-path=/var/log/wwwlogs/nginx_access.log \
    --pid-path=/usr/local/nginx/var/run/nginx.pid \
    --lock-path=/usr/local/nginx/var/lock/nginx.lock \
    --user=nginx \
    --group=nginx \
    --with-http_ssl_module \
    --with-http_stub_status_module \
    --with-http_gzip_static_module \
    --with-http_degradation_module \
    --with-http_v2_module \
    --with-http_sub_module \
    --with-pcre
make %{?_smp_mflags}

%pre
if [ $1 == 1 ];then
    useradd -s /bin/false -r nginx 2>/dev/null || :
fi
%post
if [ $1 == 1 ];then
    chkconfig --add %{name}
    service %{name} start
fi
%preun
if [ $1 == 0 ];then
    service %{name} stop >/dev/null 2>&1
    chkconfig --del %{name}
    userdel ngix
    groupdel nginx
fi
%postun
%{__rm} -rf /usr/local/nginx
%{__rm} -rf /etc/init.f/nginx
%{__rm} -rf /var/log/wwwlogs
%{__rm} -rf /var/log/nginx
%{__rm} -rf /var/run/nginx

%install
%{__rm} -rf %{buildroot}
make install DESTDIR=%{buildroot}
install -p -d -m 0755 %{buildroot}/var/log/wwwlogs
install -p -d -m 0755 %{buildroot}/var/run/nginx
install -p -d -m 0755 %{buildroot}/var/log/nginx
install -p -d -m 0755 %{buildroot}/usr/local/nginx/var/run
install -p -d -m 0755 %{buildroot}/usr/local/nginx/var/lock
install -p -D -m 0755 %{SOURCE1} %{buildroot}/etc/rc.d/init.d/nginx
install -p -D -m 0755 %{SOURCE1} %{buildroot}/etc/init.d/nginx
mv %{buildroot}/usr/local/nginx/config/fastcgi_params %{buildroot}/usr/local/nginx/config/fastcgi_params.old
install -p -D -m 0644 %{SOURCE2} %{buildroot}/usr/local/nginx/config/fastcgi_params

%clean
%{__rm} -rf %{buildroot}


%files
%defattr(-, root, root, 0755)
%doc LICENSE CHANGES README
%{_sbindir}/%{name}
%dir /var/run/nginx
%dir /var/log/nginx
%dir /usr/local/nginx/config
%config(noreplace) /usr/local/nginx/config/fastcgi.conf
%config(noreplace) /usr/local/nginx/config/fastcgi_params
%config(noreplace) /usr/local/nginx/config/fastcgi_params.old
%config(noreplace) /usr/local/nginx/config/koi-utf
%config(noreplace) /usr/local/nginx/config/koi-win
%config(noreplace) /usr/local/nginx/config/mime.types
%config(noreplace) /usr/local/nginx/config/nginx.conf
%config(noreplace) /usr/local/nginx/config/scgi_params
%config(noreplace) /usr/local/nginx/config/uwsgi_params
%config(noreplace) /usr/local/nginx/config/win-utf
%config(noreplace) /usr/local/nginx/config/fastcgi.conf.default
%config(noreplace) /usr/local/nginx/config/fastcgi_params.default
%config(noreplace) /usr/local/nginx/config/mime.types.default
%config(noreplace) /usr/local/nginx/config/nginx.conf.default
%config(noreplace) /usr/local/nginx/config/scgi_params.default
%config(noreplace) /usr/local/nginx/config/uwsgi_params.default

#%attr(0755, root, root) %{_initrddir}/%{name}
%attr(0755, root, root) /etc/rc.d/init.d/nginx
%attr(0755, root, root) /etc/init.d/nginx
%attr(0655, root, root) /usr/local/nginx/config/fastcgi_params
/usr/local/nginx/html/50x.html
/usr/local/nginx/html/index.html

%changelog
* Tue Dec 18 2018 nginx.org  <[email protected]> - 1.14.2-2
- Install version

* Mon Dec 17 2018 nginx.org  <[email protected]> - 1.14.2-1
- Install version

经过以上不停的尝试踩坑与填坑,我想我们应该能够制作简单 RPM 包了,孰能生巧,做的多了,我们也就自然明白了

七、总结

RPM 包的制作,关键在于 spec 文件的编写,对于 spec 文件如何编写,我们刚开始不会,可以照着例子边写边测,一般来说,如果有问题,测试时系统都能给出比较明确的错误原因,只要我们仔细的去发现,当不能被表象所迷惑。