Install the configuration php-fpm to build the nginx+php production environment _nginx

Source: Internet
Author: User
Tags configuration php curl fpm openssl php script php source code connection reset

Nginx itself can not 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 the results returned to the client.

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

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

I. Compiling and installing PHP-FPM

What is PHP-FPM

PHP-FPM is a PHP fastcgi manager that is only used in 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 the PHP package. It must be patch to your PHP source code and can be used only after compiling and installing PHP.

The new version of PHP has been integrated PHP-FPM, no longer a third party package, recommended use. PHP-FPM provides a better way to manage PHP processes, can effectively control memory and process, can smooth overload PHP configuration, than spawn-fcgi has more advantages, so by the official PHP included. In the./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.

Prepare 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 

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

New 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 
--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 


Make all Install

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

The above completes the PHP-FPM installation.

The following is the setting for the PHP-FPM run user

cd/usr/local/php
cp etc/php-fpm.conf.default etc/php-fpm.conf
VI etc/php-fpm.conf

Modify

user = Www-data
group = Www-data

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

Groupadd www-data
useradd-g www-data www-data

Ii. Compiling and installing Nginx

Then follow the Http://www.nginx.cn/install installation Nginx

Iii. Modify the Nginx configuration file to support PHP-FPM

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

The server section increases the following configuration, pay attention to 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:9000;
Fastcgi_index index.php;
Fastcgi_param script_filename $document _root$fastcgi_script_name;
Include Fastcgi_params;
}

Create a test PHP file

Creating PHP Files

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

<?php
  Echo phpinfo ();
? >

V. 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

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
502 Bad Gateway and 504 Gateway time-out errors are often encountered when using Nginx, and the following nginx+php-fpm are used to analyze the causes and solutions for 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.
Both of these are used to configure the maximum execution time for a PHP script. When this time is exceeded, PHP-FPM will not only terminate the execution of the script,
The worker process that executes the script is also terminated. So Nginx will find that the connection with their own communication broken down, will return to the client 502 error.

In the case of php-fpm request_terminate_timeout=30 seconds, 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 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 just by making the values of these two items larger, you can let the PHP script not 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, then only change the PHP-FPM configuration on it.

Also noted are the Max_fail and fail_timeout two items in the Nginx upstream module. Sometimes nginx communication with upstream servers (such as Tomcat, FastCGI) is only accidentally broken off,
But if the Max_fail set is relatively small, then in the next fail_timeout time, Nginx will think that the upstream server is dead, will return 502 errors.
So you can turn the max_fail larger, the fail_timeout will be smaller.

2.504 Gateway time-out error

The maximum execution time for a php-fpm set is long enough, but when you execute a time-consuming php script, you find that the Nginx error changes from 502 to 504. What is this for?
Because we are modifying only the configuration of PHP, Nginx also has the configuration factcgi_connect/read/send_timeout with the upstream server communication timeout.

In the case of a nginx timeout of 90 seconds and a php-fpm timeout of 300 seconds, the Nginx error access log reported 504 Gateway timeout error is as follows:

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

The 504 errors are also resolved after the three-item values (mainly read and send two, which are not configured by default, are set to nginx the time-out period to 60 seconds).
Also, these three configurations can be configured at the HTTP, server level, or at the location level. If you are worried about influencing other applications, configure it in your own application location.
It is to be noted that the factcgi_connect/read/send_timeout is effective for the fastcgi, and the 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   180;
        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.