概述
nginx官方库对于Centos7的打包停留在2024年5月39日,版本1.26-2 (Index of /packages/centos/7Server/x86_64/RPMS/),实际上,最新的稳定版本已经到了1.28.0
详细的change日志见: https://nginx.org/en/CHANGES-1.28, 如果您正为修复安全漏洞看到了这篇文章,恭喜您,这就是答案之门。
文末附有打包好的版本。
操作步骤
1. 准备打包环境
sudo yum install -y rpm-build rpmdevtools
rpmdev-setuptree
2. 创建RPM构建目录结构
cd ~/rpmbuild
mkdir -p SOURCES SPECS
3. 准备安装包
wget https://nginx.org/download/nginx-1.28.0.tar.gz -P ~/rpmbuild/SOURCES/
或者手动下载放置到 ~/rpmbuild/SOURCES/
4. 创建Nginx的systemd服务文件
vim ~/rpmbuild/SOURCES/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
5. 编写RPM spec文件
vim ~/rpmbuild/SPECS/nginx.spec
Name: nginx
Version: 1.28.0
Release: 1%{?dist}
Summary: Custom Nginx with SSL module
License: BSD
URL: https://nginx.org/
Source0: nginx-1.28.0.tar.gz
BuildRequires: gcc make pcre-devel zlib-devel openssl-devel
Requires: pcre zlib openssl
%description
Custom Nginx 1.28.0 with SSL module and systemd service.
%prep
%setup -q -n nginx-1.28.0
%build
./configure --prefix=/usr/share/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log \
--with-http_ssl_module \
--with-http_realip_module \
--with-threads \
--with-file-aio
make %{?_smp_mflags}
%install
make install DESTDIR=%{buildroot}
# 安装服务文件
mkdir -p %{buildroot}/usr/lib/systemd/system
install -m 644 %{_sourcedir}/nginx.service %{buildroot}/usr/lib/systemd/system/
%files
%defattr(-,root,root,-)
/usr/sbin/nginx
/etc/nginx/
/usr/lib/systemd/system/nginx.service
/var/log/nginx/
/usr/share/nginx/html/
# 可选的独立注释行
# Default HTML files and systemd service
%post
systemctl daemon-reload
%preun
if [ $1 -eq 0 ]; then
systemctl stop nginx
systemctl disable nginx
fi
%changelog
* Fri Apr 25 2025 yourname<youremail@outlook.com>
- Initial build of custom Nginx 1.28.0 with SSL
6. 构建rpm包
cd ~/rpmbuild/SPECS
rpmbuild -ba nginx.spec
生成的RPM包位于 ~/rpmbuild/RPMS/x86_64/。
安装,调优略,如果需要其他模块,请自行引入。