Installation configuration php-fpm to build nginx+php production environment

Source: Internet
Author: User
Tags configuration php php source code connection reset
This article mainly introduces the installation configuration php-fpm to build nginx+php production environment method, PHP-FPM role is to integrate FASTCGI process management into the PHP package, the need for friends can refer to the next

Nginx itself cannot handle PHP, it is just a Web server, when the request is received, if it is a PHP request, then sent to the PHP interpreter processing, and return the results to the client.

Nginx is generally the request to send fastcgi management process processing, FASCGI management process Select the CGI sub-process processing results and return to the Nginx

This article takes php-fpm as an example to explain how to make Nginx support PHP

First, compile and install PHP-FPM

What is PHP-FPM

PHP-FPM is a PHP fastcgi manager that is only for PHP and can be downloaded in http://php-fpm.org/download.

PHP-FPM is actually a patch of PHP source code designed to integrate FASTCGI process management into a PHP package. It must be patch into your PHP source code and can be used after compiling and installing PHP.

The new version of PHP has been integrated PHP-FPM, is no longer a third-party package, recommended to use. PHP-FPM provides a better way to manage the PHP process, can effectively control memory and process, can be smooth overloaded PHP configuration, more than spawn-fcgi has more advantages, so by the official PHP included. In./configure with the –ENABLE-FPM parameter can be opened PHP-FPM, the other parameters are configured PHP, the meaning of the specific options can be viewed here.

Pre-Installation Preparation
Executed under CentOS

Yum-y install gcc automake autoconf libtool makeyum-y install gcc gcc-c++ glibcyum-y install libmcrypt-devel Mhash-deve L 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 libid n Libidn-devel OpenSSL Openssl-devel

New PHP-FPM installation (recommended installation method)

wget http://cn2.php.net/distributions/php-5.4.7.tar.gztar zvxf php-5.4.7.tar.gzcd 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

Both of these methods can be installed PHP-FPM, after the installation content in the/usr/local/php directory

The above completed the installation of PHP-FPM.

Here are the settings for the PHP-FPM running user

CD/USR/LOCAL/PHPCP Etc/php-fpm.conf.default Etc/php-fpm.confvi etc/php-fpm.conf

Modify

user = Www-datagroup = Www-data

If the Www-data user does not exist, add the Www-data user first

Groupadd www-datauseradd-g Www-data Www-data

Second, compile and install Nginx

Third, modify the Nginx configuration file to support PHP-FPM

After Nginx installation is complete, modify nginx config file to nginx.conf

Where the server segment adds the following configuration, note the Red content configuration, otherwise there will be no input file specified. Error

# Pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000#location ~. php$ {root Html;fastcgi_pass 127.0.0.1:90 00;fastcgi_index index.php;fastcgi_param script_filename $document _root$fastcgi_script_name;include fastcgi_params ;}

Iv. Creating a test PHP file

Creating PHP Files

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

<?php  Echo phpinfo ();? >

Five, start the service

Start PHP-FPM and Nginx

/USR/LOCAL/PHP/SBIN/PHP-FPM #手动打补丁的启动方式/usr/local/php/sbin/php-fpm Startsudo/usr/local/nginx/nginx

PHP-FPM Close restart See end of article

VI. Browser access

Visit http://Your server ip/index.php, you can see the PHP information.


Vii. Error Resolution
In the use of nginx, often encounter 502 bad Gateway and 504 Gateway time-out error, the following NGINX+PHP-FPM to analyze the reasons for these two common errors and solutions.

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.
Both of these are used to configure the maximum execution time for a PHP script. When this time is exceeded, php-fpm not only terminates the execution of the script,
The worker process that executes the script is also terminated. So Nginx will find that the connection with its own communication has been broken, it will return to the client 502 error.

Take php-fpm request_terminate_timeout=30 seconds as an example to report the specific information of the 502 bad gateway error 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 fr Om 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 error log:

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

So simply making the values of these two items larger will allow the PHP script not to be terminated because of the long execution time. Request_terminate_timeout can cover Max_execution_time,
So if you do not want to change the overall php.ini, that only change the PHP-FPM configuration can be.

Also note is the Nginx upstream module in the Max_fail and fail_timeout two items. Sometimes the nginx communication with the upstream server (such as Tomcat, FastCGI) is only accidentally broken off,
But max_fail if the setting is relatively small, then in the next fail_timeout time, Nginx will assume that the upstream server hangs, will return 502 error.
So you can adjust the max_fail a little bit, will be fail_timeout smaller.

2.504 Gateway time-out error

The maximum execution time for a script set by PHP-FPM is long enough, but when executing a time-consuming PHP script, it turns out that the Nginx error has changed from 502 to 504. What is this for?
Because we are only modifying the configuration of PHP, Nginx also has a communication with the upstream server time-out configuration factcgi_connect/read/send_timeout.

To Nginx timeout time is 90 seconds, PHP-FPM timeout time is 300 seconds for example, reported 504 Gateway timeout error when the Nginx error access log as follows:

   2013/09/19 00:55:51 [ERROR] 27600#0: *78877 upstream timed out (110:connection timed off) while reading response header F Rom 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 "

When the value of these three items is increased (mainly read and send two entries, the default is not configured, Nginx will set the timeout to 60 seconds), the 504 error is resolved.
And these three configurations can be configured at HTTP, server level, or at the location level. If you're worried about affecting other applications, configure them in your app's location.
Note that factcgi_connect/read/send_timeout is effective for fastcgi, and proxy_connect/read/send_timeout is effective for proxy_pass.

Configuration examples:

Location ~ \.php$ {        root          /home/cdai/test.com;        Include         Fastcgi_params;        Fastcgi_connect_timeout   ;        Fastcgi_read_timeout      ;        Fastcgi_send_timeout      ;        Fastcgi_pass      Unix:/dev/shm/php-fcgi.sock;        Fastcgi_index      index.php;        Fastcgi_param     script_filename/home/cdai/test.com$fastcgi_script_name;   }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.