centos7.6下制作nginx的rpm安装包

公司需要适配ARM64架构的机器,但nginx并没有提供ARM64的rpm包,于是要自己动手制作一个,记录一下备忘。

安装软件 rpm-build, make 和 gcc

sudo yum install rpm-build
sudo yum install make
sudo yum install gcc

在用户根目录下创建目录rpmbuild

mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
rpmbuild目录下包含 BUILD,RPMS,SOURCES,SPECS,SRPMS 五个子目录,分别用于放置不同类型的文件。
BUILD 编译之前,如解压包后存放的路径
BUILDROOT 编译后存放的路径
RPMS 打包完成后rpm包存放的路径
SOURCES 源包所放置的路径
SPECS spec文档放置的路径
SPRMS 源码rpm包放置的路径

把nginx源码包下载到 SOURCES 目录

wget http://nginx.org/download/nginx-1.17.2.tar.gz -o ~/rpmbuild/SOURCES/nginx-1.17.2.tar.gz

在 SOURCES 下创建systemctl 启动文件 nginx.service,内容如下

[Unit] 
    Description=nginx 
    After=network.target 
    
    [Service] 
    Type=forking 
    ExecStart=/usr/local/nginx/sbin/nginx 
    ExecReload=/usr/local/nginx/sbin/nginx -s reload 
    ExecStop=/usr/local/nginx/sbin/nginx -s quit
    PrivateTmp=true 
    
    [Install] 
    WantedBy=multi-user.target

在 SPECS 下创建rpm规范文件 nginx.spec 内容如下

Name: nginx
    Version: 1.17.2
    Release:        3%{?dast}
    Summary: sefon nginx rpm
    
    Group:  Applications/Archiving
    License: GPLv2
    URL:    http://www.nginx.org
    # Source0: http://nginx.org/download/nginx-1.17.2.tar.gz
    Source0: nginx-1.17.2.tar.gz
    Source1: nginx.service
    
    BuildRoot:  %_topdir/BUILDROOT
    BuildRequires:  gcc
    Requires: openssl,openssl-devel,pcre-devel,pcre
    
    %description
    nginx rpm make 
    
    
    %prep
    tar -zxf $RPM_SOURCE_DIR/nginx-1.17.2.tar.gz
    
    
    %build
    cd nginx-1.17.2
    ./configure \
    --prefix=/usr/local/nginx \
    --user=www \
    --group=www \
    --with-pcre \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \
    --pid-path=/var/run/nginx.pid \
    --lock-path=/var/lock/nginx.lock \
    --with-openssl=/home/sefon/openssl-1.0.2t \
    --with-http_ssl_module \
    --with-http_v2_module \
    --with-http_realip_module \
    --with-http_addition_module \
    --with-http_sub_module \
    --with-http_dav_module \
    --with-http_flv_module \
    --with-http_mp4_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_random_index_module \
    --with-http_secure_link_module \
    --with-http_stub_status_module \
    --with-http_auth_request_module \
    --with-http_image_filter_module \
    --with-http_slice_module \
    --with-mail \
    --with-threads \
    --with-file-aio \
    --with-stream \
    --with-mail_ssl_module \
    --with-stream_ssl_module \
    
    make %{?_smp_mflags}
    
    
    %install
    rm -rf %{buildroot}
    cd nginx-1.17.2
    make install DESTDIR=%{buildroot}
    %{__install} -p -D -m 0755 $RPM_SOURCE_DIR/nginx.service %{buildroot}/usr/lib/systemd/system/nginx.service

    
    %files
    %defattr(-,root,root,-)
    %config(noreplace) /usr/local/nginx/conf/*
    /usr/local/nginx/html/
    /usr/local/nginx/sbin/nginx
    /usr/local/nginx/sbin/nginx
    /usr/lib/systemd/system/nginx.service
    
    %dir /var/log/nginx

    
    %changelog

SPEC文件深度说明在这里

进入SPEC目录,开始制作rpm包
rpmbuild -bb nginx.spec

其它命令
rpmbuild -bp nginx.spec 制作到%prep段
rpmbuild -bc nginx.spec 制作到%build段
rpmbuild -bi nginx.spec 执行 spec 文件的 “%install” 阶段 (在执行了 %prep 和 %build 阶段之后)。这通常等价于执行了一次 “make install”
rpmbuild -bb nginx.spec 制作二进制包
rpmbuild -ba nginx.spec 表示既制作二进制包又制作src格式包

成功后在 RPMS 目录下会生成一个对应架构和版本的rpm包

安装和验证rpm包

~/rpmbuild.nginx/RPMS/aarch64/nginx-1.17.2-3.aarch64.rpm

rpm其它命令
    rpm -qi nginx  # 查询软件包信息
    rpm -ivh  rpm:PATH  #安装和验证软件包
    rpm -ivh  –force rpm:PATH  # 强制安装
    rpm -qa nginx #查询已安装的软件包

参考文章
https://www.ibm.com/developerworks/cn/linux/management/package/rpm/part3/index.html
https://wiki.centos.org/HowTos/SetupRpmBuildEnvironment
https://blog.csdn.net/xlh1991/article/details/41632723