Introduction and working principle of FastCGI
First, let’s briefly introduce FastCGI:
FastCGI is a language-independent, scalable structure CGI open extension. Its main behavior is to keep the CGI interpreter in memory and thus obtain higher performance. As we all know, repeated loading of the CGI interpreter is the main reason for low CGI performance. If the CGI interpreter remains in memory and accepts FastCGI process manager scheduling, it can provide good performance, scalability, Fail-Over features, etc.
The working principle of FastCGI is:
(1) The FastCGI process manager initializes itself, starts multiple CGI interpreter processes (multiple php-cgi processes) and waits for connections from the Web Server. In the text, the PHP-FPM process manager is used to start multiple php-cgi FastCGI processes. When starting the php-cgi FastCGI process, you can configure it to start in two ways: TCP and UNIX socket.
(2) When the client request reaches the Web server (Nginx), the Web server forwards the request to the FastCGI main process using the TCP protocol or UNIX socket method. The FastCGI main process selects and connects to a CGI interpreter (sub- process). The web server sends CGI environment variables and standard input to the FastCGI child process php-cgi.
(3) After the FastCGI sub-process completes processing, it returns standard output and error information to the Web server (Nginx) from the same connection. When the FastCGI child process closes the connection, the request is notified that processing is complete. The FastCGI child process then waits for and handles the next connection from the FastCGI process management. In the normal CGI mode, php-cgi exits here.
PHP-FPM
PHP-FPM is a PHP FastCGI manager, which is only used for PHP. It can be downloaded at http://cn2.php.net/downloads.php. PHP-FPM is actually a patch of the PHP source code. Aims to integrate FastCGI process management into PHP packages. It must be patched into your PHP source code and can be used after compiling and installing PHP.
The new version of PHP has integrated php-fpm, which is no longer a third-party package. It is recommended to use it. PHP-FPM provides a better PHP process management method, can effectively control memory and processes, and can smoothly reload PHP configuration. It has more advantages than spawn-fcgi, so it is officially included in PHP. You can turn on PHP-FPM with the –enable-fpm parameter in ./configure. Other parameters are configured for php. The specific meaning of the options can be found here.
Preparation before installation:
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 version of php-fpm installation (recommended installation method)
wget http://us1.php.net/get/php-5.5.10.tar.gz/from/this/mirror
tar zvxf php-5.5.10.tar.gz
cd php-5.5.10
./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
After completing php-fpm, configure its running user:
cd /usr/local/php
cp etc/php-fpm.conf.default etc/php-fpm.conf
vi etc/php-fpm.conf
Modify:
user = nginx
group = nginx
If the nginx user does not exist, add the nginx user first
groupadd nginx
useradd -g nginx nginx
Modify nginx configuration file to support php-fpm
# 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 test php file
Create the index.php file under /usr/local/nginx/html and enter the following content
echo phpinfo();
?>
Start php-fpm and nginx
1
2
/usr/local/php/sbin/php-fpm
/usr/local/nginx/nginx
http://www.bkjia.com/PHPjc/755758.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/755758.htmlTechArticleIntroduction and working principle of FastCGI First, let’s briefly introduce FastCGI: FastCGI is a language-independent, scalable structure CGI Open extension, its main behavior is to keep the CGI interpreter in memory...