极简 RPM 打包指南 - 实战 nginx

这篇文章根据 极简 RPM 打包指南,给出打包 nginx 的一个实例,软件的下载链接在 这里

手动安装

安装依赖

软件的编译依赖有很多,这里偷懒一下,直接根据官方 nginx 软件包中写的安装依赖进行安装。

dnf builddep nginx

编译安装

很简单,三板斧即可

root@e9b068b80d54 ~/nginx-1.18.0# ./configure --user=rpmbuild
--group=rpmbuild --prefix=/usr
--conf-path=/etc/nginx/nginx.conf --modules-path=/etc/nginx/modules/
--pid-path=/run/nginx.pid --lock-path=/run/nginx.lock
--error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log`

结果如下:

 Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr"
  nginx binary file: "/usr/sbin/nginx"
  nginx modules path: "/etc/nginx/modules"
  nginx configuration prefix: "/etc/nginx"
  nginx configuration file: "/etc/nginx/nginx.conf"
  nginx pid file: "/run/nginx.pid"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

make -j 8
make install DESTDIR=/tmp/nginx INSTALLDIRS=vendor

软件的安装文件如下:

/tmp/nginx
├── etc
│   └── nginx
│       ├── fastcgi.conf
│       ├── fastcgi.conf.default
│       ├── fastcgi_params
│       ├── fastcgi_params.default
│       ├── koi-utf
│       ├── koi-win
│       ├── mime.types
│       ├── mime.types.default
│       ├── nginx.conf
│       ├── nginx.conf.default
│       ├── scgi_params
│       ├── scgi_params.default
│       ├── uwsgi_params
│       ├── uwsgi_params.default
│       └── win-utf
├── run
├── usr
│   ├── HTML
│   │   ├── 50x.html
│   │   └── index.html
│   └── sbin
│       └── nginx
└── var
    └── log
        └── nginx

RPM 打包

源码

下载源代码,放在 ~/rpmbuild/SOURCES/ 文件夹中。

nginx.spec

Name:           nginx
Version:        1.18.0
Release:        1%{?dist}
Summary:        The nginx Web server

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


%description 
The "Hello World" program, done with all bells and whistles of a proper FOSS 
project, including configuration, build, internationalization, help files, etc.

%prep
tar -xf %{_sourcedir}/nginx-1.18.0.tar.gz -C %{_builddir}

%build
cd %{_builddir}/nginx-1.18.0/
./configure \
    --user=rpmbuild \
    --group=rpmbuild \
    --prefix=/usr \
    --conf-path=/etc/nginx/nginx.conf \
    --modules-path=/etc/nginx/modules/ \
    --pid-path=/run/nginx.pid \
    --lock-path=/run/nginx.lock \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \

%install
cd %{_builddir}/nginx-1.18.0/
make install DESTDIR=%{buildroot}/ INSTALLDIRS=vendor

%files 
/etc/nginx/*
/usr/html/*html
/usr/sbin/nginx
/var/log/nginx

%changelog

打包

将 nginx.spec 放在 ~/rpmbuild/SPECS/ 文件夹中

rpmbuild ~/rpmbuild/SPECS/nginx.spec

结果如下:

/home/rpmbuild/rpmbuild/
├── BUILD
│   └── nginx-1.18.0
├── BUILDROOT
├── RPMS
│   └── x86_64
│       └── nginx-1.18.0-1.fc33.x86_64.rpm
├── SOURCES
│   └── nginx-1.18.0.tar.gz
├── SPECS
│   └── nginx.spec
└── SRPMS
    └── nginx-1.18.0-1.fc33.src.rpm

安装

# 本地安装
root@e9b068b80d54 ~# dnf localinstall /home/rpmbuild/rpmbuild/RPMS/x86_64/nginx-1.18.0-1.fc33.x86_64.rpm 

# 查看软件包的内容
root@e9b068b80d54 ~# rpm -ql nginx
/etc/nginx/fastcgi.conf
/etc/nginx/fastcgi.conf.default
/etc/nginx/fastcgi_params
/etc/nginx/fastcgi_params.default
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/mime.types.default
/etc/nginx/nginx.conf
/etc/nginx/nginx.conf.default
/etc/nginx/scgi_params
/etc/nginx/scgi_params.default
/etc/nginx/uwsgi_params
/etc/nginx/uwsgi_params.default
/etc/nginx/win-utf
/usr/html/50x.html
/usr/html/index.html
/usr/lib/.build-id
/usr/lib/.build-id/4a
/usr/lib/.build-id/4a/3e03deb43ba3f2766de23c58294e58e057cf52
/usr/sbin/nginx
/var/log/nginx

# 测试功能是否正常
root@e9b068b80d54 ~# nginx -V
nginx version: nginx/1.18.0
built by gcc 10.2.1 20201125 (Red Hat 10.2.1-9) (GCC) 
configure arguments: --user=rpmbuild --group=rpmbuild --prefix=/usr --conf-path=/etc/nginx/nginx.conf --modules-path=/etc/nginx/modules/ --pid-path=/run/nginx.pid --lock-path=/run/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log

注意

生成的 rpm 包现在已经可以在你的机器上正常安装、卸载了,软件也能正常使用了。

如果你打算把软件包在其他机器上安装,那么记得补充好"Requires"字段。

如果你打算在其他机器上重新编译 src.rpm,记得补充好"BuildRequires"字段。

如果你打算打一个更规范的 rpm 包,请至少将打包教程的参考资料看一遍,然后再与官方的 rpm 包做一下对照,官方包的源代码可以在 这里 找到。

1赞

configure 脚本从系统路径中找到 pcre 和 zlib 的库位置了。当你使用自定义版本的 pcre 时,你才需要指定位置。