PHP+FastCGI+Nginx Configure PHP Runtime Environment

  • 2021-07-10 19:09:19
  • OfStack

CGI is called Common Gateway Interface (Common Gateway Interface) in English, which is a bridge between Nginx and dynamic script program. Nginx sends dynamic request to FastCGI through FastCGI interface, Wrapper process in FastCGI generates a thread, and gives the request to script interpreter for execution. Then the result after interpretation and execution is returned to Nginx through the original socket, and then Nginx gives the result to the client.

Nginx sends dynamic requests to wrapper through the socket file socket, using the Tcp protocol. wrapper accepts requests through the CGI interface. In this way, web server and interpreter can be developed independently, which avoids errors, crashes and security problems caused by interpreter calling the interface of server directly. Moreover, Nginx can concentrate on processing static page requests and forwarding dynamic requests, and install the script interpreter on another server, so that the pressure of the server can be shared.

CGI was developed as a patch to the PHP program. When installing PHP, first install its dependent libraries, and then add support for CGI-enable-fpm-enable-cgi and other options when compiling configuration parameters. To compile the extension module of PHP, phpize tool in php is needed to generate configure file when the module is compiled. If phpize cannot be generated when running, the reasons are as follows: autoconf software package is not installed.

The configuration file for the php-fpm process is/usr/local/php/etc/php-fpm. conf configures php-fpm accordingly.

Nginx configuration supports fastcgi:


location ~ \.php${

             root html;

             fastcgi_pass  unix:/tmp/fastcgi.soke  // Through socket files and cgi Establish a contact, and the file is in php-fpm.conf Settings in 

             fastcgi_index index.php;

             fastcgi_param SCRIPT_FILENAME html$SCRIPT_FILE_NAME; Setting parameters 

             include fastcgi_params; // Import fastcgi Parameter configuration file, which is in the nginx Automatically generated during installation. 

                   }


Related articles: