欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

nginx一般是把請(qǐng)求發(fā)fastcgi管理進(jìn)程處理,fascgi管理進(jìn)程選擇cgi子進(jìn)程處理結(jié)果并返回被nginx

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

一、編譯安裝php-fpm

什么是PHP-FPM

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

PHP-FPM其實(shí)是PHP源代碼的一個(gè)補(bǔ)丁,旨在將FastCGI進(jìn)程管理整合進(jìn)PHP包中。必須將它patch到你的PHP源代碼中,在編譯安裝PHP后才可以使用。

新版PHP已經(jīng)集成php-fpm了,不再是第三方的包了,推薦使用。PHP-FPM提供了更好的PHP進(jìn)程管理方式,可以有效控制內(nèi)存和進(jìn)程、可以平滑重載PHP配置,比spawn-fcgi具有更多優(yōu)點(diǎn),所以被PHP官方收錄了。在./configure的時(shí)候帶 –enable-fpm參數(shù)即可開(kāi)啟PHP-FPM,其它參數(shù)都是配置php的,具體選項(xiàng)含義可以查看這里。

安裝前準(zhǔn)備
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的安裝。

下面是對(duì)php-fpm運(yùn)行用戶進(jìn)行設(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段增加如下配置,注意標(biāo)紅內(nèi)容配置,否則會(huì)出現(xiàn)No input file specified.錯(cuò)誤

# 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)建測(cè)試php文件

創(chuàng)建php文件

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

<?php
  echo phpinfo();
?>

五、啟動(dòng)服務(wù)

啟動(dòng)php-fpm和nginx

/usr/local/php/sbin/php-fpm 
#手動(dòng)打補(bǔ)丁的啟動(dòng)方式/usr/local/php/sbin/php-fpm start

sudo /usr/local/nginx/nginx

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

六、瀏覽器訪問(wèn)

訪問(wèn)http://你的服務(wù)器ip/index.php,皆可以見(jiàn)到php信息了。

201615100326710.png (300×120)

七、錯(cuò)誤解決
在使用Nginx時(shí),經(jīng)常會(huì)碰到502 Bad Gateway和504 Gateway Time-out錯(cuò)誤,下面以Nginx+PHP-FPM來(lái)分析下這兩種常見(jiàn)錯(cuò)誤的原因和解決方案。

1.502 Bad Gateway錯(cuò)誤

在php.ini和php-fpm.conf中分別有這樣兩個(gè)配置項(xiàng):max_execution_time和request_terminate_timeout。
這兩項(xiàng)都是用來(lái)配置一個(gè)PHP腳本的最大執(zhí)行時(shí)間的。當(dāng)超過(guò)這個(gè)時(shí)間時(shí),PHP-FPM不只會(huì)終止腳本的執(zhí)行,
還會(huì)終止執(zhí)行腳本的Worker進(jìn)程。所以Nginx會(huì)發(fā)現(xiàn)與自己通信的連接斷掉了,就會(huì)返回給客戶端502錯(cuò)誤。

以PHP-FPM的request_terminate_timeout=30秒時(shí)為例,報(bào)502 Bad Gateway錯(cuò)誤的具體信息如下:
1)Nginx錯(cuò)誤訪問(wèn)日志:

   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報(bào)錯(cuò)日志:

   WARNING: child 25708 exited on signal 15 (SIGTERM) after 21008.883410 seconds from start

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

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

2.504 Gateway Time-out錯(cuò)誤

PHP-FPM設(shè)置的腳本最大執(zhí)行時(shí)間已經(jīng)夠長(zhǎng)了,但執(zhí)行耗時(shí)PHP腳本時(shí),發(fā)現(xiàn)Nginx報(bào)錯(cuò)從502變?yōu)?04了。這是為什么呢?
因?yàn)槲覀冃薷牡闹皇荘HP的配置,Nginx中也有關(guān)于與上游服務(wù)器通信超時(shí)時(shí)間的配置factcgi_connect/read/send_timeout。

以Nginx超時(shí)時(shí)間為90秒,PHP-FPM超時(shí)時(shí)間為300秒為例,報(bào)504 Gateway Timeout錯(cuò)誤時(shí)的Nginx錯(cuò)誤訪問(wèn)日志如下:

   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)高這三項(xiàng)的值(主要是read和send兩項(xiàng),默認(rèn)不配置的話Nginx會(huì)將超時(shí)時(shí)間設(shè)為60秒)之后,504錯(cuò)誤也解決了。
而且這三項(xiàng)配置可以配置在http、server級(jí)別,也可以配置在location級(jí)別。擔(dān)心影響其他應(yīng)用的話,就配置在自己應(yīng)用的location中吧。
要注意的是factcgi_connect/read/send_timeout是對(duì)FastCGI生效的,而proxy_connect/read/send_timeout是對(duì)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)文章

最新評(píng)論