制作nginx的RPM安装包
前言
本教程制作的RPM安装包只适用于Redhat7.X版本的操作系统。nginx官方已经从2020年不在维护Redhat6.X版本。
nginx官方的RPM包维护不及时的情况下,需要我们手动制作,一般情况下还是使用官方的比较好。
Redhat7一下的操作系统,推荐源码编译安装,后续有时间再出一篇Redhat6.X的源码编译教程。
准备
需要安装 make gcc gcc-c++ openssl openssl-devel pcre-devel zlib-devel
请自行安装上述所有依赖,下面地址是Redhat7.6的rpm库
需要下载官方源码包,制作RPM时会用到,下载出来的是一个tar.gz文件
spec文件编写
生成rpmbuild目录
执行下面命令会生成rpmbuild目录(nginx.spec文件无需存在,命令一定会执行失败,但是会生成目录)
rpmbuilld -bb nginx.spec
rpmbuild目录解释
[root@hecs-270930 rpmbuild]# tree .
.
├── BUILD //编译rpm包的临时目录
├── BUILDROOT //编译后生成的软件临时安装目录
├── RPMS //RPM包输出目录
│ └── x86_64
│ ├── nginx-1.23.1-el7.x86_64.rpm
│ └── nginx-debuginfo-1.23.1-el7.x86_64.rpm
├── SOURCES //打包使用的源码
│ └── nginx-1.23.1.tar.gz
├── SPECS //spec文件目录
│ └── nginx.spec
└── SRPMS //软件最终的rpm源码格式存放路径
└── nginx-1.23.1-el7.src.rpm
spec文件
Summary: High Performance Web Server
Name: nginx
Version: 1.23.1
Release: el7
License: GPL
Group: Applications/Server
Source: nginx-1.23.1.tar.gz
URL: http://nginx.org/
Distribution: Linux
Packager: yinqin <2576384103@qq.com>
BuildRoot: root/nginx-1.23.1-el7
BuildRequires: gcc,gcc-c++,pcre-devel,zlib-devel
%description
nginx [engine x] is a HTTP and reverse proxy server, as well as a mail proxy server
%post
id nginx &>/dev/null
if [ $? -ne 0 ];
then
useradd nginx
fi
echo "export PATH=/usr/local/nginx/sbin:$PATH" >> /etc/profile
source /etc/profile
declare -a filePaths
filePaths=(/var/log/nginx /var/run /var/cache/nginx)
for i in ${filePaths[*]}
do
if [ ! -d ${i} ];
then
mkdir ${i}
fi
done
%preun
userdel -r nginx
%prep
%setup -q
%build
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--conf-path=/etc/nginx/nginx.conf \
--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/run/nginx.lock \
--modules-path=/usr/lib64/nginx/modules \
--sbin-path=/usr/local/nginx/sbin/nginx \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--with-http_ssl_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-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-ipv6
make %{?_smp_mflags}
%install
make install DESTDIR=%{buildroot}
%files
/usr/local/nginx
/etc/nginx
spec文件解释
下面这部分是对制作的RPM包的一些描述,以及资源的声明
Summary: High Performance Web Server //摘要信息
Name: nginx //包名
Version: 1.23.1 //包的版本
Release: el7 //编译版本
License: GPL //开源协议
Group: Applications/Server //软甲分组
Source: nginx-1.23.1.tar.gz //源码文件,这里的文件名一定要和SOURCES下的文件一致
URL: http://nginx.org/ //软件官网
Distribution: Linux //发版标识
Packager: yinqin <2576384103@qq.com> //制作者的信息
BuildRoot: root/nginx-1.23.1-el7 //编译时的目录,可以自定义
BuildRequires: gcc,gcc-c++,pcre-devel,zlib-devel //编译软件需要的依赖
%description //%description一下的内容可以是软件信息的详细描述
nginx [engine x] is a HTTP and reverse proxy server, as well as a mail proxy server
下面这部分是对运行时的操作定义
%post //安装时执行的操作
id nginx &>/dev/null //这里判断nginx用户是否存在,不存在则创建
if [ $? -ne 0 ];
then
useradd nginx
fi
echo "export PATH=/usr/local/nginx/sbin:$PATH" >> /etc/profile //将nginx可执行文件添加到环境变量
source /etc/profile //刷新环境变量
declare -a filePaths
filePaths=(/var/log/nginx /var/run /var/cache/nginx)
for i in ${filePaths[*]} //这里是判断nginx运行时需要的目录是否存在,不存在则创建
do
if [ ! -d ${i} ];
then
mkdir ${i}
fi
done
%preun
userdel -r nginx //卸载时,删除nginx用户
%prep
%setup -q //构建BUILD环境,将解压源码压缩包到BUILD目录
%build
./configure \ //对编译打包时的参数设置和环境检查
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--conf-path=/etc/nginx/nginx.conf \
--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/run/nginx.lock \
--modules-path=/usr/lib64/nginx/modules \
--sbin-path=/usr/local/nginx/sbin/nginx \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--with-http_ssl_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-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-ipv6 \
--with-stream
make %{?_smp_mflags} //编译
%install
make install DESTDIR=%{buildroot} //编译安装
%files
/usr/local/nginx
/etc/nginx
制作rpm包
1、将上述的spec文件放入/root/rpmbuild/SPECS目录下,命名为nginx.spec,对其中的文件信息和版本信息做调整
2、将nginx源码放入/root/rpmbuild/SOURCES目录下
3、去SPECS目录,执行下面命令,制作rpm包
rpmbuild -bb nginx.spec
安装nginx
执行下面命令
rpm -ivh nginx-1.23.1-el7.x86_64.rpm
使用此spec文件制作的nginx,他的设置如下:
- 配置文件在/etc/nginx目录下
- 静态资源在/usr/local/nginx/html目录下
- 可执行文件在/usr/local/nginx/sbin目录下
补充说明
如果要支持nginx的stream模块,需要在spec文件下面加上--with-stream参数