如何把nginx源码包打包成一个rpm包

一、为什么要自己打包rpm

源码包指的是一个程序的源代码,由于部分软件只提供源码包,需要自己去编译,而后面想更快的去安装,只能自己打包rpm包了,源码编译可以把软件安装到指定的位置,可以更好的方便去管理。

源码编译安装分为以下几个步骤

  • 安装依赖包
  • 下载源码包
  • 解压源码包
  • 预编译./configure
  • 编译make
  • 安装make install

二、编译安装nginx

安装依赖环境

[root@localhost ~]# yum install pcre pcre-devel zlib zlib-devel openssl openssl gcc gcc-c++ wget net-tools bash-comp* unzip -y

下载nginx源码包

[root@localhost ~]# wget https://repo.huaweicloud.com/nginx/nginx-1.19.0.tar.gz

解压

[root@localhost ~]# tar xf nginx-1.19.0.tar.gz

预编译

[root@localhost ~]# cd nginx-1.19.0
[root@localhost nginx-1.19.0]# ./configure --prefix=/usr/local/nginx --with-stream

编译

[root@localhost nginx-1.19.0]# make

安装

[root@localhost nginx-1.19.0]# make install

启动服务

[root@localhost nginx-1.19.0]# /usr/local/nginx/sbin/nginx

查看进程和端口号

[root@localhost nginx-1.19.0]# ps -ef|grep nginx
root       4335      1  0 17:05 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody     4336   4335  0 17:05 ?        00:00:00 nginx: worker process
root       4338   1301  0 17:05 pts/0    00:00:00 grep --color=auto nginx
[root@localhost nginx-1.19.0]# netstat -ntlp|grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      4335/nginx: master

关闭防火墙和selinux

[root@localhost nginx-1.19.0]# systemctl stop firewalld && systemctl disable firewalld
[root@localhost nginx-1.19.0]# sed -i '/SELINUX/s/enforcing/disabled/g' /etc/selinux/config

访问测试

三、制作rpm包

下载fpm包

wget https://rubygems.org/downloads/fpm-1.4.0.gem

创建fpm目录并移动到fpm目录中

[root@localhost ~]# mkdir /home/fpm
[root@localhost ~]# mv fpm-1.4.0.gem /home/fpm/

安装依赖环境

[root@localhost ~]# yum -y install ruby rubygems ruby-devel rpm-build

查看gen的源

[root@localhost ~]# gem sources --list
*** CURRENT SOURCES ***

https://rubygems.org/

移除国外源,更换阿里源

[root@localhost ~]# gem sources --remove https://rubygems.org/
https://rubygems.org/ removed from sources
[root@localhost ~]# gem sources -a http://mirrors.aliyun.com/rubygems/
http://mirrors.aliyun.com/rubygems/ added to sources
[root@localhost ~]# gem sources --list
*** CURRENT SOURCES ***

http://mirrors.aliyun.com/rubygems/

安装当前目录下的所有gem文件

#先确认ruby版本
[root@localhost 3.0.0]# ruby -v
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux]
#当前fpm依赖ruby版本是2.4.0以上,yum安装的是2.0.4,如果报错需要自己手动安装升级ruby,这里不再过多阐述,如果不会的话欢迎下面评论留言。
[root@localhost fpm]# gem install *.gem

使用fpm打包

[root@localhost fpm]# fpm -s dir  -t rpm -n nginx -v 1.19.0 -d 'zlib,zlib-devel,pcre,pcre-devel,openssl,openssl-devel' -
f /usr/local/nginx/
Doing `require 'backports'` is deprecated and will not load any backport in the next major release.
Require just the needed backports instead, or 'backports/latest'.
no value for epoch is set, defaulting to nil {:level=>:warn}
no value for epoch is set, defaulting to nil {:level=>:warn}
Created package {:path=>"nginx-1.19.0-1.x86_64.rpm"}

fpm参数详解

  • -s:dir # 打目录
  • -t:rpm # 把目录打成rpm包
  • -n:nginx # 软件名字叫nginx
  • -v:1.19.0 # 软件的版本号
  • -d: # 指定nginx的依赖包
  • -f: # 指定要达成rpm包的目录路径
  • --post-install # 指定rpm包安装完成之后要执行的脚本
  • --pre-install # 指定rpm包安装之前,要执行的脚本

查看生成的rpm包

四、安装测试

至此,rpm包打包完成。

发表评论

后才能评论