一、基本概念
1. 官方未提供RPM包
2. 官方的RPM无法自定义
3. 大量源码包,希望提供统一的软件管理机制
二、软件打包流程
1.准备源码包软件
? 案例:以下载php7.4版本为案例:官方软件包下载地址:PHP: Downloads
2. 安装rpm-build
[root@module ~]# yum -y install rpm-build #安装软件
[root@module ~]# -ba xxx.spec #会提示失败,自动创建目录
[root@module ~]# ls /root/ #查看rpmbuild是否自动创建成功
3. 准备源码包软件
[root@module ~]# cp php-7.4.27.tar.gz /root/rpmbuild/SOURCES/ #将提前下载好的源码包拷贝到SOURCES目录
4. 编写编译配置文件
[root@module x86_64]# vim /root/rpmbuild/SPECS/php.spec #编写编译配置文件
%global __python /usr/local/bin/python3 #指定使用python3,根据python安装路径填写;默认是用的python2,编译过程中会有问题
%define _user www
%define _group www
%define _prefix /usr/local/php
Name: php #软件包名称
Version: 7.4.27 #版本号
Release: 1%{?dist} #release号,对应下面的changelog
Summary: PHP is a server-side scripting language for creating dynamic Web page #简要描述
Group: Development/Languages #要全用这里面的一个组:less /usr/share/doc/rpm-version/GROUPS
License: GPLv2 #软件授权方式
URL: http://www.php.net #源码相关网站
Packager: hfrm<huafeirenmo@mail.com> #打包人的信息
Vendor: OneinStack #发行商或打包组织的信息
Source0: %{name}-%{version}.tar.gz #源代码包,可以带多个用Source1、Source2等源,用%{source1}、%{source2}引用
BuildRoot: %_topdir/BUILDROOT #安装或编译时使用的“虚拟目录”
Requires: libmcrypt
Requires: mhash
Requires: mcrypt
Requires: libiconv #定义php依赖的包,需要yum安装(此处使用epel源)
%description ##软件包详述
PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.
%prep #软件编译之前的处理,如解压
%setup -q #这个宏的作用静默模式解压并cd
%build #开始编译软件
%configure --prefix=%{_prefix} --with-config-file-path=%{_prefix}/etc \
--with-fpm-user=%{_user} --with-fpm-group=%{_group} --enable-fpm --enable-fileinfo \
--with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \
--with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib \
--with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-exif \
--enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-inline-optimization \
--enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl \
--with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-ftp --enable-calendar \
--with-gettext --enable-zip --enable-soap --disable-ipv6 --disable-debug
make ZEND_EXTRA_LIBS='-liconv' %{?_smp_mflags} #如果就多处理器的话make时并行编译
%install #开始安装软件,如make install
rm -rf %{buildroot}
make INSTALL_ROOT=%{buildroot} install
rm -rf %{buildroot}/{.channels,.depdb,.depdblock,.filemap,.lock,.registry}
%{__install} -p -D -m 0755 sapi/fpm/init.d.php-fpm %{buildroot}/etc/init.d/php-fpm
%{__install} -p -D -m 0644 php.ini-production %{buildroot}/%{_prefix}/etc/php.ini
#rpm安装前执行的脚本
%pre
echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
/sbin/ldconfig
if [ $1 == 1 -a -z "`grep ^%{_user} /etc/passwd`" ]; then # $1有3个值,代表动作,安装类型,处理类型
groupadd %{_group} -g 10000 # 1:表示安装
useradd -u 10000 -g 10000 -m %{_user} # 2:表示升级
fi # 0:表示卸载
%post #rpm安装后执行的脚本
if [ $1 == 1 ];then
[ -z "`grep ^'export PATH=' /etc/profile`" ] && echo "export PATH=%{_prefix}/bin:\$PATH" >> /etc/profile
[ -n "`grep ^'export PATH=' /etc/profile`" -a -z "`grep '%{_prefix}' /etc/profile`" ] && sed -i "s@^export PATH=\(.*\)@export PATH=%{_prefix}/bin:\1@" /etc/profile
/sbin/chkconfig --add php-fpm
/sbin/chkconfig php-fpm on
Mem=`free -m | awk '/Mem:/{print $2}'` #下面主要是参数的优化
if [ $Mem -le 640 ];then
Mem_level=512M
Memory_limit=64
elif [ $Mem -gt 640 -a $Mem -le 1280 ];then
Mem_level=1G
Memory_limit=128
elif [ $Mem -gt 1280 -a $Mem -le 2500 ];then
Mem_level=2G
Memory_limit=192
elif [ $Mem -gt 2500 -a $Mem -le 3500 ];then
Mem_level=3G
Memory_limit=256
elif [ $Mem -gt 3500 -a $Mem -le 4500 ];then
Mem_level=4G
Memory_limit=320
elif [ $Mem -gt 4500 -a $Mem -le 8000 ];then
Mem_level=6G
Memory_limit=384
elif [ $Mem -gt 8000 ];then
Mem_level=8G
Memory_limit=448
fi
sed -i "s@^memory_limit.*@memory_limit = ${Memory_limit}M@" %{_prefix}/etc/php.ini
sed -i 's@^output_buffering =@output_buffering = On\noutput_buffering =@' %{_prefix}/etc/php.ini
sed -i 's@^;cgi.fix_pathinfo.*@cgi.fix_pathinfo=0@' %{_prefix}/etc/php.ini
sed -i 's@^short_open_tag = Off@short_open_tag = On@' %{_prefix}/etc/php.ini
sed -i 's@^expose_php = On@expose_php = Off@' %{_prefix}/etc/php.ini
sed -i 's@^request_order.*@request_order = "CGP"@' %{_prefix}/etc/php.ini
sed -i 's@^;date.timezone.*@date.timezone = Asia/Shanghai@' %{_prefix}/etc/php.ini
sed -i 's@^post_max_size.*@post_max_size = 50M@' %{_prefix}/etc/php.ini
sed -i 's@^upload_max_filesize.*@upload_max_filesize = 50M@' %{_prefix}/etc/php.ini
sed -i 's@^;upload_tmp_dir.*@upload_tmp_dir = /tmp@' %{_prefix}/etc/php.ini
sed -i 's@^max_execution_time.*@max_execution_time = 5@' %{_prefix}/etc/php.ini
sed -i 's@^disable_functions.*@disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,fsocket,popen@' %{_prefix}/etc/php.ini
sed -i 's@^session.cookie_httponly.*@session.cookie_httponly = 1@' %{_prefix}/etc/php.ini
sed -i 's@^mysqlnd.collect_memory_statistics.*@mysqlnd.collect_memory_statistics = On@' %{_prefix}/etc/php.ini
cat > %{_prefix}/etc/php-fpm.conf <<EOF
;;;;;;;;;;;;;;;;;;;;;
; FPM Configuration ;
;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;
; Global Options ;
;;;;;;;;;;;;;;;;;;
[global]
pid = run/php-fpm.pid
error_log = log/php-fpm.log
log_level = warning
emergency_restart_threshold = 30
emergency_restart_interval = 60s
process_control_timeout = 10s
daemonize = yes
;;;;;;;;;;;;;;;;;;;;
; Pool Definitions ;
;;;;;;;;;;;;;;;;;;;;
[%{_user}]
;listen = /dev/shm/php-cgi.sock
listen = 127.0.0.1:9000
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = %{_user}
listen.group = %{_group}
listen.mode = 0666
user = %{_user}
group = %{_group}
pm = dynamic
pm.max_children = 12
pm.start_servers = 8
pm.min_spare_servers = 6
pm.max_spare_servers = 12
pm.max_requests = 2048
pm.process_idle_timeout = 10s
request_terminate_timeout = 120
request_slowlog_timeout = 0
slowlog = log/slow.log
rlimit_files = 51200
rlimit_core = 0
catch_workers_output = yes
;env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
EOF
if [ $Mem -le 3000 ];then
sed -i "s@^pm.max_children.*@pm.max_children = $(($Mem/2/20))@" %{_prefix}/etc/php-fpm.conf
sed -i "s@^pm.start_servers.*@pm.start_servers = $(($Mem/2/30))@" %{_prefix}/etc/php-fpm.conf
sed -i "s@^pm.min_spare_servers.*@pm.min_spare_servers = $(($Mem/2/40))@" %{_prefix}/etc/php-fpm.conf
sed -i "s@^pm.max_spare_servers.*@pm.max_spare_servers = $(($Mem/2/20))@" %{_prefix}/etc/php-fpm.conf
elif [ $Mem -gt 3000 -a $Mem -le 4500 ];then
sed -i "s@^pm.max_children.*@pm.max_children = 80@" %{_prefix}/etc/php-fpm.conf
sed -i "s@^pm.start_servers.*@pm.start_servers = 50@" %{_prefix}/etc/php-fpm.conf
sed -i "s@^pm.min_spare_servers.*@pm.min_spare_servers = 40@" %{_prefix}/etc/php-fpm.conf
sed -i "s@^pm.max_spare_servers.*@pm.max_spare_servers = 80@" %{_prefix}/etc/php-fpm.conf
elif [ $Mem -gt 4500 -a $Mem -le 6500 ];then
sed -i "s@^pm.max_children.*@pm.max_children = 90@" %{_prefix}/etc/php-fpm.conf
sed -i "s@^pm.start_servers.*@pm.start_servers = 60@" %{_prefix}/etc/php-fpm.conf
sed -i "s@^pm.min_spare_servers.*@pm.min_spare_servers = 50@" %{_prefix}/etc/php-fpm.conf
sed -i "s@^pm.max_spare_servers.*@pm.max_spare_servers = 90@" %{_prefix}/etc/php-fpm.conf
elif [ $Mem -gt 6500 -a $Mem -le 8500 ];then
sed -i "s@^pm.max_children.*@pm.max_children = 100@" %{_prefix}/etc/php-fpm.conf
sed -i "s@^pm.start_servers.*@pm.start_servers = 70@" %{_prefix}/etc/php-fpm.conf
sed -i "s@^pm.min_spare_servers.*@pm.min_spare_servers = 60@" %{_prefix}/etc/php-fpm.conf
sed -i "s@^pm.max_spare_servers.*@pm.max_spare_servers = 100@" %{_prefix}/etc/php-fpm.conf
elif [ $Mem -gt 8500 ];then
sed -i "s@^pm.max_children.*@pm.max_children = 120@" %{_prefix}/etc/php-fpm.conf
sed -i "s@^pm.start_servers.*@pm.start_servers = 80@" %{_prefix}/etc/php-fpm.conf
sed -i "s@^pm.min_spare_servers.*@pm.min_spare_servers = 70@" %{_prefix}/etc/php-fpm.conf
sed -i "s@^pm.max_spare_servers.*@pm.max_spare_servers = 120@" %{_prefix}/etc/php-fpm.conf
fi
fi
%preun
if [ $1 == 0 ];then
/etc/init.d/php-fpm stop > /dev/null 2>&1
/sbin/chkconfig --del php-fpm
if [ -e '/etc/profile.d/custom_profile_new.sh' ];then
sed -i 's@%{_prefix}/bin:@@' /etc/profile.d/custom_profile_new.sh
else
sed -i 's@%{_prefix}/bin:@@' /etc/profile
fi
fi
#%postun rpm卸载后执行的脚本
%clean #clean的主要作用就是删除BUILD
rm -rf %{buildroot}
%files #指定哪些文件需要被打包,如/usr/local/php
/etc/php-fpm.conf.default
/etc/php-fpm.d/www.conf.default
%defattr(-,root,root,-)
%{_prefix}
%attr(0755,root,root) /etc/init.d/php-fpm
%changelog ##日志改变段, 这一段主要描述软件的开发记录
* Sat Oct 11 2 2022 hfrm<huafeirenmo@mail.com> 7.4.27-1
- Initial version
5. 过程中遇到的问题
问题1:error: Installed (but unpackaged) file(s) found:
error: Installed (but unpackaged) file(s) found:
/etc/php-fpm.conf.default
/etc/php-fpm.d/www.conf.default
RPM build errors:
Installed (but unpackaged) file(s) found:
/etc/php-fpm.conf.default
/etc/php-fpm.d/www.conf.default
#解决方案:
#RPM包的spec脚本中没有包含但又被安装的文件,把这个文件添加在%files段就可以了,如下
%file
/etc/php-fpm.conf.default
/etc/php-fpm.d/www.conf.default
问题2:No package oniguruma found?
# 编译安装 PHP 7.4 提示 No package oniguruma found
# 使用 --with-mbstring 参数,则需要安装 oniguruma
#oniguruma 是一个处理正则表达式库,php7.4 中,mbstring 正则表达式处理功能对这个包有依赖。所以,安
装php7.4前需要安装oniguruma和oniguruma-devel 两个软件包
#下载地址:https://pkgs.org/download/oniguruma 和 https://pkgs.org/download/oniguruma-devel
#方法1:一般安装epel 然后安装即可:
[root@module ~]# yum install -y epel-release
[root@module ~]# yum install -y oniguruma oniguruma-devel
#方法2:行手动安装/更新epel:
[root@module ~]# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
[root@module ~]# yum install -y https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
[root@module ~]# yum install -y oniguruma oniguruma-devel
#方法3:Centos7,直接执行以下命令即可安装:
[root@module ~]# yum -y install https://rpms.remirepo.net/enterprise/7/remi/x86_64/oniguruma5php-6.9.6-1.el7.remi.x86_64.rpm
[root@module ~]# yum -y install https://rpms.remirepo.net/enterprise/7/remi/x86_64/oniguruma5php-devel-6.9.6-1.el7.remi.x86_64.rpm
#方法4:如果报冲突
[root@module ~]# yum remove epel-release #如果报冲突,先把冲突的包删除
[root@module ~]# rpm -ivh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@module ~]# yum install -y oniguruma oniguruma-devel
[root@module ~]# yum clean all && yum makecache
[root@module ~]# yum repolist all #检查 epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 是否启用
问题3:Bad exit status from /var/tmp/rpm-tmp.CwPoYe (%install)
error: Bad exit status from /var/tmp/rpm-tmp.CwPoYe (%install)
RPM build errors:
Bad exit status from /var/tmp/rpm-tmp.CwPoYe (%install)
#处理方式在:spec文件最上方添加
%global __python /usr/bin/python3
原因:生成python二进制文件的时候使用了python2
问题4:
编译安装libiconv库
[root@module ~]# wget https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.16.tar.gz #下载软件包
[root@module ~]# cd libiconv-1.16 #进入目录
[root@module ~]# mkdir /root/liconv
[root@module ~]# ./configure --prefix=/root/liconv
[root@module ~]# make && make install #源码方式安装
[root@module ~]# cp -f /root/liconv/libcharset.so.1 /usr/lib #拷贝库文件
[root@module ~]# cp -f /root/liconv/libiconv.so.2 /usr/lib
[root@module ~]# ln -s /usr/lib/libiconv.so.2 /usr/lib/libiconv.so #该库链接到/usr/lib/libiconv.so
若还是提示cannot find -liconv
[root@module ~]# vim /root/rm.sh #编写脚本文件,目的删除-static
#!/bin/sh
echo "begin"
gcc -static -o mtserver mtsocket.c current.c ini.c dcmscomm.c public.c sqlite3.c cjson.c config.c db.c dbcd.c dcms01dev.c power.c common.c mtserver.c -lm -liconv -lrt -ldl -lpthread
echo "cp to nfs .."
cp mtserver ../../nfs
echo "end"
|