首頁  >  文章  >  後端開發  >  安裝配置php-fpm來搭建Nginx+PHP的生產環境

安裝配置php-fpm來搭建Nginx+PHP的生產環境

不言
不言原創
2018-06-02 10:19:2012864瀏覽

這篇文章主要介紹了安裝配置php-fpm來搭建Nginx PHP的生產環境的方法,php-fpm的作用是將FastCGI進程管理整合進PHP包,需要的朋友可以參考下

#nginx本身不能處理PHP,它只是個web伺服器,當接收到請求後,如果是php請求,則發給php解釋器處理,並把結果傳回給客戶端。

nginx一般是把請求發fastcgi管理進程處理,fascgi管理進程選擇cgi子進程處理結果並回傳被nginx

本文以php-fpm為例介紹如何使nginx支援PHP

一、編譯安裝php-fpm

什麼是PHP-FPM

PHP-FPM是一個PHP FastCGI管理器,是只用於PHP的,可以在http://php-fpm.org/download下載得到.

PHP-FPM其實是PHP原始碼的一個補丁,旨在將FastCGI進程管理整合進PHP包中。必須將它patch到你的PHP原始碼中,在編譯安裝PHP後才可以使用。

新版PHP已經整合php-fpm了,不再是第三方的套件了,建議使用。 PHP-FPM提供了更好的PHP進程管理方式,可以有效控制記憶體和進程、可以平滑重載PHP配置,比spawn-fcgi有更多優點,所以被PHP官方收錄了。在./configure的時候帶 –enable-fpm參數即可開啟PHP-FPM,其它參數都是配置php的,具體選項意義可以查看這裡。

安裝前準備
centos下執行

yum -y install gcc automake autoconf libtool make

yum -y install gcc gcc-c++ glibc

yum -y install libmcrypt-devel mhash-devel libxslt-devel

libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel 
zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel 
ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel 
krb5 krb5-devel libidn libidn-devel openssl openssl-devel

新版php- fpm安裝(建議安裝方式)

wget http://cn2.php.net/distributions/php-5.4.7.tar.gz
tar zvxf php-5.4.7.tar.gz
cd php-5.4.7
./configure --prefix=/usr/local/php --enable-fpm --with-mcrypt

#
--enable-mbstring --disable-pdo --with-curl --disable-debug --disable-rpath 
--enable-inline-optimization --with-bz2 --with-zlib --enable-sockets 
--enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex 
--with-mhash --enable-zip --with-pcre-regex --with-mysql --with-mysqli 
--with-gd --with-jpeg-dir

make all install


201615100155789.png (300×41)


以上兩種方式都可以安裝php-fpm,安裝後內容放在/usr/local/php目錄下


以上就完成了php- fpm的安裝。

下面是對php-fpm運行使用者進行設定

cd /usr/local/php
cp etc/php-fpm.conf.default etc/php-fpm.conf
vi etc/php-fpm.conf

修改

user = www-data
group = www-data

如果www-data使用者不存在,那麼先加入www-data使用者

#

groupadd www-data
useradd -g www-data www-data

二、編譯安裝nginx

三、修改nginx設定檔以支援php-fpm

nginx安裝完成後,修改nginx設定檔為,nginx.conf

其中server段增加如下配置,注意標紅內容配置,否則會出現No input file specified.錯誤

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

四、建立測試php檔案

建立php檔案

#在/usr/local/nginx/html下建立index.php文件,輸入如下內容

<?php
  echo phpinfo();
?>

201615100326710.png (300×120)五、啟動服務


啟動php-fpm和nginx

/usr/local/php/sbin/php-fpm 
#手动打补丁的启动方式/usr/local/php/sbin/php-fpm start

sudo /usr/local/nginx/nginx

#php-fpm關閉重啟見文章結尾

六、瀏覽器訪問


#訪問http://你的伺服器ip/index.php,皆可以見到php資訊了。







