1.安装
rpmbuild命令用于创建软件的二进制包和源代码包。
yum install rpmbuild rpmdevtools -y
生成rpm工作目录:rpmdev-setuptree命令默认将在当前用户主目录下创建一个RPM构建根目录结构,如果需要改变次默认位置,可以修改配置文件:~/.rpmmacros中变量_topdir对应的值即可。
目录结构
[root@master ~]#tree rpmbuild/
rpmbuild/
├── BUILD #打包过程中的工作目录
├── RPMS #存放生成的二进制包
├── SOURCES #放置打包资源,包括源码打包文件和补丁文件等
├── SPECS #放置SPEC文档
└── SRPMS #存放生成的源码包
5 directories, 0 files
2.使用
SOURCES目录下下载源码安装包
[root@master ~/rpmbuild/SOURCES]#ls
[root@master ~/rpmbuild/SOURCES]#wget http://nginx.org/download/nginx-1.23.3.tar.gz
......
[root@master ~/rpmbuild/SOURCES]#ls
nginx-1.23.3.tar.gz
撰写spec文件:SPEC撰写是打包RPM的核心,也算是最难的一步,好在我们可以从参照一个简单的模板文件开始,在可以实现基本功能的基础上再一步一步的扩充文档内容,直至完全达到要求。下面是一个简单的SPEC文档,其中包括了一些说明信息(注:#后面的内容为说明信息),该SPEC文档是对一个测试的软件项目hellorpm写的,hellorpm软件包编译后仅有一个执行文件、一个手册文件和一个项目说文件。
https://www.cnblogs.com/274914765qq/p/4737803.html
[root@master ~/rpmbuild/SPECS]#cat nginx.spec
Summary: High Performance Web Server
Name: nginx
Version: 1.23.3
Release: el7
License: GPL
Group: Applications/Server
Source: nginx-1.23.3.tar.gz
URL: http://nginx.org/
Distribution: Linux
Packager: syhj <1520509800@qq.com>
BuildRoot: root/nginx-1.23.3-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
安装依赖
yum -y install make gcc gcc-c++ openssl openssl-devel pcre-devel zlib-devel
构建rpm包:-ba表示build all,即生成包括二进制包和源代码包的所有RPM包,如果正常的话,rpmbuild将正常退出,同时在RPMS目录和SRPMS目录中将生成对应的RPM包。
[root@master ~/rpmbuild/SPECS]#ls
nginx.spec
[root@master ~/rpmbuild/SPECS]#rpmbuild -ba nginx.spec
......
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd nginx-1.23.3
+ /usr/bin/rm -rf /root/rpmbuild/BUILDROOT/nginx-1.23.3-el7.x86_64
+ exit 0
查看rpm文件:x86的rpm包会在x86目录中,arm的包在aarch64目录中。
[root@master ~/rpmbuild]#cd RPMS/
[root@master ~/rpmbuild/RPMS]#ls
x86_64
[root@master ~/rpmbuild/RPMS]#cd x86_64/
[root@master ~/rpmbuild/RPMS/x86_64]#ll
total 1536
-rw-r--r-- 1 root root 370816 Dec 24 13:58 nginx-1.23.3-el7.x86_64.rpm
-rw-r--r-- 1 root root 1198204 Dec 24 13:58 nginx-debuginfo-1.23.3-el7.x86_64.rpm
......
[root@master ~/rpmbuild/SRPMS]#ls
nginx-1.23.3-el7.src.rpm
使用此spec文件制作的nginx,他的设置如下:
- 配置文件在/etc/nginx目录下
- 静态资源在/usr/local/nginx/html目录下
- 可执行文件在/usr/local/nginx/sbin目录下
安装nginx
[root@master ~/rpmbuild/RPMS/x86_64]#ll
total 1536
-rw-r--r-- 1 root root 370816 Dec 24 13:58 nginx-1.23.3-el7.x86_64.rpm
-rw-r--r-- 1 root root 1198204 Dec 24 13:58 nginx-debuginfo-1.23.3-el7.x86_64.rpm
[root@master ~/rpmbuild/RPMS/x86_64]#rpm -ivh nginx-1.23.3-el7.x86_64.rpm
Preparing... ################################# [100%]
Updating / installing...
1:nginx-1.23.3-el7 ################################# [100%]
[root@master ~/rpmbuild/RPMS/x86_64]#rpm -qa | grep nginx
nginx-1.23.3-el7.x86_64
编写nginx.service
[root@master ~]#cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile =/run/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecrReload=/bin/kill -s HUP
ExecrStop=/bin/kill -s QUIT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
启动nginx
chmod 754 /usr/lib/systemd/system/nginx.service
systemctl daemon-reload && systemctl start nginx.service && systemctl enable nginx.service
参考:
https://www.cnblogs.com/274914765qq/p/4737803.html
https://blog.csdn.net/zd147896325/article/details/119673178
http://www.jinbuguo.com/pkgmanager/redhat/rpmbuild.html
https://blog.csdn.net/weixin_45913746/article/details/126285688