欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

制作PHP的RPM包详解及实例

程序员文章站 2023-11-11 16:04:10
制作php的rpm包教程 有时候为了方便源码包的安装,我们需要自己订制软件包的需求,我们会把一些源码包按照我们的需求来做成rpm包,当有了源码包就可以直接编译得到二进制安...

制作php的rpm包教程

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

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

安装rpm-build

[root@linuxeye.com sources]# yum -y install rpm-build

建立工作车间目录

[root@linuxeye.com sources]# vim ~/.rpmmacros
%_topdir /root/rpmbuild
[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临时安装到这个目录,把这个目录当作根来用的,所以在这个目录下的目录文件,才是真正的目录文件。当打包完成后,在清理阶段,这个目录将被删除

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

收集源码文件脚本文件

[root@linuxeye.com sources]# pwd
/root/rpmbuild/sources
[root@linuxeye.com sources]# ls
php-5.4.45.tar.gz

编写spec文件

[root@linuxeye.com spec]# pwd 
/root/rpmbuild/sources
[root@linuxeye.com spec]# vim php.spec #内容如下: 
%define _user www
%define _group www
%define _prefix /usr/local/php
name: php #软件包名称
version: 5.4.45 #版本号(不能使用-)
release: 1%{?dist}  #release号,对应下面的changelog,如php-5.4.45-1.el6.x86_64.rpm
summary: php is a server-side scripting language for creating dynamic web pages #简要描述信息,最好不要超过50个字符,如要详述,使用下面的%description
group: development/languages  #要全用这里面的一个组:less /usr/share/doc/rpm-version/groups
license: 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: 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} #%{?_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:表示卸载
#rpm安装后执行的脚本
%post
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
#rpm卸载前执行的脚本
%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
%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

[root@linuxeye.com sources]# pwd
/root/rpmbuild/sources
[root@linuxeye.com sources]# ls
redis-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-redis
version: 2.2.7
release: 1%{?dist}
summary: the phpredis extension provides an api for communicating with the redis key-value store.
group: development/languages
license: php
url: http://pecl.php.net/package/redis
source0: redis-%{version}.tgz
buildroot: %_topdir/buildroot
requires: php
buildrequires: php >= 5.4.40
%description
the 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
%configure
make %{?_smp_mflags}
%install
rm -rf %{buildroot}
mkdir -p %{buildroot}%{php_extdir}
make install install_root=%{buildroot}
find %{buildroot} -name redis.so -exec /bin/mv {} %{buildroot}%{php_extdir} \;
#rpm安装后执行的脚本
%post
if [ $1 == 1 ];then
  [ -z "`grep '^extension_dir' /usr/local/php/etc/php.ini`" ] && echo "extension_dir = \"%{php_extdir}\"" >> /usr/local/php/etc/php.ini
  sed -i 's@^extension_dir\(.*\)@extension_dir\1\nextension = "redis.so"@' /usr/local/php/etc/php.ini
fi
#rpm卸载前执行的脚本
%preun
if [ $1 == 0 ];then
  /etc/init.d/php-fpm stop > /dev/null 2>&1
  sed -i '/redis.so/d' /usr/local/php/etc/php.ini
fi
#%postun rpm卸载后执行的脚本
if [ $1 == 0 ];then
  /etc/init.d/php-fpm start > /dev/null 2>&1
fi
%clean
rm -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包

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

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