1. 程式人生 > 實用技巧 >如何最接近rpm的安裝方式去編譯安裝最新的php-fpm

如何最接近rpm的安裝方式去編譯安裝最新的php-fpm

1 前言

筆者非常不喜歡某些人編譯安裝的軟體,維護起來非常缺乏rpm安裝的那種規範感覺,於是筆者參閱官方的編譯安裝教程以及對比分析rpm安裝的目錄佈置結構,完成以下安裝。

2 安裝PHP-FPM

2.1 環境配置

2.1.1 安裝解壓工具

yuminstall-ybzip2

2.1.2 配置編譯環境

yum-yinstallgccgcc-c++makeexpat-devel

2.1.3 下載php二進位制安裝包

cd~
wgethttp://cn2.php.net/distributions/php-7.1.12.tar.bz2

注:具體的下載頁面請參閱

http://sg2.php.net/downloads.php

2.1.4 建立執行使用者

groupadd-g48apache
useradd-u48-g48-d/usr/share/httpd-s/sbin/nologinapache

2.1.5 預建目錄

mkdir/var/log/php-fpm/
mkdir/run/php-fpm/

2.1.6 配置防火牆

firewall-cmd --permanent --add-port 9000/tcp

firewall-cmd --reload

firewall-cmd --list-all

2.2 編譯安裝

2.2.1 解壓安裝包

tar-xfphp-7.1.12.tar.bz2

2.2.2 安裝前構建

cdphp-7.1.12		
./configure--bindir=/usr/sbin/\
--sbindir=/usr/sbin/\
--sysconfdir=/etc/\
--libdir=/usr/lib64/\
--mandir=/usr/share/man/\
--includedir=/usr/include/\
--with-fpm-user=apache\
--with-fpm-group=apache\
--enable-fpm\
--with-mysqli

根據提示解決安裝的依賴關係

yuminstall-ylibxml2-devel

2.2.3 編譯並安裝

make
makeinstall|teeinstall.log

2.3 配置FPM

2.3.1 配置php.ini

cpphp.ini-development/etc/php.ini

2.3.2 防止空檔案遞給PHP-FPM後端

vim/etc/php.ini

修改以下引數:

cgi.fix_pathinfo=0

注:可防任意指令碼注入

2.3.3 建立配置

vim/etc/php-fpm.conf

加入如下配置:

;;;;;;;;;;;;;;;;;;;;;
;FPMConfiguration;
;;;;;;;;;;;;;;;;;;;;;

;AllrelativepathsinthisconfigurationfilearerelativetoPHP'sinstall
;prefix.

;Includeoneormorefiles.Ifglob(3)exists,itisusedtoincludeabunchof
;filesfromaglob(3)pattern.Thisdirectivecanbeusedeverywhereinthe
;file.
include=/etc/php-fpm.d/*.conf

;;;;;;;;;;;;;;;;;;
;GlobalOptions;
;;;;;;;;;;;;;;;;;;

[global]
;Pidfile
;DefaultValue:none
pid=/run/php-fpm/php-fpm.pid

;Errorlogfile
;DefaultValue:/var/log/php-fpm.log
error_log=/var/log/php-fpm/error.log

;Loglevel
;PossibleValues:alert,error,warning,notice,debug
;DefaultValue:notice
;log_level=notice

;IfthisnumberofchildprocessesexitwithSIGSEGVorSIGBUSwithinthetime
;intervalsetbyemergency_restart_intervalthenFPMwillrestart.Avalue
;of'0'means'Off'.
;DefaultValue:0
;emergency_restart_threshold=0

;Intervaloftimeusedbyemergency_restart_intervaltodeterminewhen
;agracefulrestartwillbeinitiated.Thiscanbeusefultoworkaround
;accidentalcorruptionsinanaccelerator'ssharedmemory.
;AvailableUnits:s(econds),m(inutes),h(ours),ord(ays)
;DefaultUnit:seconds
;DefaultValue:0
;emergency_restart_interval=0

;Timelimitforchildprocessestowaitforareactiononsignalsfrommaster.
;Availableunits:s(econds),m(inutes),h(ours),ord(ays)
;DefaultUnit:seconds
;DefaultValue:0
;process_control_timeout=0

;SendFPMtobackground.Setto'no'tokeepFPMinforegroundfordebugging.
;DefaultValue:yes
daemonize=no

;;;;;;;;;;;;;;;;;;;;
;PoolDefinitions;
;;;;;;;;;;;;;;;;;;;;

;See/etc/php-fpm.d/*.conf

建立以下配置檔案:

mkdir/etc/php-fpm.d/
vim/etc/php-fpm.d/www.conf

加入如下配置:

;Startanewpoolnamed'www'.
[www]

;TheaddressonwhichtoacceptFastCGIrequests.
;Validsyntaxesare:
;'ip.add.re.ss:port'-tolistenonaTCPsockettoaspecificaddresson
;aspecificport;
;'port'-tolistenonaTCPsockettoalladdressesona
;specificport;
;'/path/to/unix/socket'-tolistenonaunixsocket.
;Note:Thisvalueismandatory.
listen=127.0.0.1:9000

;Setlisten(2)backlog.Avalueof'-1'meansunlimited.
;DefaultValue:-1
;listen.backlog=-1

;Listofipv4addressesofFastCGIclientswhichareallowedtoconnect.
;EquivalenttotheFCGI_WEB_SERVER_ADDRSenvironmentvariableintheoriginal
;PHPFCGI(5.2.2+).Makessenseonlywithatcplisteningsocket.Eachaddress
;mustbeseparatedbyacomma.Ifthisvalueisleftblank,connectionswillbe
;acceptedfromanyipaddress.
;DefaultValue:any
listen.allowed_clients=127.0.0.1

;Setpermissionsforunixsocket,ifoneisused.InLinux,read/write
;permissionsmustbesetinordertoallowconnectionsfromawebserver.Many
;BSD-derivedsystemsallowconnectionsregardlessofpermissions.
;DefaultValues:userandgrouparesetastherunninguser
;modeissetto0666
;listen.owner=nobody
;listen.group=nobody
;listen.mode=0666

;Unixuser/groupofprocesses
;Note:Theuserismandatory.Ifthegroupisnotset,thedefaultuser'sgroup
;willbeused.
;RPM:apacheChoosedtobeabletoaccesssomedirashttpd
user=apache
;RPM:Keepagroupallowedtowriteinlogdir.
group=apache

;Choosehowtheprocessmanagerwillcontrolthenumberofchildprocesses.
;PossibleValues:
;static-afixednumber(pm.max_children)ofchildprocesses;
;dynamic-thenumberofchildprocessesaresetdynamicallybasedonthe
;followingdirectives:
;pm.max_children-themaximumnumberofchildrenthatcan
;bealiveatthesametime.
;pm.start_servers-thenumberofchildrencreatedonstartup.
;pm.min_spare_servers-theminimumnumberofchildrenin'idle'
;state(waitingtoprocess).Ifthenumber
;of'idle'processesislessthanthis
;numberthensomechildrenwillbecreated.
;pm.max_spare_servers-themaximumnumberofchildrenin'idle'
;state(waitingtoprocess).Ifthenumber
;of'idle'processesisgreaterthanthis
;numberthensomechildrenwillbekilled.
;Note:Thisvalueismandatory.
pm=dynamic

;Thenumberofchildprocessestobecreatedwhenpmissetto'static'andthe
;maximumnumberofchildprocessestobecreatedwhenpmissetto'dynamic'.
;Thisvaluesetsthelimitonthenumberofsimultaneousrequeststhatwillbe
;served.EquivalenttotheApacheMaxClientsdirectivewithmpm_prefork.
;EquivalenttothePHP_FCGI_CHILDRENenvironmentvariableintheoriginalPHP
;CGI.
;Note:Usedwhenpmissettoeither'static'or'dynamic'
;Note:Thisvalueismandatory.
pm.max_children=50

;Thenumberofchildprocessescreatedonstartup.
;Note:Usedonlywhenpmissetto'dynamic'
;DefaultValue:min_spare_servers+(max_spare_servers-min_spare_servers)/2
pm.start_servers=5

;Thedesiredminimumnumberofidleserverprocesses.
;Note:Usedonlywhenpmissetto'dynamic'
;Note:Mandatorywhenpmissetto'dynamic'
pm.min_spare_servers=5

;Thedesiredmaximumnumberofidleserverprocesses.
;Note:Usedonlywhenpmissetto'dynamic'
;Note:Mandatorywhenpmissetto'dynamic'
pm.max_spare_servers=35

;Thenumberofrequestseachchildprocessshouldexecutebeforerespawning.
;Thiscanbeusefultoworkaroundmemoryleaksin3rdpartylibraries.For
;endlessrequestprocessingspecify'0'.EquivalenttoPHP_FCGI_MAX_REQUESTS.
;DefaultValue:0
;pm.max_requests=500

;TheURItoviewtheFPMstatuspage.Ifthisvalueisnotset,noURIwillbe
;recognizedasastatuspage.Bydefault,thestatuspageshowsthefollowing
;information:
;acceptedconn-thenumberofrequestacceptedbythepool;
;pool-thenameofthepool;
;processmanager-staticordynamic;
;idleprocesses-thenumberofidleprocesses;
;activeprocesses-thenumberofactiveprocesses;
;totalprocesses-thenumberofidle+activeprocesses.
;Thevaluesof'idleprocesses','activeprocesses'and'totalprocesses'are
;updatedeachsecond.Thevalueof'acceptedconn'isupdatedinrealtime.
;Exampleoutput:
;acceptedconn:12073
;pool:www
;processmanager:static
;idleprocesses:35
;activeprocesses:65
;totalprocesses:100
;Bydefaultthestatuspageoutputisformattedastext/plain.Passingeither
;'html'or'json'asaquerystringwillreturnthecorrespondingoutput
;syntax.Example:
;http://www.foo.bar/status
;http://www.foo.bar/status?json
;http://www.foo.bar/status?html
;Note:Thevaluemuststartwithaleadingslash(/).Thevaluecanbe
;anything,butitmaynotbeagoodideatousethe.phpextensionorit
;mayconflictwitharealPHPfile.
;DefaultValue:notset
;pm.status_path=/status

;ThepingURItocallthemonitoringpageofFPM.Ifthisvalueisnotset,no
;URIwillberecognizedasapingpage.Thiscouldbeusedtotestfromoutside
;thatFPMisaliveandresponding,orto
;-createagraphofFPMavailability(rrdorsuch);
;-removeaserverfromagroupifitisnotresponding(loadbalancing);
;-triggeralertsfortheoperatingteam(24/7).
;Note:Thevaluemuststartwithaleadingslash(/).Thevaluecanbe
;anything,butitmaynotbeagoodideatousethe.phpextensionorit
;mayconflictwitharealPHPfile.
;DefaultValue:notset
;ping.path=/ping

;Thisdirectivemaybeusedtocustomizetheresponseofapingrequest.The
;responseisformattedastext/plainwitha200responsecode.
;DefaultValue:pong
;ping.response=pong

;Thetimeoutforservingasinglerequestafterwhichtheworkerprocesswill
;bekilled.Thisoptionshouldbeusedwhenthe'max_execution_time'inioption
;doesnotstopscriptexecutionforsomereason.Avalueof'0'means'off'.
;Availableunits:s(econds)(default),m(inutes),h(ours),ord(ays)
;DefaultValue:0
;request_terminate_timeout=0

;ThetimeoutforservingasinglerequestafterwhichaPHPbacktracewillbe
;dumpedtothe'slowlog'file.Avalueof'0s'means'off'.
;Availableunits:s(econds)(default),m(inutes),h(ours),ord(ays)
;DefaultValue:0
;request_slowlog_timeout=0

;Thelogfileforslowrequests
;DefaultValue:notset
;Note:slowlogismandatoryifrequest_slowlog_timeoutisset
slowlog=/var/log/php-fpm/www-slow.log

;Setopenfiledescriptorrlimit.
;DefaultValue:systemdefinedvalue
;rlimit_files=1024

;Setmaxcoresizerlimit.
;PossibleValues:'unlimited'oranintegergreaterorequalto0
;DefaultValue:systemdefinedvalue
;rlimit_core=0

;Chroottothisdirectoryatthestart.Thisvaluemustbedefinedasan
;absolutepath.Whenthisvalueisnotset,chrootisnotused.
;Note:chrootingisagreatsecurityfeatureandshouldbeusedwhenever
;possible.However,allPHPpathswillberelativetothechroot
;(error_log,sessions.save_path,...).
;DefaultValue:notset
;chroot=

;Chdirtothisdirectoryatthestart.Thisvaluemustbeanabsolutepath.
;DefaultValue:currentdirectoryor/whenchroot
;chdir=/var/www

;Redirectworkerstdoutandstderrintomainerrorlog.Ifnotset,stdoutand
;stderrwillberedirectedto/dev/nullaccordingtoFastCGIspecs.
;DefaultValue:no
;catch_workers_output=yes

;LimitstheextensionsofthemainscriptFPMwillallowtoparse.Thiscan
;preventconfigurationmistakesonthewebserverside.Youshouldonlylimit
;FPMto.phpextensionstopreventmalicioususerstouseotherextensionsto
;exectutephpcode.
;Note:setanemptyvaluetoallowallextensions.
;DefaultValue:.php
;security.limit_extensions=.php.php3.php4.php5

;PassenvironmentvariableslikeLD_LIBRARY_PATH.All$VARIABLEsaretakenfrom
;thecurrentenvironment.
;DefaultValue:cleanenv
;env[HOSTNAME]=$HOSTNAME
;env[PATH]=/usr/local/bin:/usr/bin:/bin
;env[TMP]=/tmp
;env[TMPDIR]=/tmp
;env[TEMP]=/tmp

;Additionalphp.inidefines,specifictothispoolofworkers.Thesesettings
;overwritethevaluespreviouslydefinedinthephp.ini.Thedirectivesarethe
;sameasthePHPSAPI:
;php_value/php_flag-youcansetclassicinidefineswhichcan
;beoverwrittenfromPHPcall'ini_set'.
;php_admin_value/php_admin_flag-thesedirectiveswon'tbeoverwrittenby
;PHPcall'ini_set'
;Forphp_*flag,validvaluesareon,off,1,0,true,false,yesorno.

;Defining'extension'willloadthecorrespondingsharedextensionfrom
;extension_dir.Defining'disable_functions'or'disable_classes'willnot
;overwritepreviouslydefinedphp.inivalues,butwillappendthenewvalue
;instead.

;DefaultValue:nothingisdefinedbydefaultexceptthevaluesinphp.iniand
;specifiedatstartupwiththe-dargument
;php_admin_value[sendmail_path]=/usr/sbin/sendmail-t-i-f[email protected]
;php_flag[display_errors]=off
php_admin_value[error_log]=/var/log/php-fpm/www-error.log
php_admin_flag[log_errors]=on
;php_admin_value[memory_limit]=128M

;Setsessionpathtoadirectoryownedbyprocessuser
php_value[session.save_handler]=files
php_value[session.save_path]=/var/lib/php/session

2.3.4 測試fpm啟動

/usr/sbin/php-fpm&

2.3.5 查詢服務狀態

ps-aux|grepphp-fpm
netstat-antp|grepphp-fpm

2.3.6 手動關閉服務

kill-s15`pgrep-urootphp-fpm`

2.4 配置服務啟動

2.4.1 配置啟動指令碼

vim/usr/lib/systemd/system/php-fpm.service

加入如下配置:

[Unit]
Description=ThePHPFastCGIProcessManager
After=syslog.targetnetwork.target

[Service]
Type=simple
PIDFile=/run/php-fpm/php-fpm.pid
EnvironmentFile=/etc/sysconfig/php-fpm
ExecStart=/usr/sbin/php-fpm--nodaemonize
ExecReload=/bin/kill-USR2$MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

配置環境檔案:

echo'#Additionalenvironmentfileforphp-fpm'>/etc/sysconfig/php-fpm

過載控制指令碼

systemctldaemon-reload

2.4.2 啟動服務並配置預設啟動

systemctlstartphp-fpm
systemctlenablephp-fpm

2.4.3 服務其他控制命令

systemctlstartphp-fpm
systemctlrestartphp-fpm
systemctlstopphp-fpm

2.5 測試

由於nginx不是本章的重點,所以如需測試請根據以下文章配置nginx,

https://blog.51cto.com/cmdschool/1962061

最後你可以看到如下頁面,

php-fpm.png

參閱文件:

==============================================

http://sg2.php.net/manual/en/install.unix.nginx.php


轉載於:https://blog.51cto.com/cmdschool/2046062