current position:Home>How to build the production environment of nginx + PHP with PHP FPM

How to build the production environment of nginx + PHP with PHP FPM

2022-04-29 19:27:04Billion cloud speed

How to use it? php-fpm build Nginx+PHP Production environment of

This article mainly introduces how to use it php-fpm build Nginx+PHP Knowledge of production environment , The content is detailed and easy to understand , The operation is simple and fast , It has certain reference value , I believe you can use it after reading this article php-fpm build Nginx+PHP All articles on production environment will be fruitful , Let's have a look .

One 、 Compilation and installation php-fpm

What is? php-fpm

php-fpm It's a php fastcgi Manager , It's only for php Of , Can be in http://php-fpm.org/download Download it .

php-fpm It's actually php A patch of the source code , The aim is to fastcgi Process management is integrated into php In bag . It has to be patch To your php In the source code , Install... In compilation php You can use it later .

new edition php Has integrated php-fpm 了 , It's no longer a third-party package , Recommended .php-fpm Provides better php Process management , Can effectively control memory and processes 、 You can smooth overload php To configure , Than spawn-fcgi Have more advantages , So be php Officially included . stay ./configure Bring... When –enable-fpm Parameters can be turned on php-fpm, Other parameters are configuration php Of , You can see the meaning of specific options here .

Preparation before installation
centos perform

yum -y install gcc automake autoconf libtool makeyum -y install gcc gcc-c++ glibcyum -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 edition php-fpm install ( 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 the above methods can be installed php-fpm, After installation, the contents are placed in /usr/local/php Under the table of contents

 How to use it? php-fpm build Nginx+PHP Production environment of

That's it php-fpm Installation .

The following is true. php-fpm Run the user to set

cd /usr/local/phpcp etc/php-fpm.conf.default etc/php-fpm.confvi etc/php-fpm.conf

modify

user = www-datagroup = www-data

If www-data The user doesn't exist , Then add it first www-data user

groupadd www-datauseradd -g www-data www-data

Two 、 Compilation and installation nginx

And then according to http://www.nginx.cn/install install nginx

3、 ... and 、 modify nginx Configuration files to support php-fpm

nginx After installation , modify nginx The configuration file is ,nginx.conf

among server Add the following configuration to the section , Pay attention to the red content configuration , Otherwise 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;}

Four 、 Create the test php file

establish php file

stay /usr/local/nginx/html Create index.php file , Enter the following

<?php  echo phpinfo();?>

5、 ... and 、 Start the service

start-up php-fpm and nginx

/usr/local/php/sbin/php-fpm # The startup mode of manual patching /usr/local/php/sbin/php-fpm startsudo /usr/local/nginx/nginx

php-fpm Shut down and restart, see the end of the article

6、 ... and 、 Browser access

visit http:// Yours The server ip/index.php, Can be seen php Information. .

 How to use it? php-fpm build Nginx+PHP Production environment of

7、 ... and 、 Error resolution
In the use of nginx when , I often come across 502 bad gateway and 504 gateway time-out error , Let's say nginx+php-fpm To analyze the causes and solutions of these two common errors .

1.502 bad gateway error

stay php.ini and php-fpm.conf There are two configuration items in :max_execution_time and request_terminate_timeout.
Both are used to configure a php The maximum execution time of the script . When the time is over ,php-fpm It will not only terminate the execution of the script ,
It also terminates the execution of the script worker process . therefore nginx You will find that the connection to your communication is broken , It will be returned to the client 502 error .

With php-fpm Of request_terminate_timeout=30 Second time as an example , newspaper 502 bad gateway The details of the error are 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"

2)php-fpm Error log :

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

So just increase the value of these two items to make php The script will not be terminated due to long execution time .request_terminate_timeout You can override max_execution_time,
So if you don't want to change the overall situation php.ini, That one changed php-fpm The configuration is OK .

Besides, pay attention to nginx Of upstream Module max_fail and fail_timeout two . Sometimes nginx With upstream servers ( Such as tomcat、fastcgi) Our communication was just broken by accident ,
but max_fail If the setting is small , So in the next fail_timeout Within time ,nginx Will think that the upstream server is down , Will return to 502 error .
So you can max_fail Turn up some , take fail_timeout Turn it down .

2.504 gateway time-out error

php-fpm The maximum execution time of the set script is long enough , But execution takes time php Script time , Find out nginx Error reporting from 502 Turn into 504 了 . Why is that ?
Because what we modified was just php Configuration of ,nginx There is also the configuration of communication timeout with upstream servers in factcgi_connect/read/send_timeout.

With nginx The timeout is 90 second ,php-fpm The timeout is 300 Second as an example , newspaper 504 gateway timeout On error nginx The error access log is as follows :

   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"

Increase the values of these three items ( Mainly read and send two , If it is not configured by default nginx The timeout will be set to 60 second ) after ,504 The error has also been solved .
And these three configurations can be configured in http、server Level , It can also be configured in location Level . If you're worried about affecting other applications , Just configure it in your own application location In the! .
It should be noted that factcgi_connect/read/send_timeout It's right fastcgi Effective , and proxy_connect/read/send_timeout It's right proxy_pass Effective .

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;   }

About “ How to use it? php-fpm build Nginx+PHP Production environment of ” That's all for this article , Thank you for reading ! I'm sure you're right “ How to use it? php-fpm build Nginx+PHP Production environment of ” Knowledge has a certain understanding , If you want to learn more , Welcome to the Yisu cloud industry information channel .

copyright notice
author[Billion cloud speed],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204291744570766.html

Random recommended