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:04【Billion 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
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. .
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
The sidebar is recommended
- How to use HTML to display segmentation
- How to define and use HTML box model
- How to use the box pack attribute and box align attribute inside the box in HTML
- What are the differences and relationships among HTML, CSS and JS
- How to save home page as HTML
- How to solve the post request return 405 of nginx reverse proxy to HTML page
- How to upload pictures without refresh with HTML5 + PHP
- How to define HTML text tags, pictures, paths, hyperlinks and anchors
- How to upload static HTML files to the host or server
- How to use calculated and watch in Vue
guess what you like
How does Vue Preview PDF, word, xls, PPT and txt files
Can jQuery listen for events
Luxury cars "senseless price increase", the configuration of the new Porsche Macan remains unchanged, with a maximum increase of 19000 yuan
1-ch579m program upgrade OTA (self built Internet of things platform) - ch579m uses its own Ethernet to remotely download and upgrade MCU program through HTTP (MCU program rotation check and update)
The front-end downloads files, and the back-end sends gzip files. Is there a way to get the file size?
Why does Vue route jump refresh the page?
The conversion code of Babel in the project needs to include node_ Modules
[nginx] prefix removal in nginx proxy pass configuration
Vue packaging error module build failed: typeerror: this getOptions is not a function
Use of I18N in Vue
Random recommended
- Who is bunk, the new hero of endless dungeon? Introduction to new hero bunk skills
- Front end entry first bullet | development tools and configuration
- Nice HTML login page
- Vue error prone summary
- Summary of material resources website (icon | wallpaper | Adobe plug-in | jQuery plug-in | badge | online conversion | color splicing | continuous update)
- Step by step, configure nodesjs environment with hbuilder for Vue JS development
- Vue sets the global websocket and accepts message notification processing on the corresponding page
- Solve the problem that vuex data is reset during page refresh
- Java data structures and algorithms lesson 2 - generics
- Leetcode-82 - delete duplicate Element II in the sorting linked list
- [front end] quickly create HTML
- Vue router routing navigation
- Vue router redirection and alias
- Vue router named routes, (nested) named views
- Dynamic components & slots & custom instructions for getting started with Vue Foundation
- JavaScript algorithm -- heap sorting
- K8s deployment-43-take you to learn ingress nginx (Part 2)
- Detailed breakdown of actual combat of CMS background system project (VII)
- Explore the store | new architecture blessing, performance and safety. This style is super personality_ Integra_ Honda_ platform
- Sports kit blessing / replacement of four cylinder engine Chevrolet chuangku RS live shooting analysis_ Aspect_ Display screen_ Complete vehicle
- Talking about nodejs server
- Node. js&lt; I & gt—— Encounter node and repl usage
- Vue basic API: calculation attribute + filter + listener
- 1-stm32 + mn316 (nb-iot) remote upgrade OTA (self built Internet of things platform) - STM32 uses HTTP to download program files and upgrade programs through mn316 (MCU program rotation check and update)
- Vue Axios response interception
- vue3 ref
- How does Vue transfer the data from the parent component to the child component intact?
- The back-end interface developed by springboot in idea and the Vue front-end developed by vscode. How to integrate Vue code into springboot?
- Fried cold rice series 4: scope and closure in JavaScript
- Typescript type compatibility learning
- Summary of bugs encountered in front-end development
- Chrome developer tool: performance analysis using web panel
- Collation of common semantic elements and global attributes in HTML
- Life cycle in Vue
- 5.1 fear of traffic jam? With a budget of less than 100000, these cars with adaptive cruise make it easy for you to travel
- Docker compose deploy nginx configure SSL
- The content of element type “mapper“ must match “(cache-ref|cache|resultMap*|parameterMap*|sql*|inse
- -CSS-
- Vue uses two-way binding to implement the user registration page
- Is Infiniti qx60 worth less than 400000 yuan? It depends on the discount