製作PHP的RPM包教程

來源:互聯網
上載者:User
有時候為了方便源碼包的安裝,我們需要自己訂製軟體包的需求,我們會把一些源碼包按照我們的需求來做成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名.specSRPMS: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]# lsphp-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/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   #打包人的資訊Vendor: OneinStack  #發行商或打包組織的資訊Source0: %{name}-%{version}.tar.gz  #原始碼包,可以帶多個用Source1、Source2等源,後面也可以用%{source1}、%{source2}引用BuildRoot: %_topdir/BUILDROOT  #安裝或編譯時間使用的“虛擬目錄”Requires: libmcryptRequires: mhashRequires: 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-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 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 < /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    fifi#%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  5.4.45-1- Initial version

下面是php-redis.spec

[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.ini    sed -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>&1    sed -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  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二進位包
原文:https://blog.linuxeye.com/431.html
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.