制作PHP的RPM包详解及实例

linuxPHPrpm包 2024-03-13

制作PHP的RPM包教程

有时候为了方便源码包的安装,我们需要自己订制软件包的需求,我们会把一些源码包按照我们的需求来做成rpm包,当有了源码包就可以直接编译得到二进制安装包和其他任意包。spec file是制作rpm包最核心的部分,rpm包的制作就是根据spec file来实现的。下面是我以制作php的rpm开始介绍其制作方法。以下操作在CentOS6.6 64位系统进行。

下面我们以制作php的rpm开始介绍其制作方法。以下操作在CentOS6.6 64位系统进行。

安装rpm-build

?
1[root@linuxeye.com SOURCES]# yum -y install rpm-build

建立工作车间目录

?
12[root@linuxeye.com SOURCES]# vim ~/.rpmmacros%_topdir /root/rpmbuild
?
1[root@linuxeye.com SOURCES]# mkdir -pv ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}

在redhat下,rpm包的默认制作路径在/usr/src/redhat下,但CentOS并没有该目录,因此,我们不得不自定义工作目录,这其中包含了6个目录(要求全部大写)

BUILD:源代码解压以后放的位置
RPMS:制作完成后的rpm包存放目录,为特定平台指定子目录(x86_64)
SOURCES:收集的源文件,源材料,补丁文件等存放位置
SPECS:存放spec文件,作为制作rpm包的领岗文件,以rpm名.spec
SRPMS:src格式的rpm包位置 ,既然是src格式的包,就没有平台的概念了
BuiltRoot:假根,使用install临时安装到这个目录,把这个目录当作根来用的,所以在这个目录下的目录文件,才是真正的目录文件。当打包完成后,在清理阶段,这个目录将被删除

?
12345678[root@linuxeye.com SOURCES]# rpmbuild --showrc | grep topdir #工作车间目录:_topdir /root/rpmbuild-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  /root/rpmbuild

rpmbuild --showrc显示所有的宏,以下划线开头,一个下划线:定义环境的使用情况,二个下划线:通常定义的是命令,为什么要定义宏,因为不同的系统,命令的存放位置可能不同,所以通过宏的定义找到命令的真正存放位置

收集源码文件脚本文件

?
1234[root@linuxeye.com SOURCES]# pwd/root/rpmbuild/SOURCES[root@linuxeye.com SOURCES]# lsphp-5.4.45.tar.gz

编写SPEC文件

