Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use php-fpm to build the production Environment of Nginx+PHP

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces how to use php-fpm to build Nginx+PHP production environment related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will have something to gain after reading this article on how to use php-fpm to build Nginx+PHP production environment, let's take a look.

First, compile and install php-fpm

What is php-fpm?

Php-fpm is a php fastcgi manager that is available only for php and can be downloaded from http://php-fpm.org/download.

Php-fpm is actually a patch to the php source code designed to integrate fastcgi process management into the php package. You must patch it into your php source code before you can use it after compiling and installing php.

The new version of php has integrated php-fpm, is no longer a third-party package, and is recommended. Php-fpm provides a better way of php process management, can effectively control memory and processes, can smoothly reload php configuration, and has more advantages than spawn-fcgi, so it is officially included by php. You can enable php-fpm with-enable-fpm parameter in. / configure. Other parameters are configured with php. For more information, please see here.

Pre-installation preparation

Execute under centos

Yum-y install gcc automake autoconf libtool makeyum-y install gcc gcc-c++ glibcyum-y install libmcrypt-devel mhash-devel libxslt-devellibjpeg 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

Installation of the new version of php-fpm (recommended)

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-dirmake all install

Php-fpm can be installed in both ways, and the contents are placed in the / usr/local/php directory after installation.

The above completes the installation of php-fpm.

Here are the settings for the php-fpm runner

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

II. Compile and install nginx

Then install nginx according to http://www.nginx.cn/install

Modify the nginx configuration file to support php-fpm

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

Add the following configuration to the server section. Pay attention to the red content configuration, otherwise no input file specified will appear. 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 index.php;fastcgi_param script_filename 9000 fastcgival index $document_root$fastcgi_script_name;include fastcgi_params;}

Create a test php file

Create a php file

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

V. start the service

Start php-fpm and nginx

/ usr/local/php/sbin/php-fpm # how to start patching manually / usr/local/php/sbin/php-fpm startsudo / usr/local/nginx/nginx

Php-fpm shutdown and restart see the end of the article

VI. Browser access

Visit http:// your server ip/index.php, and you can see php messages.

VII. Error resolution

502 bad gateway and 504 gateway time-out errors are often encountered when using nginx. Here is an analysis of the causes and solutions of these two common errors with nginx+php-fpm.

1.502 bad gateway error

There are two configuration items in php.ini and php-fpm.conf: max_execution_time and request_terminate_timeout, respectively.

Both items are used to configure the maximum execution time of 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 to communicate with itself is broken and will return the 502 error to the client.

Taking the request_terminate_timeout=30 second time of php-fpm as an example, the specific message that 502 bad gateway error is reported is as follows:

1) nginx error access log:

2013-09-19 01:09:00 [error] 27600: 0: * 78887 recv () failed 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 error log:

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

So just increase the values of these two items so that the php script will not be terminated because of the long execution time. Request_terminate_timeout can overwrite max_execution_time

So if you don't want to change the global php.ini, just change the configuration of php-fpm.

Also note the max_fail and fail_timeout items in nginx's upstream module. Sometimes the communication between nginx and upstream servers (such as tomcat, fastcgi) is only accidentally cut off

However, if max_fail is set to a small size, nginx will think that the upstream server is down during the next fail_timeout time and will return a 502 error.

So you can make the max_fail larger and the fail_timeout smaller.

2.504 gateway time-out error

The maximum execution time of the script set by php-fpm is long enough, but when executing the time-consuming php script, it is found that the nginx error has changed from 502 to 504. Why is that?

Because we are only modifying the configuration of php, there is also a configuration factcgi_connect/read/send_timeout in nginx about the timeout of communicating with upstream servers.

For example, the nginx timeout is 90 seconds and the php-fpm timeout is 300 seconds. When a 504 gateway timeout error is reported, the nginx error access log is as follows:

2013-09-19 00:55:51 [error] 27600: 0: * 78877 upstream timed out (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"

After increasing the values of these three items (mainly read and send, if not configured by default, nginx will set the timeout to 60 seconds), the 504 error has also been resolved.

And these three configurations can be configured at the http, server level, or location level. If you are worried about affecting other applications, configure it in the location of your own application.

Note that factcgi_connect/read/send_timeout is valid for fastcgi, while proxy_connect/read/send_timeout is for proxy_pass.

Configuration example:

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;} this is the end of the article on "how to build a Nginx+PHP production environment with php-fpm". Thank you for reading! I believe you all have a certain understanding of "how to use php-fpm to build a production environment for Nginx+PHP". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report