小知识:安装配置php-fpm来搭建Nginx+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下执行
?
1
2
3
4
5
yum -y install gcc automake autoconf libtool make
yum -y install gcc gcc-c++ glibc
yum -y install libmcrypt-devel mhash-devel libxslt-devel
?
1
2
3
4
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安装(推荐安装方式)

?
1
2
3
4
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
?
1
2
3
4
5
–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
?
1
make all install

以上两种方式都可以安装php-fpm,安装后内容放在/usr/local/php目录下

%小知识:安装配置php-fpm来搭建Nginx+PHP的生产环境-猿站网-插图

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

下面是对php-fpm运行用户进行设置

?
1
2
3
cd /usr/local/php
cp etc/php-fpm.conf.default etc/php-fpm.conf
vi etc/php-fpm.conf

修改

?
1
2
user = www-data
group = www-data

如果www-data用户不存在,那么先添加www-data用户

?
1
2
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.错误

?
1
2
3
4
5
6
7
8
9
# 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文件,输入如下内容

?
1
2
3
<?php
echo phpinfo();
?>

五、启动服务

启动php-fpm和nginx

?
1
2
3
4
/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的生产环境-1猿站网-插图

七、错误解决 在使用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错误访问日志:
?
1
2
3
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报错日志:

?
1
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错误访问日志如下:

?
1
2
3
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生效的。

配置举例:

?
1
2
3
4
5
6
7
8
9
10
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;
}
声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:初级:在 Linux 中自定义 Cinnamon 桌面的 7 种方法

2023-5-2 23:55:28

建站知识

小知识:配置ab来为Nginx服务器做压力测试的方法

2023-5-3 1:04:10

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索