PHP rpm Package Tutorial in Linux

Source: Internet
Author: User
Tags fpm mcrypt mkdir redis rpmbuild value store

Here we start by making PHP rpm to introduce its production methods. The following operations are performed on the CentOS6.6 64-bit system.

Install Rpm-build

[root@linuxeye.com sources]# yum-y Install Rpm-build

Create a work shop directory

[Root@linuxeye.com sources]# Vim ~/.rpmmacros
%_topdir/root/rpmbuild
[Root@linuxeye.com sources]# MKDIR-PV ~/rpmbuild/{build,rpms,sources,specs,srpms}

Under Redhat, the RPM package's default production path is/usr/src/redhat, but CentOS does not have the directory, so we have to customize the working directory, which contains 6 directories (all caps are required)

Build: Location of source code after decompression
RPMS: After the completion of the RPM package storage directory, for a specific platform to specify subdirectories (x86_64)
SOURCES: Collection of source files, source materials, patch files, etc. storage location
SPECS: Store spec file, as the lead file for RPM package, with RPM name. Spec
SRPMS:SRC format of the RPM package location, since the SRC format of the package, there is no platform concept
Builtroot: rhizoids, use install temporarily installs to this directory, uses this directory as the root to use, therefore in this directory directory file, is the real directory file. When the package is complete, this directory will be deleted during the cleanup phase

[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 displays all macros, beginning with an underscore, an underscore: defining the use of the environment, two underscores: usually defined as a command, why to define macros, because different systems, the location of the command may be different, so the definition of the macro to find the true location of the command

Collect source File script files

[Root@linuxeye.com sources]# pwd
/root/rpmbuild/sources
[Root@linuxeye.com sources]# ls
Php-5.4.45.tar.gz
Writing spec Files
[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 #版本号 (not available-)
Release:1%{?dist} #release号, corresponding to the following changelog, such as php-5.4.45-1.el6.x86_64.rpm
Summary:php is a server-side scripting language for creating dynamic Web pages #简要描述信息, it is best not to exceed 50 characters, if you want to elaborate, use the following%descriptio N

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 #源代码包, you can bring more than Source1, Source2 and other sources, can be followed by%{source1},%{source2} reference
BuildRoot:%_topdir/buildroot #安装或编译时使用的 "virtual directory"

Requires:libmcrypt
Requires:mhash
Requires:mcrypt
Requires:libiconv #定义php依赖的包, requires Yum installation (use Epel source here)

%description #软件包详述
PHP is a widely-used general-purpose scripting language This is especially suited to Web development and can be embedded Into HTML.


%prep #软件编译之前的处理, such as decompression
%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} means: If multiprocessor

Parallel compilation when Make


%install  #开始安装软件, such as make install
Rm-rf%{buildroot}
Make install_root=%{buildroot} install
Rm-r F%{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- A-Z "' grep ^%{_user}/etc/passwd '"]; then    # has 3 values representing actions, installation type, processing type
    groupadd%{_group}-G 10000                                # 1: Indicates installation
    useradd-u 10000-g 10000-m%{_user}& nbsp;                    # 2: Upgrade
fi                                                              # 0: means uninstall

#rpm安装后执行的脚本
%post
if [$ = 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 $} ' #下面主要是参数的优化
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@^;d ate.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 [$ = 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

Scripts executed after the #%postun rpm uninstall

%clean #clean的主要作用就是删除BUILD
RM-RF%{buildroot}


%files #指定哪些文件需要被打包, such as/usr/local/php
%defattr (-,root,root,-)
%{_prefix}
%attr (0755,root,root)/etc/init.d/php-fpm


%changelog #日志改变段, this section mainly describes the development record of software
* Sat Oct 2015 Yeho <lj2007331@gmail.com> 5.4.45-1
-Initial Version

Here's 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 APIs 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 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];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 [$ = 0];then
/ETC/INIT.D/PHP-FPM Stop >/dev/null 2>&1
Sed-i '/redis.so/d '/usr/local/php/etc/php.ini
Fi

Scripts executed after the #%postun rpm uninstall
If [$ = 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 2015 Yeho <lj2007331@gmail.com> 2.2.7-1
-Initial Version
Compiling RPM Packages
[root@linuxeye.com spec]# rpmbuild-bb php.spec make php rpm binary package
[Root@linuxeye.com spec]# rpmbuild-bb php-redis.spec production php-redis rpm binary package

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.