本文适用:rhel8系列,或同类系统(CentOS8,AlmaLinux8,RockyLinux8等)
文档形成时期:2022-2023年
因系统版本不同,构建部署应略有差异,但本文未做细分,对稍有经验者应不存在明显障碍。
因软件世界之复杂和个人能力之限,难免疏漏和错误,欢迎指正。
文章目录
- 环境准备
- rpmbuild命令详解
- rpmbuild的spec文件说明
- nginx-1.24.0-el8.spec内容
- 构建
- 安装
环境准备
yum install rpmdevtools
#创建目录
rpmdev-setuptree
#或手动创建目录:
mkdir rpmbuild-nginx-1.24.0; cd rpmbuild-nginx-1.24.0
mkdir -p ./{BUILD,RPMS,SOURCES,SPECS,SRPMS}
#无论是使用rpmdev-setuptree创建目录,还是手动创建的,默认使用路径是/root/rpmbuild,除非使用参数--define "_topdir `pwd`"
,可在其它目录下构建。
rpmbuild命令详解
rpmbuild --help
-ba build source and binary packages from
-bb build binary package only from
rpmbuild的spec文件说明
参考:https://blog.csdn.net/qq_22418329/article/details/109983388
%pre和%post中的scriptlet分别在安装软件包之前和之后运行。脚本%preun和%postun在软件包卸载之前和之后运行。脚本%pretrans和%posttrans在事务的开始和结束时运行。
依赖包:
/root/software/{openssl-1.1.1w.tar.gz,pcre-8.45.tar.gz,zlib-1.2.13.tar.gz} 解压即可
源目录文件列表:
ls SOURCES/
nginx-1.24.0.tar.gz nginx.conf nginx.service phpinfo.php
nginx-1.24.0-el8.spec内容
Name: nginx
Version: 1.24.0
Release: custom%{?dist}
Summary: www.nginx.org
Group: GNU Linux Nginx Product
License: GPLv3+
URL: https://nginx.org/en/download.html
Source0: https://nginx.org/download/%{name}-%{version}.tar.gz
BuildRequires: gcc
Requires: perl perl-devel
%define debug_package %{nil}
%define _prefix /opt/nginx
Prefix: %{_prefix}
%description
nginx install
%prep
%setup -q
%build
./configure --prefix=%{_prefix} \
--user=www --group=www \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_flv_module \
--with-http_realip_module \
--with-http_ssl_module \
--with-http_v2_module \
--with-stream \
--with-pcre=/root/software/pcre-8.45 \
--with-openssl=/root/software/openssl-1.1.1w \
--with-zlib=/root/software/zlib-1.2.13
make %{?_smp_mflags}
#
# Installation section
#
%install
[ %{buildroot} != "/" ] && rm -rf %{buildroot}
make install DESTDIR=%{buildroot}
%__install -c -d -m 755 "%{buildroot}%{_prefix}/conf"
%__install -c -d -m 755 "%{buildroot}/usr/lib/systemd/system"
%__install -c -d -m 755 "%{buildroot}/opt/web/eg"
cp -f %_sourcedir/nginx.service "%{buildroot}/usr/lib/systemd/system/nginx.service"
cp -f %_sourcedir/nginx.conf "%{buildroot}%{_prefix}/conf/"
cp -f %_sourcedir/phpinfo.php "%{buildroot}/opt/web/eg/"
#
# Clean section
#
%clean
[ %{buildroot} != "/" ] && rm -rf "%{buildroot}"
#将所需要打包的文件都存放到这边中;-f %{name}.lang 加上后的意思为声明找到的文件
%files
%{_prefix}
%doc
/usr/lib/systemd/system/nginx.service
/opt/web/eg
#安装后执行的命令
%post
if [ $1 == 1 ];then
groupadd www -g 319 2> /dev/null
useradd -s /sbin/nologin -M www -u 319 -g 319 2> /dev/null
mkdir /home/www 2> /dev/null
chown www:www /home/www 2> /dev/null
chown www:www -R /opt/web/eg
systemctl daemon-reload
systemctl enable nginx
fi
#卸载前执行的命令
%preun
if [ "$1" = 0 ]
then
systemctl disable nginx
systemctl stop nginx
# userdel -r www
cp -r %{_prefix}/conf /opt/nginx_conf.rpmsave-`date +"%Y%m%d-%H%M%S"`
fi
%postun
if [ "$1" = 0 ]
then
systemctl disable nginx
rm -f /usr/lib/systemd/system/nginx.service
rm -rf /opt/nginx
echo "%{name}-%{version}-%{release} uninstalled."
fi
#软件更新说明
%changelog
* Thu Dec 14 2023 A
- For the first time, Custom made Nginx1.24.0 in AlmaLinux8.8.
构建
cd /root/rpmbuild-nginx-1.24.0
rpmbuild --define "_topdir `pwd`" --nodebuginfo -ba SPECS/nginx-1.24.0-el8.spec
构建后的主要文件:
安装
dnf localinstall nginx-1.24.0-custom.el8.x86_64.rpm