?
123[root@linuxeye.com SPEC]# pwd /root/rpmbuild/SOURCES[root@linuxeye.com SPEC]# vim php.spec #内容如下:
?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186%define _user www%define _group www%define _prefix /usr/local/phpName: php #软件包名称Version: 5.4.45 #版本号(不能使用-)Release: 1%{?dist}  #release号,对应下面的changelog,如php-5.4.45-1.el6.x86_64.rpmSummary: PHP is a server-side scripting language for creating dynamic Web pages #简要描述信息,最好不要超过50个字符,如要详述,使用下面的%descriptionGroup: Development/Languages  #要全用这里面的一个组:less /usr/share/doc/rpm-version/GROUPSLicense: GPLv2 #软件授权方式URL: http://www.php.net #源码相关网站Packager: yeho <lj2007331@gmail.com> #打包人的信息Vendor: OneinStack #发行商或打包组织的信息Source0: %{name}-%{version}.tar.gz #源代码包,可以带多个用Source1、Source2等源,后面也可以用%{source1}、%{source2}引用BuildRoot: %_topdir/BUILDROOT #安装或编译时使用的“虚拟目录”Requires: libmcryptRequires: mhashRequires: mcryptRequires: 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-debugmake ZEND_EXTRA_LIBS='-liconv' %{?_smp_mflags} #%{?_smp_mflags} 的意思是:如果就多处理器的话make时并行编译%install #开始安装软件,如make installrm -rf %{buildroot}make INSTALL_ROOT=%{buildroot} installrm -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安装前执行的脚本%preecho '/usr/local/lib' > /etc/ld.so.conf.d/local.conf/sbin/ldconfigif [ $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:表示卸载#rpm安装后执行的脚本%postif [ $1 == 1 ];then[ -z "`grep ^'export ] && echo "export PATH=%{_prefix}/bin:\$PATH" >> /etc/profile[ -n "`grep ^'export -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 onMem=`free -m | awk '/Mem:/{print $2}'` #下面主要是参数的优化if [ $Mem -le 640 ];thenMem_level=512MMemory_limit=64elif [ $Mem -gt 640 -a $Mem -le 1280 ];thenMem_level=1GMemory_limit=128elif [ $Mem -gt 1280 -a $Mem -le 2500 ];thenMem_level=2GMemory_limit=192elif [ $Mem -gt 2500 -a $Mem -le 3500 ];thenMem_level=3GMemory_limit=256elif [ $Mem -gt 3500 -a $Mem -le 4500 ];thenMem_level=4GMemory_limit=320elif [ $Mem -gt 4500 -a $Mem -le 8000 ];thenMem_level=6GMemory_limit=384elif [ $Mem -gt 8000 ];thenMem_level=8GMemory_limit=448fised -i "s@^memory_limit.*@memory_limit = ${Memory_limit}M@" %{_prefix}/etc/php.inised -i 's@^output_buffering =@output_buffering = On\noutput_buffering =@' %{_prefix}/etc/php.inised -i 's@^;cgi.fix_pathinfo.*@cgi.fix_pathinfo=0@' %{_prefix}/etc/php.inised -i 's@^short_open_tag = Off@short_open_tag = On@' %{_prefix}/etc/php.inised -i 's@^expose_php = On@expose_php = Off@' %{_prefix}/etc/php.inised -i 's@^request_order.*@request_order = "CGP"@' %{_prefix}/etc/php.inised -i 's@^;date.timezone.*@date.timezone = Asia/Shanghai@' %{_prefix}/etc/php.inised -i 's@^post_max_size.*@post_max_size = 50M@' %{_prefix}/etc/php.inised -i 's@^upload_max_filesize.*@upload_max_filesize = 50M@' %{_prefix}/etc/php.inised -i 's@^;upload_tmp_dir.*@upload_tmp_dir = /tmp@' %{_prefix}/etc/php.inised -i 's@^max_execution_time.*@max_execution_time = 5@' %{_prefix}/etc/php.inised -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.inised -i 's@^session.cookie_httponly.*@session.cookie_httponly = 1@' %{_prefix}/etc/php.inised -i 's@^mysqlnd.collect_memory_statistics.*@mysqlnd.collect_memory_statistics = On@' %{_prefix}/etc/php.inicat > %{_prefix}/etc/php-fpm.conf <<EOF;;;;;;;;;;;;;;;;;;;;;; FPM Configuration ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Global Options ;;;;;;;;;;;;;;;;;;;[global]pid = run/php-fpm.piderror_log = log/php-fpm.loglog_level = warningemergency_restart_threshold = 30emergency_restart_interval = 60sprocess_control_timeout = 10sdaemonize = yes;;;;;;;;;;;;;;;;;;;;; Pool Definitions ;;;;;;;;;;;;;;;;;;;;;[%{_user}];listen = /dev/shm/php-cgi.socklisten = 127.0.0.1:9000listen.backlog = -1listen.allowed_clients = 127.0.0.1listen.owner = %{_user}listen.group = %{_group}listen.mode = 0666user = %{_user}group = %{_group}pm = dynamicpm.max_children = 12pm.start_servers = 8pm.min_spare_servers = 6pm.max_spare_servers = 12pm.max_requests = 2048pm.process_idle_timeout = 10srequest_terminate_timeout = 120request_slowlog_timeout = 0slowlog = log/slow.logrlimit_files = 51200rlimit_core = 0catch_workers_output = yes;env[HOSTNAME] = $HOSTNAMEenv[PATH] = /usr/local/bin:/usr/bin:/binenv[TMP] = /tmpenv[TMPDIR] = /tmpenv[TEMP] = /tmpEOFif [ $Mem -le 3000 ];thensed -i "s@^pm.max_children.*@pm.max_children = $(($Mem/2/20))@" %{_prefix}/etc/php-fpm.confsed -i "s@^pm.start_servers.*@pm.start_servers = $(($Mem/2/30))@" %{_prefix}/etc/php-fpm.confsed -i "s@^pm.min_spare_servers.*@pm.min_spare_servers = $(($Mem/2/40))@" %{_prefix}/etc/php-fpm.confsed -i "s@^pm.max_spare_servers.*@pm.max_spare_servers = $(($Mem/2/20))@" %{_prefix}/etc/php-fpm.confelif [ $Mem -gt 3000 -a $Mem -le 4500 ];thensed -i "s@^pm.max_children.*@pm.max_children = 80@" %{_prefix}/etc/php-fpm.confsed -i "s@^pm.start_servers.*@pm.start_servers = 50@" %{_prefix}/etc/php-fpm.confsed -i "s@^pm.min_spare_servers.*@pm.min_spare_servers = 40@" %{_prefix}/etc/php-fpm.confsed -i "s@^pm.max_spare_servers.*@pm.max_spare_servers = 80@" %{_prefix}/etc/php-fpm.confelif [ $Mem -gt 4500 -a $Mem -le 6500 ];thensed -i "s@^pm.max_children.*@pm.max_children = 90@" %{_prefix}/etc/php-fpm.confsed -i "s@^pm.start_servers.*@pm.start_servers = 60@" %{_prefix}/etc/php-fpm.confsed -i "s@^pm.min_spare_servers.*@pm.min_spare_servers = 50@" %{_prefix}/etc/php-fpm.confsed -i "s@^pm.max_spare_servers.*@pm.max_spare_servers = 90@" %{_prefix}/etc/php-fpm.confelif [ $Mem -gt 6500 -a $Mem -le 8500 ];thensed -i "s@^pm.max_children.*@pm.max_children = 100@" %{_prefix}/etc/php-fpm.confsed -i "s@^pm.start_servers.*@pm.start_servers = 70@" %{_prefix}/etc/php-fpm.confsed -i "s@^pm.min_spare_servers.*@pm.min_spare_servers = 60@" %{_prefix}/etc/php-fpm.confsed -i "s@^pm.max_spare_servers.*@pm.max_spare_servers = 100@" %{_prefix}/etc/php-fpm.confelif [ $Mem -gt 8500 ];thensed -i "s@^pm.max_children.*@pm.max_children = 120@" %{_prefix}/etc/php-fpm.confsed -i "s@^pm.start_servers.*@pm.start_servers = 80@" %{_prefix}/etc/php-fpm.confsed -i "s@^pm.min_spare_servers.*@pm.min_spare_servers = 70@" %{_prefix}/etc/php-fpm.confsed -i "s@^pm.max_spare_servers.*@pm.max_spare_servers = 120@" %{_prefix}/etc/php-fpm.conffifi#rpm卸载前执行的脚本%preunif [ $1 == 0 ];then/etc/init.d/php-fpm stop > /dev/null 2>&1/sbin/chkconfig --del php-fpmif [ -e '/etc/profile.d/custom_profile_new.sh' ];thensed -i 's@%{_prefix}/bin:@@' /etc/profile.d/custom_profile_new.shelsesed -i 's@%{_prefix}/bin:@@' /etc/profilefifi#%postun rpm卸载后执行的脚本%clean  #clean的主要作用就是删除BUILDrm -rf %{buildroot}%files #指定哪些文件需要被打包,如/usr/local/php%defattr(-,root,root,-)%{_prefix}%attr(0755,root,root) /etc/init.d/php-fpm%changelog #日志改变段, 这一段主要描述软件的开发记录* Sat Oct 24 2015 yeho <lj2007331@gmail.com> 5.4.45-1- Initial version