在使用Nginx時,常常會碰到502 Bad Gateway和504 Gateway Time-out錯誤,以下以Nginx PHP-FPM來分析下這兩種常見錯誤的原因和解決方案。

1.502 Bad Gateway錯誤 #########在php.ini和php-fpm.conf中分別有這樣兩個設定項:max_execution_time和request_terminate_timeout。 ###這兩項都是用來設定一個PHP腳本的最大執行時間的。當超過這個時間時,PHP-FPM不僅會終止腳本的執行,###還會終止執行腳本的Worker進程。所以Nginx會發現與自己通訊的連線斷掉了,就會回傳給客戶端502錯誤。 ######以PHP-FPM的request_terminate_timeout=30秒時為例,報502 Bad Gateway錯誤的具體資訊如下:###1)Nginx錯誤存取日誌:############ #
   2013/09/19 01:09:00 [error] 27600#0: *78887 recv() failed (104: Connection reset by peer) while reading response header from upstream, 
   client: 192.168.1.101, server: test.com, request: "POST /index.php HTTP/1.1", upstream: "fastcgi://unix:/dev/shm/php-fcgi.sock:", 
   host: "test.com", referrer: "http://test.com/index.php"
#########2)PHP-FPM錯誤日誌:#############
   WARNING: child 25708 exited on signal 15 (SIGTERM) after 21008.883410 seconds from start
########所以只要將這兩項的值調大一些就可以讓PHP腳本不會因為執行時間長而被終止了。 request_terminate_timeout可以覆寫max_execution_time,###所以如果不想改全域的php.ini,那隻改PHP-FPM的設定就可以了。 ######此外要注意的是Nginx的upstream模組中的max_fail和fail_timeout兩個項目。有時Nginx與上游伺服器(如Tomcat、FastCGI)的通訊只是偶然斷掉了,###但max_fail如果設定的比較小的話,那麼在接下來的fail_timeout時間內,Nginx都會認為上游伺服器掛掉了,都會返回502錯誤。 ###所以可以將max_fail調大一些,將fail_timeout調小一些。 #########2.504 Gateway Time-out錯誤######

PHP-FPM设置的脚本最大执行时间已经够长了,但执行耗时PHP脚本时,发现Nginx报错从502变为504了。这是为什么呢?
因为我们修改的只是PHP的配置,Nginx中也有关于与上游服务器通信超时时间的配置factcgi_connect/read/send_timeout。

以Nginx超时时间为90秒,PHP-FPM超时时间为300秒为例,报504 Gateway Timeout错误时的Nginx错误访问日志如下:

   2013/09/19 00:55:51 [error] 27600#0: *78877 upstream timed out (110: Connection timed out) while reading response header from upstream, 
   client: 192.168.1.101, server: test.com, request: "POST /index.php HTTP/1.1", upstream: "fastcgi://unix:/dev/shm/php-fcgi.sock:", 
   host: "test.com", referrer: "http://test.com/index.php"

调高这三项的值(主要是read和send两项,默认不配置的话Nginx会将超时时间设为60秒)之后,504错误也解决了。
而且这三项配置可以配置在http、server级别,也可以配置在location级别。担心影响其他应用的话,就配置在自己应用的location中吧。
要注意的是factcgi_connect/read/send_timeout是对FastCGI生效的,而proxy_connect/read/send_timeout是对proxy_pass生效的。

配置举例:

location ~ \.php$ {
        root          /home/cdai/test.com;
        include         fastcgi_params;
        fastcgi_connect_timeout   180;
        fastcgi_read_timeout      600;
        fastcgi_send_timeout      600;
        fastcgi_pass      unix:/dev/shm/php-fcgi.sock;
        fastcgi_index      index.php;
        fastcgi_param     SCRIPT_FILENAME /home/cdai/test.com$fastcgi_script_name;
   }

以上是安裝配置php-fpm來搭建Nginx+PHP的生產環境的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn