Home > Backend Development > PHP Tutorial > Install and configure php-fpm to build an Nginx+PHP production environment

Install and configure php-fpm to build an Nginx+PHP production environment

不言
Release: 2023-03-29 12:42:01
Original
13000 people have browsed it

This article mainly introduces how to install and configure php-fpm to build a Nginx PHP production environment. The function of php-fpm is to integrate FastCGI process management into the PHP package. Friends who need it can refer to it

nginx itself cannot handle PHP. It is just a web server. When a request is received, if it is a PHP request, it will be sent to the PHP interpreter for processing and the result will be returned to the client.

nginx generally sends the request to the fastcgi management process for processing. The fascgi management process selects the cgi sub-process processing result and returns it to nginx

This article uses php-fpm as an example to introduce how to make nginx support PHP

1. Compile and install php-fpm

What is PHP-FPM

PHP-FPM is a PHP FastCGI manager, which is only used for PHP It can be downloaded at http://php-fpm.org/download.

PHP-FPM is actually a patch of the PHP source code, aiming to integrate FastCGI process management into the PHP package. It must be patched into your PHP source code, and it can be used after compiling and installing PHP.

The new version of PHP has integrated php-fpm, which is no longer a third-party package. It is recommended to use it. PHP-FPM provides a better PHP process management method, which can effectively control memory and processes, and can smoothly reload PHP configuration. It has more advantages than spawn-fcgi, so it is officially included in PHP. You can turn on PHP-FPM with the –enable-fpm parameter in ./configure. Other parameters are configured for php. The specific meaning of the options can be found here.

Preparation before installation
Execute under 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
Copy after login

##

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
Copy after login

New version of php- fpm installation (recommended installation method)

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
Copy after login

--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
Copy after login

make all install
Copy after login

Both of the above methods can be used to install php-fpm. After installation, the content is placed in the /usr/local/php directory


201615100155789.png (300×41)

The above completes php- Installation of fpm.

The following is to set the php-fpm running user

cd /usr/local/php
cp etc/php-fpm.conf.default etc/php-fpm.conf
vi etc/php-fpm.conf
Copy after login

Modify


user = www-data
group = www-data
Copy after login

If the www-data user does not exist, then add the www-data user first


groupadd www-data
useradd -g www-data www-data
Copy after login

2. Compile and install nginx

3. Modify the nginx configuration file to support php-fpm

After the nginx installation is completed, modify the nginx configuration file to, nginx.conf

Add the following configuration to the server section, pay attention to the configuration marked in red, otherwise the No input file specified. error will appear

# 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;
}
Copy after login

##four , Create a test php file

Create a php file

Create the index.php file under /usr/local/nginx/html and enter the following content

<?php
  echo phpinfo();
?>
Copy after login

5. Start the service

Start php-fpm and nginx

/usr/local/php/sbin/php-fpm 
#手动打补丁的启动方式/usr/local/php/sbin/php-fpm start

sudo /usr/local/nginx/nginx
Copy after login

php-fpm Close and restart, see the end of the article

6. Browser access

Visit http://your server ip/index.php, all can be seen To the php information.


201615100326710.png (300×120)


##7. Error resolution

When using Nginx, you often encounter 502 Bad Gateway and 504 Gateway Time-out errors. Let’s use Nginx PHP-FPM to analyze the causes and solutions of these two common errors.
1.502 Bad Gateway error

There are two configuration items in php.ini and php-fpm.conf respectively: max_execution_time and request_terminate_timeout. These two items are used to configure the maximum execution time of a PHP script. When this time is exceeded, PHP-FPM will not only terminate the execution of the script,

will also terminate the Worker process that executes the script. Therefore, Nginx will find that the connection to communicate with itself is broken, and will return a 502 error to the client.


Take PHP-FPM's request_terminate_timeout=30 seconds as an example. The specific information of the 502 Bad Gateway error is as follows:
1) Nginx error access log:



   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"
Copy after login

2) PHP-FPM error log:


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

So just change the values ​​of these two items Adjusting it larger can prevent the PHP script from being terminated due to long execution time. request_terminate_timeout can override max_execution_time,

So if you don’t want to change the global php.ini, then just change the configuration of PHP-FPM.

In addition, we should pay attention to the max_fail and fail_timeout items in the upstream module of Nginx. Sometimes the communication between Nginx and the upstream server (such as Tomcat, FastCGI) is only interrupted accidentally.
But if max_fail is set to a relatively small value, then in the next fail_timeout time, Nginx will think that the upstream server is down, and it will Returns 502 error.

So you can increase max_fail and decrease fail_timeout.



2.504 Gateway Time-out error

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"
Copy after login

调高这三项的值(主要是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;
   }
Copy after login

The above is the detailed content of Install and configure php-fpm to build an Nginx+PHP production environment. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template