下面是php-redis.spec

?
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455[root@linuxeye.com SOURCES]# pwd/root/rpmbuild/SOURCES[root@linuxeye.com SOURCES]# lsredis-2.2.7.tgz[root@linuxeye.com SOURCES]# cd ../SPEC[root@linuxeye.com SPEC]# vim php-redis.spec%global php_extdir %(/usr/local/php/bin/php-config --extension-dir 2>/dev/null || echo "undefined")Name: php-redisVersion: 2.2.7Release: 1%{?dist}Summary: The phpredis extension provides an API for communicating with the Redis key-value store.Group: Development/LanguagesLicense: PHPURL: http://pecl.php.net/package/redisSource0: redis-%{version}.tgzBuildRoot: %_topdir/BUILDROOTRequires: phpBuildRequires: php >= 5.4.40%descriptionThe phpredis extension provides an API for communicating with the Redis key-value store.%prep%setup -q -n redis-%{version}%build/usr/local/php/bin/phpize%configuremake %{?_smp_mflags}%installrm -rf %{buildroot}mkdir -p %{buildroot}%{php_extdir}make install INSTALL_ROOT=%{buildroot}find %{buildroot} -name redis.so -exec /bin/mv {} %{buildroot}%{php_extdir} \;#rpm安装后执行的脚本%postif [ $1 == 1 ];then[ -z "`grep '^extension_dir' /usr/local/php/etc/php.ini`" ] && echo "extension_dir = \"%{php_extdir}\"" >> /usr/local/php/etc/php.inised -i 's@^extension_dir\(.*\)@extension_dir\1\nextension = "redis.so"@' /usr/local/php/etc/php.inifi#rpm卸载前执行的脚本%preunif [ $1 == 0 ];then/etc/init.d/php-fpm stop > /dev/null 2>&1sed -i '/redis.so/d' /usr/local/php/etc/php.inifi#%postun rpm卸载后执行的脚本if [ $1 == 0 ];then/etc/init.d/php-fpm start > /dev/null 2>&1fi%cleanrm -rf %{buildroot}%files%defattr(-,root,root,-)%{php_extdir}/redis.so%changelog* Sat Oct 24 2015 yeho <lj2007331@gmail.com> 2.2.7-1- Initial version

