Compile PHP and integrate with Nginx
Nginx itself cannot process PHP. It is only a web server. After receiving the request, if it is a php request, it is sent to the php interpreter for processing and the result is returned to the client.
Nginx generally sends a request to the fastcgi management process for processing. The fascgi management process selects the cgi sub-process for processing the result and returns Nginx.
I. First, let's get to know the operating principles of FastCGI and Ngnix + FastCGI.
1. Differences between apache + PHP and ngnix + php
Apache generally uses php as a module to start. While ngnix forwards the http request variable to the php process, that is, the independent php process, and communicates with ngnix, this method is called FastCGI running mode.
Therefore, php compiled by apache cannot be used in ngnix.
Let's first look at a simple figure:
The following section I think is the most easy-to-understand explanation on the Internet. I copied it for your convenience:
1. What is FastCGI?
FastCGI is a scalable and fast interface for communication between HTTP server and dynamic scripting language. Most popular HTTP servers support FastCGI, including Apache, Nginx, and lighttpd. FastCGI is also supported by many scripting languages, including PHP.
FastCGI is improved from CGI development. The main disadvantage of the traditional CGI interface method is poor performance, because every time the HTTP server encounters a dynamic program, it needs to restart the script parser to execute the parsing and then return the result to the HTTP server. This is almost unavailable when processing highly concurrent access. In addition, the traditional CGI interface method has poor security and is rarely used now.
The FastCGI interface adopts the C/S structure, which can separate the HTTP server and the script parsing server, and start one or more script parsing Daemon Processes on the script parsing server. When the HTTP server encounters a dynamic program, it can be directly delivered to the FastCGI process for execution, and then the result is returned to the browser. This method allows the HTTP server to process static requests or return the results of the dynamic script server to the client, which greatly improves the performance of the entire application system.
2. operating principles of Nginx + FastCGI
Nginx does not support direct calling or parsing of external programs. All external programs (including PHP) must be called through the FastCGI interface. The FastCGI interface is a socket in Linux (this socket can be a file socket or an ip socket ).
Wrapper: to call the CGI program, a FastCGI wrapper (wrapper can be understood as the program used to start another program) is also required. This wrapper is bound to a fixed socket, such as port or file socket. When Nginx sends a CGI request to this socket, through the FastCGI interface, wrapper receives the request and Fork (derived) generates a new thread, this thread calls the interpreter or external program to process the script and read the returned data. Then, wrapper transmits the returned data to Nginx through the FastCGI interface along a fixed socket. Finally, nginx sends the returned data (html pages or images) to the client. This is the entire operation process of Nginx + FastCGI,
Therefore, we need a wrapper first. The wrapper must complete the following work:
1. Call the fastcgi (Library) function to communicate with ningx through socket (read/write socket is implemented inside fastcgi and is non-transparent to wrapper)
2. Schedule thread for fork and kill
3. Communicate with application (php)
2. Compile PHP
First, let's get to know a concept: php-fpm
(1) PHP-FPM is a PHP FastCGI manager that is only used for PHP and can be downloaded at http://php-fpm.org/download
(2) PHP-FPM is actually a PHP source code patch, designed to integrate FastCGI process management into the PHP package. You must patch it to your PHP source code before using it after compiling and installing PHP.
(3) the new version of PHP has integrated php-fpm, in the./configure with-enable-fpm parameters to open the PHP-FPM
Next we will start to install and compile PHP
1. Before compilation, We need to install some libraries required by php (I use the CentOS linux system)
Yum-y install gcc automake autoconf libtool make
Yum-y install gcc-c ++ glibc
Yum-y install libmcrypt-devel mhash-devel libxslt-devel \
Libjpeg-devel libpng-devel freetype-devel libxml2 libxml2-devel \
Zlib-devel glibc-devel glib2 glib2-devel bzip2 bzip2-devel \
Ncurses-devel curl-devel e2fsprogs e2fsprogs-devel \
Krb5 krb5-devel libidn-devel openssl-devel
2. Obtain the php source code package.
Wget http://cn2.php.net/distributions/php-5.4.7.tar.gz
3. decompress the package and enter the php Directory.
Tar zvxf php-5.4.7.tar.gz
Cd php-5.4.7
4. Compile and install
./Configure -- prefix =/usr/local/fastphp -- 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
5. Copy the php-fpm File
6. Copy the php configuration file
7. Modify the nginx configuration file to support php-fpm
First open the ngnix configuration file
Vim/usr/local/ngnix/conf/ngnix. conf
Add the following code to the server segment:
Save
8. Start php-fpm and ngnix
/Usr/local/ngnix
/Usr/local/ngnix/sbin/php-fpm
9. Create the test file test. php In the root directory for a simple test.
10. Test successful
Signal control is required for the shutdown and restart of php-fpm, which is the same as that of ngnix.
Php-fpm close: kill-INT 'cat/usr/local/fastphp/var/run/php-fpm.pid'
Php-fpm restart: kill-USR2 'cat/usr/local/fastphp/var/run/php-fpm.pid'
View the number of php-fpm processes: ps aux | grep-c php-fpm
For Ngnix installation, see my other article.
-------------------------------------- Split line --------------------------------------
Deployment of Nginx + MySQL + PHP in CentOS 6.2
Build a WEB server using Nginx
Build a Web server based on Linux6.3 + Nginx1.2 + PHP5 + MySQL5.5
Performance Tuning for Nginx in CentOS 6.3
Configure Nginx to load the ngx_pagespeed module in CentOS 6.3
Install and configure Nginx + Pcre + php-fpm in CentOS 6.4
Nginx installation and configuration instructions
Nginx log filtering using ngx_log_if does not record specific logs
Nginx details: click here
Nginx: click here
This article permanently updates the link address: