installation, configuration and optimization of Nginx and PHP (FastCGI)

Source: Internet
Author: User
Tags fpm install php php and php source code socket versions wrapper nginx server

One, what is FastCGI

FastCGI is a scalable, high-speed interface for communicating between HTTP server and dynamic scripting languages. Most popular HTTP servers support FASTCGI, including Apache, Nginx, and lighttpd, and fastcgi are supported by a number of scripting languages, including PHP. FastCGI is from the CGI development improvement. The main disadvantage of the traditional CGI interface approach is poor performance, because every time the HTTP server encounters a dynamic program, it needs to restart the script parser to perform the parsing, and the result is returned to the HTTP server. This is almost not available when handling high concurrent access. In addition, the traditional CGI interface is also very poor security, it is now rarely used. The FastCGI interface adopts the C/s structure, which separates the HTTP server from the script resolution server, and initiates one or more script resolution Daemons on the script resolution server. Each time the HTTP server encounters a dynamic program, it can be delivered directly to the fastcgi process to execute, and the resulting results are returned to the browser. This approach allows HTTP servers to handle static requests in a single-minded manner or to return the results of a dynamic script server to the client, which greatly improves the performance of the entire application system.

Second, nginx+fastcgi operating principle Nginx does not support direct calls or parsing of external programs, all external programs (including PHP) must be invoked through the FastCGI interface. FastCGI interface in Linux is the socket, (this socket can be a file socket, can also be an IP socket). To invoke a CGI program, you also need a fastcgi wrapper (wrapper can be understood as a program to start another program), which is bound to a fixed socket, such as a port or a file socket. When Nginx sends a CGI request to this socket, the FastCGI interface wrapper the request and derives a new thread that invokes the interpreter or external program to process the script and read the return data; Wrapper then passes the returned data through the FastCGI interface to the nginx along the fixed socket, and Nginx sends the returned data to the client, which is the whole operation of the nginx+fastcgi. Detailed procedures, as shown in Figure 1.

Fig. 1 Operation Principle of nginx+fastcgi

Iii. spawn-fcgi and PHP-FPM

As described earlier, the FastCGI interface method initiates one or more daemons on a script resolution server to parse dynamic scripts, which are fastcgi process managers, or fastcgi engines, SPAWN-FCGI and PHP-FPM are the two fastcgi process managers that support PHP. The following is a brief introduction to the similarities and differences between spawn-fcgi and PHP-FPM.

SPAWN-FCGI is a part of the HTTP server lighttpd, has now become a stand-alone project, generally with LIGHTTPD with the use to support PHP, but ligttpd spwan-fcgi in high concurrent access, There is a memory leak or even an automatic reboot of the fastcgi problem.

Nginx is a lightweight HTTP server, you must use a Third-party fastcgi processor to be able to parse the PHP, so the combination of nginx+spawn-fcgi can also achieve the interpretation of PHP, not too much to tell.

PHP-FPM is also a third-party fastcgi process manager, which is developed as a patch in PHP, also need to compile with the PHP source code when installing, that is to say, PHP-FPM is compiled into the PHP kernel, so it is better to handle the performance At the same time it is much better at handling high concurrency than the spawn-fcgi engine, so it is recommended that nginx+php/php-fpm this combination to parse PHP.

The main advantage of FastCGI is the separation of the dynamic language and HTTP server, so nginx and PHP/PHP-FPM are often deployed on different servers to share the pressure of the front-end Nginx server, so that the nginx single-minded processing of static requests and forwarding dynamic requests, while PHP The/PHP-FPM server is dedicated to parsing PHP dynamic requests.

Iv. installation and optimization of PHP and PHP-FPM

1. Download the installation package from the Www.php.net official website Download the PHP source code package, here is a stable version of the download php-5.2.13.tar.gz. Download the corresponding PHP-FPM source package from http://php-fpm.org/downloads/, here is php-5.2.13-fpm-0.5.13.diff.gz. Note that when downloading the package version, try to keep the PHP and PHP-FPM versions consistent, and compatibility issues can occur if the versions are too different.

2. Configure installation environment to install PHP requires the following package support, if not installed, please install yourself.

GCC gcc-c++ libxml2 libxml2-devel autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel zlib zlib-de Vel glibc glibc-devel glib2 glib2-devel

Because of the uncertainty of each Linux system version, the reader can install the corresponding software library in the course of installing PHP according to the error message.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.