资讯动态

【RHCA+】源码包编译(.tar.gz转RPM)

发布时间:2026/10/20 1:42:24 来源:尧图企业网站定制
“ 目录RHCA - 6 软件仓库 - 26 源码包编译.tar.gz转RPM ”rpm包用SPECS内文件制作是最核心的文件夹普通用户即可编译不能用root用户操作制作rpm包本身不需要root权限并且可避免误操作[rootwuzuniao ~]# rpmbuild --showrc | grep topdir #查看默认定义的宏不同系统命令存放的位置可能不一样可以通过宏定义查找路径也可自己在用户家目录下面创建.rpmmacros文件来自定义宏-14: _builddir %{_topdir}/BUILD-14: _buildrootdir %{_topdir}/BUILDROOT-14: _rpmdir %{_topdir}/RPMS-14: _sourcedir %{_topdir}/SOURCES-14: _specdir %{_topdir}/SPECS-14: _srcrpmdir %{_topdir}/SRPMS-14: _topdir %{getenv:HOME}/rpmbuild[rootwuzuniao ~]#目录作用BUILD编译过程中产生的数据保存位置源代码解压后位置RPMS编译成功生成的RPM包保存位置SOURCES从SRPM包中解压出来的源码包*.tar.gz保存位置SPECS生成的设置文件的安装位置命名rpm名.specSRPMS放置SRPM包的位置BUILDROOT假根使用install临时安装到这个目录当作根用这个目录下是真正的目录文件。打包完成后将被删除参数作用--showrc查看所有的宏以下划线开头一个下划线定义环境的使用情况两个下划线定义的是命令-bp编译到%prep段-bc编译到%build段-bi编译到%install段相当于执行一次make install-bb编译仅生成RPM二进制包-bs编译仅生成SRPM源码包-ba编译同时生成RPM二进制包和SRPM源码包-bl-bi执行完成后检查BUILDROOT生成了文件但是没有被包含到%file段的报错一、rpm包制作原理图二、制作rpm包1、安装rpmbuild[rootwuzuniao ~]# yum -y install openssl openssl-devel libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed libtool zlib zlib-devel pcre pcre-devel patch #安装rpmbuild命令的依赖包[rootwuzuniao ~]# yum -y install rpm-build gcc make redhat-lsb-core libuuid-devel #rpmbuild命令默认没装安装2、切换到普通用户并创建目录[rootwuzuniao ~]# su - wuzuniao #切换到普通用户Last login: Tue Apr 4 22:15:42 CST 2023 on pts/0[wuzuniaowuzuniao ~]$ mkdir -pv ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS} #创建工作目录mkdir: created directory ‘/home/wuzuniao/rpmbuild’mkdir: created directory ‘/home/wuzuniao/rpmbuild/BUILD’mkdir: created directory ‘/home/wuzuniao/rpmbuild/RPMS’mkdir: created directory ‘/home/wuzuniao/rpmbuild/SOURCES’mkdir: created directory ‘/home/wuzuniao/rpmbuild/SPECS’mkdir: created directory ‘/home/wuzuniao/rpmbuild/SRPMS’[wuzuniaowuzuniao ~]$3、准备配置文件[wuzuniaowuzuniao SPECS]$ ll /home/wuzuniao/rpmbuild/SOURCES/ #下面为文件内容或获取方式total 1100-rwxrwxrwx. 1 wuzuniao wuzuniao 849 Apr 4 23:12 fastcgi_params-rwxrwxrwx. 1 wuzuniao wuzuniao 2648 Apr 4 23:11 init.nginx-rwxrwxrwx. 1 wuzuniao wuzuniao 1112403 Apr 4 23:06 nginx-1.23.4.tar.gz-rwxrwxrwx. 1 wuzuniao wuzuniao 2632 Apr 4 23:06 nginx.conf[wuzuniaowuzuniao SPECS]$① init.nginx#!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /usr/local/nginx/conf/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx/nginx.pid # pidfile: /usr/local/nginx/logs/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ $NETWORKING no ] exit 0 nginx/usr/local/nginx/sbin/nginx prog$(basename $nginx) #NGINX_CONF_FILE/etc/nginx/nginx.conf NGINX_CONF_FILE/usr/local/nginx/conf/nginx.conf [ -f /etc/sysconfig/nginx ] . /etc/sysconfig/nginx lockfile/var/lock/subsys/nginx make_dirs() { # make required directories usernginx -V 21 | grep configure arguments: | sed s/[^*]*--user\([^ ]*\).*/\1/g - options$nginx -V 21 | grep configure arguments: for opt in $options; do if [ echo $opt | grep .*-temp-path ]; then valueecho $opt | cut -d -f 2 if [ ! -d $value ]; then # echo creating $value mkdir -p $value chown -R $user $value fi fi done } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $Starting $prog: daemon $nginx -c $NGINX_CONF_FILE retval$? echo [ $retval -eq 0 ] touch $lockfile return $retval } stop() { echo -n $Stopping $prog: killproc $prog -QUIT retval$? echo [ $retval -eq 0 ] rm -f $lockfile return $retval } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $Reloading $prog: killproc $nginx -HUP RETVAL$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status /dev/null 21 } case $1 in start) rh_status_q exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest} exit 2 esac② fastcgi_paramsfastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name;③ nginx.conf#user nobody; worker_processes 4; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 40960; } http { include mime.types; default_type application/octet-stream; #log_format main $remote_addr - $remote_user [$time_local] $request # $status $body_bytes_sent $http_referer # $http_user_agent $http_x_forwarded_for; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} #include vhosts.conf; map $time_iso8601 $logdate { ~^(?ymd\\d{4}-\\d{2}-\\d{2}) $ymd; default date-not-found; } include vhosts/*.conf; # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} client_max_body_size 50m; client_body_buffer_size 60k; client_body_timeout 60; client_header_buffer_size 64k; client_header_timeout 60; error_page 400 /error/400.html; error_page 403 /error/403.html; error_page 404 /error/404.html; error_page 500 /error/500.html; error_page 501 /error/501.html; error_page 502 /error/502.html; error_page 503 /error/503.html; error_page 504 /error/504.html; error_page 505 /error/505.html; error_page 506 /error/506.html; error_page 507 /error/507.html; error_page 509 /error/509.html; error_page 510 /error/510.html; keepalive_requests 100; large_client_header_buffers 4 64k; reset_timedout_connection on; send_timeout 60; sendfile_max_chunk 512k; server_names_hash_bucket_size 256; } worker_rlimit_nofile 100000;④ nginx-1.23.4.tar.gz源码包nginx.conf也可在此包中获取下载地址http://nginx.org/download/nginx-1.23.4.tar.gz4、在SPECS目录下创建nginx.spec[wuzuniaowuzuniao ~]$ cd rpmbuild/SPECS/[wuzuniaowuzuniao SPECS]$ vim nginx.spec #默认打开有个模板### 1.The introduction section Name: nginx # 软件包名称 Version: 1.23.4 # 版本号不能使用-后面可使用%{version}引用 Release: 1%{?dist} # 发布序列号标明第几次打包后面可使用%{release}引用 Summary: .tar.gz to .rpm # 简要描述信息最好不要超过50个字符如要详述使用下面的%description Group: Applications/Archiving # 软件分组建议使用标准分组 License: GPLv2 # 软件授权方式通常就是GPL一定带上最好是对方源码包的LicenseBSDGPLGPLv2 URL:https://www.wuzuniao.cn #软件的主页 Packager: wuzuniao xpgwuzuniao.cn #打包者的信息 Vendor:wuzuniao #发行商或打包组织的信息 Source0: %{name}-%{version}.tar.gz # source主要是引用一下自己定义好的脚本配置文件之类的内容。 Source1: init.nginx # nginx在主配置文件里面做了很多优化包括cpu抢占各种缓存策略tcp进程数等。 Source2: nginx.conf # 每增加一个 Source 都需要在 %install 段和 %files 段做相应配置如果是启动脚本的话最好在脚本段配置一下 Source3: fastcgi_params BuildRoot: %_topdir/BUILDROOT #这个是安装或编译时使用的“虚拟目录”考虑到多用户的环境一般定义为%{tmppath}/{name}-%{version}-%{release}-root或%{tmppath}/%{name}-%{version}-%{release}-buildroot-%%__id_u} -n}.该参数非常重要因为在生成rpm的过程中执行make install时就会把软件安装到上述的路径中在打包的时候同样依赖“虚拟目录”为“根目录”进行操作。后面可使用$RPM_BUILD_ROOT 方式引用。 BuildRequires: gcc Requires: openssl,openssl-devel,pcre-devel,pcre # 定义nginx依赖的包需要yum安装 %description # 软件包详述 Custom a rpm by yourself!to rpm ### 2.The Prep section 准备阶段,主要就是把源码包解压到build目录下设置一下环境变量并cd进去 %prep %setup -q # 这个宏的作用静默模式解压并cd ### 3.The Build Section 编译制作阶段这一节主要用于编译源码 %build #%configure #在 RMP 创建时候, 由于 nginx 不按照常规定义, 不可以定义 %{_prefix} 之类参数, 也不可以使用 %configure 这个参数进行 rpm 编译 #一旦定义该参数, 会导致编译自动增加下面参数, 导致报错 # ./configure --buildx86_64-redhat-linux-gnu --hostx86_64-redhat-linux-gnu --targetx86_64-redhat-linux-gnu --program-prefix #因此这里需要 ./configure且需把%configure删掉 #而且这里需要安装 pcre-devel包如果没有的话会提示关于pcre的错误直接安装此包就可以了 ./configure \ --prefix/usr/local/nginx \ --userwww \ --groupwww \ --with-http_ssl_module \ --with-http_flv_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --http-client-body-temp-path/var/tmp/nginx/client/ \ --http-proxy-temp-path/var/tmp/nginx/proxy/ \ --http-fastcgi-temp-path/var/tmp/nginx/fcgi/ \ --http-uwsgi-temp-path/var/tmp/nginx/uwsgi \ --http-scgi-temp-path/var/tmp/nginx/scgi \ --with-pcre make %{?_smp_mflags} # make后面的意思是如果是多处理器make时并行编译 ### 4.Install section 这一节主要用于完成实际安装软件必须执行的命令可包含4种类型脚本 %install rm -rf %{buildroot} make install DESTDIR%{buildroot} %{__install} -p -D -m 0755 %{SOURCE1} %{buildroot}/etc/rc.d/init.d/nginx %{__install} -p -D %{SOURCE2} %{buildroot}/usr/local/nginx/conf/nginx.conf %{__install} -p -D %{SOURCE3} %{buildroot}/usr/local/nginx/conf/fastcgi_params %pre if [ $1 1 ];then /usr/sbin/useradd -r www -s /sbin/nologin 2 /dev/null fi # $1有3个值代表动作安装类型处理类型 # 0表示卸载 # 1表示安装 # 2表示升级 %post if [ $1 1 ];then /sbin/chkconfig --add %{name} /sbin/chkconfig %{name} on echo #下面是内核参数的优化包括tcp的快速释放和重利用等。 net.ipv4.tcp_max_syn_backlog 65536 net.core.netdev_max_backlog 32768 net.core.somaxconn 32768 net.core.wmem_default 8388608 net.core.rmem_default 8388608 net.core.rmem_max 16777216 net.core.wmem_max 16777216 net.ipv4.tcp_timestamps 0 net.ipv4.tcp_synack_retries 2 net.ipv4.tcp_syn_retries 2 net.ipv4.tcp_tw_recycle 1 net.ipv4.tcp_tw_reuse 1 net.ipv4.tcp_mem 94500000 915000000927000000 net.ipv4.tcp_max_orphans 3276800 #net.ipv4.tcp_fin_timeout 30 #net.ipv4.tcp_keepalive_time 120 net.ipv4.ip_local_port_range 1024 65535 /etc/sysctl.conf sysctl -p 21 /dev/null fi %preun if [ $1 0 ];then /usr/sbin/userdel -r www 2 /dev/null /etc/init.d/nginx stop /dev/null 21 fi %postun ### 5.clean section 清理段,clean的主要作用就是删除BUILD %clean rm -rf %{buildroot} ### 6.file section 文件列表段这个阶段是把前面已经编译好的内容要打包了,其中exclude是指要排除什么不打包进来。 %files %defattr(-,root,root,0755) /usr/local/nginx/ %attr(0755,root,root) /etc/rc.d/init.d/nginx %config(noreplace) /usr/local/nginx/conf/nginx.conf %config(noreplace) /usr/local/nginx/conf/fastcgi_params ### 7.chagelog section 日志改变段 这一段主要描述软件的开发记录 %changelog此图为nginx.spec 34行-39行介绍的报错5、制作rpm包[wuzuniaowuzuniao SPECS]$ rpmbuild -ba nginx.spec #同时制作二进制包和srpm包[wuzuniaowuzuniao SPECS]$ ll /home/wuzuniao/rpmbuild/SRPMS/ #验证srpm包total 1092-rw-rw-r--. 1 wuzuniao wuzuniao 1118053 Apr 4 23:36 nginx-1.23.4-1.el7.src.rpm[wuzuniaowuzuniao SPECS]$ ll /home/wuzuniao/rpmbuild/RPMS/x86_64/ #验证二进制包total 1340-rw-rw-r--. 1 wuzuniao wuzuniao 327016 Apr 4 23:36 nginx-1.23.4-1.el7.x86_64.rpm-rw-rw-r--. 1 wuzuniao wuzuniao 1043144 Apr 4 23:36 nginx-debuginfo-1.23.4-1.el7.x86_64.rpm[wuzuniaowuzuniao SPECS]$三、签名RPM包签名的时候失败以后更正作者Phil Zimmermann于1991年创建PGPPretty Good Privacy是最早的加密工具现已闭源由同一OpenPGPRFC 4880标准制作的开源工具GPGGNU Privacy Guard被广泛使用1、使用gpg生成签名密钥[rootwuzuniao ~]# gpg --gen-key #创建签名秘钥 gpg (GnuPG) 2.0.22; Copyright (C) 2013 Free Software Foundation, Inc. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Please select what kind of key you want: (1) RSA and RSA (default) (2) DSA and Elgamal (3) DSA (sign only) (4) RSA (sign only) Your selection? #默认即可 RSA keys may be between 1024 and 4096 bits long. What keysize do you want? (2048) #选择密钥长度默认 Requested keysize is 2048 bits Please specify how long the key should be valid. 0 key does not expire n key expires in n days nw key expires in n weeks nm key expires in n months ny key expires in n years Key is valid for? (0) 1y #有效期 Key expires at Thu 04 Apr 2024 08:43:20 PM CST Is this correct? (y/N) y #确认 GnuPG needs to construct a user ID to identify your key. Real name: wuzuniao #密钥名称 Email address: xpgwuzuniao.cn #邮箱 Comment: wuzuniao #备注 You selected this USER-ID: wuzuniao (wuzuniao) xpgwuzuniao.cn Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O #kay确认 You need a Passphrase to protect your secret key. #这里会弹框让输入密码直接输入即可这里用的xpgwuzuniao.cn We need to generate a lot of random bytes. It is a good idea to perform some other action (type on the keyboard, move the mouse, utilize the disks) during the prime generation; this gives the random number generator a better chance to gain enough entropy. We need to generate a lot of random bytes. It is a good idea to perform some other action (type on the keyboard, move the mouse, utilize the disks) during the prime generation; this gives the random number generator a better chance to gain enough entropy. gpg: /root/.gnupg/trustdb.gpg: trustdb created gpg: key 741F586A marked as ultimately trusted public and secret key created and signed. gpg: checking the trustdb gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model gpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1u gpg: next trustdb check due at 2024-04-04 pub 2048R/741F586A 2023-04-05 [expires: 2024-04-04] Key fingerprint E432 6029 58BE DD93 B8B3 D4FE 739C 7832 741F 586A uid wuzuniao (wuzuniao) xpgwuzuniao.cn sub 2048R/9FC866C5 2023-04-05 [expires: 2024-04-04] [rootwuzuniao ~]#2、查看生成的秘钥[rootwuzuniao ~]# gpg --list-keys #查看秘钥 /root/.gnupg/pubring.gpgpub 2048R/741F586A 2023-04-05 [expires: 2024-04-04]uid wuzuniao (wuzuniao) xpgwuzuniao.cnsub 2048R/9FC866C5 2023-04-05 [expires: 2024-04-04]3、导出公钥以供验证[rootwuzuniao ~]# gpg --export -a wuzuniao RPM-GPG-KEY-wuzuniao #导出公钥4、签名时报错无法继续[rootwuzuniao ~]# rpm --addsign /home/wuzuniao/rpmbuild/RPMS/x86_64/nginx-1.23.4-1.el7.x86_64.rpmrpm: /usr/bin/rpmsign: No such file or directory[rootwuzuniao ~]#参考https://blog.51cto.com/kling/1239571?articleABtest1https://blog.51cto.com/nmshuishui/1583117https://blog.csdn.net/weixin_42134818/article/details/116713025

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价 →
↑