Comparison of three installation modes of PHP

I recently installed a LAMP application server. The installation process is not too problematic, but I encountered a problem that can be considered, that is , several installation modes of PHP : cgi mode, module mode and fastcgi mode.
Collect information on the Internet, and my personal understanding after reading it:
1. cgi mode
The early traditional cgi mode is actually that the webserver process fork out the cgi sub-process to process the request of the dynamic web page. Specifically, after the webserver receives a user request, it must fork a process of a separate cgi program to process. The cgi program handles the corresponding processing according to the parameters submitted by the request, and then outputs the standard html statement and returns it to the webserver, and then forks it out. The cgi process exits, and the webserver returns to the client. In this way, the webserver and the specific processing program are independent, and the responsibilities are clearly defined. Problems in the cgi processing process will not affect the normal work of the webserver. However, in the case of high traffic, the webserver to fork the process will cause the load to exceed the standard and resources. Serious consumption, affecting server performance.

2. The dynamic library method (module method) The
cgi mode affects performance and has seriously damaged its reputation. Now there is a way to solve it, that is, the module design. The function of the cgi program is replaced by the extension module on the webserver. The webserver can Built-in perl interpreter or php interpreter. That is to say, the way these interpreters are made into modules can be inserted into the webserver's own process processing, and the webserver will start these interpreters when it starts. When a new dynamic request comes in, the webserver parses these perl or php scripts by itself, saving a new fork process and improving the efficiency. But in this case, different webservers have different ways to extend modules, for example: isapi of IIS, dso (dynamic library) of apache. For different webservers, they must be developed according to different standards, and it is impossible to achieve webserver independence. Once a problem occurs, the entire web server processing process will be affected.
Take php as an example: the option –with-apxs2=/usr/local/apache2/bin/apxs needs to be added after ./configure during installation, which means compiling php into a functional module of apache. The official explanation: apxs is A tool for compiling and installing extension modules for the Apache HTTP server, which is used to compile one or more source or object code files into dynamic shared objects that can be loaded into the Apache server at runtime using the LoadModule directive provided by mod_so. After the installation is complete, there will be an additional libphp5.so dynamic library in the apache2/module directory, and LoadModule will load this library in http.conf. At this point, the webserver can use the module call to process dynamic page requests.

3. fastcgi mode
We have another option, which is fastcgi. fastcgi is an extension based on the cgi architecture . Its core idea is to establish an intelligent and sustainable middle layer between the webserver and the specific cgi program to manage the operation of the cgi program, so that the webserver only needs to submit requests to this layer, this layer Then derive several reusable cgi program instances, and then distribute requests to these instances. These instances are controllable, sustainable, and reusable. Therefore, on the one hand, repeated forks of the process are avoided, and on the other hand, The running status of these instances can be monitored through the control and detection mechanism of the middle layer, and instances can be forked or recycled according to different conditions to achieve both flexibility and stability. When the webserver receives a request, it will not re-fork a process (because the process is started when the webserver starts, and will not exit), the webserver directly passes the content to the process (inter-process communication, but fastcgi uses other method, tcp communication), this process processes the request after receiving it, returns the result to the webserver, and finally waits for the next request instead of exiting. This installation mode of php is popular when using LNMP.
Taking php as an example, when installing ./configure, the compilation parameters cannot be added –with-apxs2=/usr/local/apache2/bin/apxs, plus the parameter option –enable-fastcgi –enable-fpm –enable-force -cgi-redirect.

Someone on the Internet has tested that the operating efficiency of the php_mod mode is about 50 times that of the traditional cgi mode, while the performance of fastcgi is basically equivalent to that of mod_php, but the php_mod mode cannot guarantee the security isolation of virtual host sites.

cgi和它的工具
CGI只是个技术标准(目前使用的是CGI/1.1),因为该标准非常简单所有它很容易用各种高级语言和脚本语言进行描述;但它也给CGI开发者带来了困难,那就是过去没有非常好的开发工具以开发方便开发者开发复杂的应用.很多时候它们不得不手工printf输入每一句html语句.这样严重限制了传统 cgi 的发展;
与此同时,另一批工具(暂时称之为泛CGI吧),如asp,jsp,php,它们继承了CGI标准的简单性,同时提供了相应的解释或编译工具.通过这些工具,开发人员只要在html/xml模板文件中嵌入相应(脚本)语句即可,特别是asp.NET开发环境,php之后的模板技术等,jsp等strust框架等都为开发人员提供了更为抽象化的开发工具,使开发人员彻底忘记了它们(asp,php,jsp等)的前身CGI(之所以称它们为泛CGI,因为它们都是CGI技术的继承和发展者).

