亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

安裝配置php-fpm來搭建Nginx+PHP的生產(chǎn)環(huán)境

 更新時間:2016年01月05日 10:06:14   作者:mood  
這篇文章主要介紹了安裝配置php-fpm來搭建Nginx+PHP的生產(chǎn)環(huán)境的方法,php-fpm的作用是將FastCGI進程管理整合進PHP包,需要的朋友可以參考下

nginx本身不能處理PHP,它只是個web服務器,當接收到請求后,如果是php請求,則發(fā)給php解釋器處理,并把結(jié)果返回給客戶端。

nginx一般是把請求發(fā)fastcgi管理進程處理,fascgi管理進程選擇cgi子進程處理結(jié)果并返回被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已經(jīng)集成php-fpm了,不再是第三方的包了,推薦使用。PHP-FPM提供了更好的PHP進程管理方式,可以有效控制內(nèi)存和進程、可以平滑重載PHP配置,比spawn-fcgi具有更多優(yōu)點,所以被PHP官方收錄了。在./configure的時候帶 –enable-fpm參數(shù)即可開啟PHP-FPM,其它參數(shù)都是配置php的,具體選項含義可以查看這里。

安裝前準備
centos下執(zhí)行

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

以上兩種方式都可以安裝php-fpm,安裝后內(nèi)容放在/usr/local/php目錄下

201615100155789.png (300×41)

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

下面是對php-fpm運行用戶進行設(shè)置

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

然后按照http://www.nginx.cn/install 安裝nginx

三、修改nginx配置文件以支持php-fpm

nginx安裝完成后,修改nginx配置文件為,nginx.conf

其中server段增加如下配置,注意標紅內(nèi)容配置,否則會出現(xiàn)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;
}

四、創(chuàng)建測試php文件

創(chuàng)建php文件

在/usr/local/nginx/html下創(chuàng)建index.php文件,輸入如下內(nèi)容

<?php
  echo phpinfo();
?>

五、啟動服務

啟動php-fpm和nginx

/usr/local/php/sbin/php-fpm 
#手動打補丁的啟動方式/usr/local/php/sbin/php-fpm start

sudo /usr/local/nginx/nginx

php-fpm關(guān)閉重啟見文章結(jié)尾

六、瀏覽器訪問

訪問http://你的服務器ip/index.php,皆可以見到php信息了。

201615100326710.png (300×120)

七、錯誤解決
在使用Nginx時,經(jīng)常會碰到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腳本的最大執(zhí)行時間的。當超過這個時間時,PHP-FPM不只會終止腳本的執(zhí)行,
還會終止執(zhí)行腳本的Worker進程。所以Nginx會發(fā)現(xiàn)與自己通信的連接斷掉了,就會返回給客戶端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

所以只需將這兩項的值調(diào)大一些就可以讓PHP腳本不會因為執(zhí)行時間長而被終止了。request_terminate_timeout可以覆蓋max_execution_time,
所以如果不想改全局的php.ini,那只改PHP-FPM的配置就可以了。

此外要注意的是Nginx的upstream模塊中的max_fail和fail_timeout兩項。有時Nginx與上游服務器(如Tomcat、FastCGI)的通信只是偶然斷掉了,
但max_fail如果設(shè)置的比較小的話,那么在接下來的fail_timeout時間內(nèi),Nginx都會認為上游服務器掛掉了,都會返回502錯誤。
所以可以將max_fail調(diào)大一些,將fail_timeout調(diào)小一些。

2.504 Gateway Time-out錯誤

PHP-FPM設(shè)置的腳本最大執(zhí)行時間已經(jīng)夠長了,但執(zhí)行耗時PHP腳本時,發(fā)現(xiàn)Nginx報錯從502變?yōu)?04了。這是為什么呢?
因為我們修改的只是PHP的配置,Nginx中也有關(guān)于與上游服務器通信超時時間的配置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"

調(diào)高這三項的值(主要是read和send兩項,默認不配置的話Nginx會將超時時間設(shè)為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;
   }

相關(guān)文章

最新評論