六、配置PHP-FPM

來源:互聯網
上載者:User
六、配置PHP-FPM

5.1 簡介:

PHP-FPM(FastCGIProcess Manager:FastCGI進程管理器)

PHP-FPM是一個PHPFastCGI管理器,是只用於PHP的。它提供了更好的PHP進程管理方式,可以有效控制記憶體和進程、可以平滑重載PHP配置,比spawn-fcgi具有更多優點,所以被PHP官方收錄了。在./configure的時候帶�enable-fpm參數即可開啟PHP-FPM。

使用PHP-FPM來控制PHP-CGI的FastCGI進程

/usr/local/php/sbin/php-fpm{start|stop|quit|restart|reload|logrotate}

--start 啟動php的fastcgi進程

--stop 強制終止php的fastcgi進程

--quit 平滑終止php的fastcgi進程

--restart 重啟php的fastcgi進程

--reload 重新平滑載入php的php.ini

--logrotate 重新啟用log檔案

5.2 配置php-fpm:

2.1為php-fpm提供SysV init指令碼,並將其添加至服務列表:

[root@web tool]# cd php-5.5.34[root@web php-5.5.34]# cpsapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm[root@web php-5.5.34]# chmod +x/etc/rc.d/init.d/php-fpm[root@web php-5.5.34]# chkconfig --addphp-fpm[root@webphp-5.5.34]# chkconfig php-fpm on

2.2為php-fpm提供設定檔:

[root@webphp-5.5.34] # cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

2.3編輯php-fpm的設定檔:

[root@webphp-5.5.34] # vim /usr/local/php/etc/php-fpm.conf配置fpm的相關選項為你所需要的值,預設會啟用pid檔案:pm.max_children = 50pm.start_servers = 5pm.min_spare_servers = 2pm.max_spare_servers= 8

2.4接下來就可以啟動php-fpm了:

[root@web php-5.5.34]# service php-fpm startStarting php-fpm  done說明:php-fpm 可用參數start|stop|force-quit|restart|reload|status

2.5為了方便使用,可以將 PHP 命令添加到環境變數

[root@web php-5.5.34]# vim ~/.bash_profile # .bash_profile # Get the aliases and functionsif [ -f ~/.bashrc ]; then       . ~/.bashrcfi # User specific environment and startupprograms#將PATH=$PATH:$HOME/bin 改為:PATH=$PATH:$HOME/bin:/usr/local/php/binPATH=$PATH:$HOME/bin:/usr/local/php/bin export PATH 儲存退出!
使 PHP 環境變數生效:[root@webphp-5.5.34]# . ~/.bash_profile
查看看 PHP 版本[root@web php-5.5.34]# php -vPHP 5.5.34 (cli) (built: Apr  7 2016 11:22:13)Copyright (c) 1997-2015 The PHP GroupZend Engine v2.5.0, Copyright (c) 1998-2015Zend Technologies   with XCache v3.1.2, Copyright (c) 2005-2014, by mOo    with XCache Cacher v3.1.2, Copyright (c)2005-2014, by mOo

5.3 檢驗:

使用如下命令來驗正(如果此命令輸出有中幾個php-fpm進程就說明啟動成功了):

[root@web php]# psaux | grep php-fpmroot      54722 0.0  0.3 175728  5932 ?       Ss   20:19   0:00 php-fpm: master process(/usr/local/php/etc/php-fpm.conf)                                                                   www       54723 0.0  0.2 175728  5144 ?       S    20:19   0:00 php-fpm: pool www                                                                                                           www       54724 0.0  0.2 175728  5144 ?       S    20:19   0:00 php-fpm: pool www                                                                                                            www       54725 0.0  0.2 175728  5144 ?       S    20:19   0:00 php-fpm: pool www                                                                                                           www       54726 0.0  0.2 175728  5144 ?       S    20:19   0:00 php-fpm: pool www                                                                                                           www       54727 0.0  0.2 175728  5144 ?       S    20:19   0:00 php-fpm: pool www                                                                                                           root      54837 0.0  0.0 103308   852 pts/1   S+   20:22   0:00 grep php-fpm

預設情況下,fpm監聽在127.0.0.1的9000連接埠,也可以使用如下命令驗正其是否已經監聽在相應的通訊端。

[root@web php-5.5.34]#netstat -tnlp | grep php-fpmtcp        0     0 127.0.0.1:9000             0.0.0.0:*                   LISTEN      54722/php-fpm

5.4啟用httpd的相關模組

在Apache httpd 2.4以後已經專門有一個模組針對FastCGI的實現,此模組為mod_proxy_fcgi.so,它其實是作為mod_proxy.so模組的擴充,因此,這兩個模組都要載入

[root@web php-5.5.34]# vim/etc/httpd24/httpd.conf 115 LoadModule proxy_module modules/mod_proxy.so119 LoadModule proxy_fcgi_modulemodules/mod_proxy_fcgi.so

然後重新載入httpd服務:

[root@web php-5.5.34]# service httpd reload

5.5 配置虛擬機器主機支援使用fcgi

1.更改httpd-vhosts.conf配置在相應的虛擬機器主機中添加類似如下兩行。

ProxyRequests Off

ProxyPassMatch^/(.*\.php)$ fcgi://127.0.0.1:9000/PATH/TO/DOCUMENT_ROOT/$1

[root@web extra]# vim httpd-vhosts.conf   ServerAdmin webmaster@dummy-host.example.com   DocumentRoot "/usr/local/apache/htdocs/www/"   ServerName www.52linux.com    ProxyRequests Off    ProxyPassMatch ^/(.*\.php)$fcgi://127.0.0.1:9000/usr/local/apache/htdocs/www/$1   ErrorLog "logs/www_error_log"   CustomLog "logs/www_access_log" common    ServerAdmin webmaster@dummy-host2.example.com   DocumentRoot "/usr/local/apache/htdocs/blog/"   ServerName  blog.52linux.com    ProxyRequests Off    ProxyPassMatch ^/(.*\.php)$fcgi://127.0.0.1:9000/usr/local/apache/htdocs/blog/$1   ErrorLog "logs/blog_error_log"   CustomLog "logs/blog_access_log" common    ServerAdmin webmaster@dummy-host2.example.com    DocumentRoot"/usr/local/apache/htdocs/bbs/"   ServerName bbs.52linux.com    ProxyRequests Off    ProxyPassMatch ^/(.*\.php)$fcgi://127.0.0.1:9000/usr/local/apache/htdocs/bbs/$1   ErrorLog "logs/bbs_error_log"   CustomLog "logs/bbs_access_log"   comomn

ProxyRequests Off:關閉正向 Proxy

ProxyPassMatch:把以.php結尾的檔案請求發送到php-fpm進程,php-fpm至少需要知道啟動並執行目錄和URI,所以這裡直接在fcgi://127.0.0.1:9000後指明了這兩個參數,其它的參數的傳遞已經被mod_proxy_fcgi.so進行了封裝,不需要手動指定。

2.編輯apache設定檔httpd.conf,讓apache能識別php格式的頁面,並支援php格式的首頁

[root@web extra]# vim/etc/httpd24/httpd.conf# 1添加如下二行:388     AddTypeapplication/x-httpd-php  .php389    AddType application/x-httpd-php-source .phps
# 2 定位至DirectoryIndexindex.html 修改為:257 258     DirectoryIndex index.phpindex.html259 

補充:Apache httpd 2.4以前的版本中,要麼把PHP作為Apache的模組運行,要麼添加一個第三方模組支援PHP-FPM實現。


下一篇:七、phpmyadmin安裝

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.