怎么用php-fpm搭建Nginx+PHP的生产环境

这篇文章主要介绍了怎么用php-fpm搭建Nginx+PHP的生产环境的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇怎么用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搭建Nginx+PHP的生产环境  php 第1张

以上就完成了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信息了。

怎么用php-fpm搭建Nginx+PHP的生产环境  php 第2张

七、错误解决
在使用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-fpm搭建Nginx+PHP的生产环境”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注蜗牛博客行业资讯频道。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo99@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

评论

有免费节点资源,我们会通知你!加入纸飞机订阅群

×
天气预报查看日历分享网页手机扫码留言评论电报频道链接