编译rpm包

?
12[root@linuxeye.com SPEC]# rpmbuild -bb php.spec 制作php rpm二进制包[root@linuxeye.com SPEC]# rpmbuild -bb php-redis.spec 制作php-redis rpm二进制包

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

常见的云安全错误认知以及应对方法!

随着多云及混合云趋势的发展,过去传统的云安全策略显然已不适应新的云环境。尽管,很多企业一直非常重视云安全问题,但其中很多风险点并没有得到实际解决。大多数企业依然在采用过去本地环境下的云安全措施,导致企业出现云安全策略不一致,应用风险和漏洞增加的状况!最严重的问...
服务器安全云安全

2021年对网络安全技能需求较大的10个领域

组织需要的安全技能有很多,而且还在不断增加。专家表示,网络攻击的发展正在推动安全技能的需求。Netflix公司DVD业务安全负责人、美国信息系统安全协会(ISSA)旧金山分会会长Jimmy Sanders表示,该公司在网络安全方面有很多的工作要做,因此他希望带...
服务器安全网络安全网络攻击网络安全技能

最新DataGrip2020.2.x破解版激活码的步骤详解(支持Mac/Windows/Linux)

datagrip是数据库管理工具,操作数据库非常方便!本教程提供了datagrip2020激活码、datagrip2020破解版和datagrip2020安装包,可以完美激活和破解所有2020版本(2020.2/2020 . 2 . 1/2020 . 2 . ...
数据库技术DataGripdatagrip激活码datagrip破解版

ELK elasticsearch cat API 介绍(5th)

回到ELK系列的第二篇,有一张图http://www.ttlsa.com/wp-content/uploads/2015/11/elk2.jpg,这上面的每列是啥含义? 还有其它的API接口么?每个API接口能查看到啥信息?elasticsearch默认是以j...
服务器运维ELkelasticsearch