最近安装了一台LAMP应用服务器,安装过程没有太大问题,但遇到了一个可以思考的问题,就是PHP的几种安装模式:cgi模式,模块模式和fastcgi模式。
在网上收集资料,看完后的个人理解:
1. cgi模式
早期传统的cgi模式其实是webserver进程fork出cgi子进程去处理动态网页的请求。具体来讲,webserver在收到用户请求后,都要fork出一个单独的cgi程序的进程来处理,cgi程序根据请求提交的参数作相应处理,然后输出标准的html语句返回给webserver,接着fork出来的cgi进程退出,webserver再返回给客户端。这样一来,webserver和具体的处理程序独立了开来,责任分明,cgi处理进程出现问题不会影响webserver的正常工作,但是在访问量高的情况下,webserver去fork进程会导致负载超标,资源消耗严重,影响服务器性能。

2. 动态库方式(模块方式)
cgi模式影响性能已经严重破坏了它的声誉,现在有了解决它的办法,就是模块设计,在webserver上用扩展模块的方式来替代cgi程序的功能,webserver可以内置perl解释器或php解释器。也就是说这些解释器做成模块的方式,就能插入到webserver自身的进程处理,webserver会在启动的时候就启动这些解释器。 当有新的动态请求进来时,webserver就是自己解析这些perl或php脚本,省得重新fork一个进程,效率提高了。但是这样的话,不同的webserver有不同的模块扩展的方式,例如:IIS的isapi,apache的dso(动态库)。对于不同的 webserver,要按照不同标准开发,无法做到webserver无关性,一旦出现问题将影响整个web server处理流程。
以php为例:在安装的时候./configure后面需要加 –with-apxs2=/usr/local/apache2/bin/apxs这个选项,意思是将php编译成apache的一个功能模块,官方解释:apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象,使之可以用由mod_so提供的LoadModule指令在运行时加载到Apache服务器中。安装完毕后,会在apache2/module目录下多出一个libphp5.so动态库,在http.conf中LoadModule加载这个库,至此webserver就可以用模块调用的方式去处理动态页面请求了。

3. fastcgi模式
我们还有另外的选择,这就是fastcgi。fastcgi是基于cgi架构的扩展,他的核心思想就是在webserver和具体cgi程序之间建立一个智能的可持续的中间层,统管cgi程序的运行,这样webserver只需要将请求提交给这个层,这个层再派生出几个可复用的cgi程序实例,然后再把请求分发给这些实例,这些实例是可控的,可持续,可复用的, 因此一方面避免了进程反复fork,另一方面又可以通过中间层的控制和探测机制来监视这些实例的运行情况,根据不同的状况fork或者回收实例,达到灵活性和稳定性兼得的目的。webserver收到一个请求时,他不会重新fork一个进程(因为这个进程在webserver启动时就开启了,而且不会退出),webserver直接把内容传递给这个进程(进程间通信,但fastcgi使用了别的方式,tcp方式通信),这个进程收到请求后进行处理,把结果返回给webserver,最后自己接着等待下一个请求的到来,而不是退出。当使用LNMP时,php的这种安装模式很受欢迎。
还是以php为例,在安装时./configure时,编译参数不能加–with-apxs2=/usr/local/apache2/bin/apxs,加上参数选项–enable-fastcgi –enable-fpm –enable-force-cgi-redirect。

网上有人测过,php_mod方式的运行效率是传统的cgi方式50倍左右,而fastcgi性能则基本和mod_php相当,但是php_mod模式无法保证虚拟主机站点的安全性隔离。

cgi and its tools
CGI is only a technical standard (currently using CGI/1.1), because the standard is so simple that it can be easily described in various high-level and scripting languages; but it also brings CGI developers The difficulty is that there were no very good development tools in the past to facilitate developers to develop complex applications. Many times they had to manually printf input each html statement. This severely limited the development of traditional cgi; at the
same time, another batch of Tools (for the time being called Pan-CGI), such as asp, jsp, php, inherit the simplicity of the CGI standard and provide corresponding interpretation or compilation tools. Through these tools, developers only need to add html/xml template files It is enough to embed the corresponding (script) statement in it, especially the asp.NET development environment, the template technology after php, the strust framework such as jsp, etc., all provide developers with more abstract development tools, so that developers completely forget They (asp, php, jsp, etc.) are the predecessors of CGI (the reason why they are called pan-CGI, because they are all inheritors and developers of CGI technology).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325724177&siteId=291194637