发布于 2016-02-06 13:15:43 | 243 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Nginx中文文档,程序狗速度看过来!

Nginx WEB服务器

Nginx 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。


这篇文章主要介绍了安装配置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

以上两种方式都可以安装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

然后按照http://www.nginx.cn/install 安装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();
?>

五、启动服务

启动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;
   }



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务