Table of Contents
CGI
FastCGI
FastCGI特点
FastCGI的工作原理
FastCGI的不足
PHP-CGI
PHP-FPM
Spawn-FCGI
PHP-FPM与spawn-CGI对比
Home php教程 php手册 概念了解:CGI,FastCGI,PHP-CGI与PHP-FPM

概念了解:CGI,FastCGI,PHP-CGI与PHP-FPM

Jun 13, 2016 am 09:38 AM
cgi php kernel

CGI

CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上。

CGI可以用任何一种语言编写,只要这种语言具有标准输入、输出和环境变量。如php,perl,tcl等。

FastCGI

FastCGI像是一个常驻(long-live)型的CGI,它可以一直执行着,只要激活后,不会每次都要花费时间去fork一次(这是CGI最为人诟病的fork-and-execute 模式)。它还支持分布式的运算,即 FastCGI 程序可以在网站服务器以外的主机上执行并且接受来自其它网站服务器来的请求。

FastCGI是语言无关的、可伸缩架构的CGI开放扩展,其主要行为是将CGI解释器进程保持在内存中并因此获得较高的性能。众所周知,CGI解释器的反复加载是CGI性能低下的主要原因,如果CGI解释器保持在内存中并接受FastCGI进程管理器调度,则可以提供良好的性能、伸缩性、Fail- Over特性等等。

FastCGI特点

  1. FastCGI具有语言无关性.
  2. FastCGI在进程中的应用程序,独立于核心web服务器运行,提供了一个比API更安全的环境。APIs把应用程序的代码与核心的web服务器链接在一起,这意味着在一个错误的API的应用程序可能会损坏其他应用程序或核心服务器。 恶意的API的应用程序代码甚至可以窃取另一个应用程序或核心服务器的密钥。
  3. FastCGI技术目前支持语言有:C/C++、Java、Perl、Tcl、Python、SmallTalk、Ruby等。相关模块在Apache, ISS, Lighttpd等流行的服务器上也是可用的。
  4. FastCGI的不依赖于任何Web服务器的内部架构,因此即使服务器技术的变化, FastCGI依然稳定不变。

FastCGI的工作原理

  1. Web Server启动时载入FastCGI进程管理器(IIS ISAPI或Apache Module)
  2. FastCGI进程管理器自身初始化,启动多个CGI解释器进程(可见多个php-cgi)并等待来自Web Server的连接。
  3. 当客户端请求到达Web Server时,FastCGI进程管理器选择并连接到一个CGI解释器。Web server将CGI环境变量和标准输入发送到FastCGI子进程php-cgi。
  4. FastCGI子进程完成处理后将标准输出和错误信息从同一连接返回Web Server。当FastCGI子进程关闭连接时,请求便告处理完成。FastCGI子进程接着等待并处理来自FastCGI进程管理器(运行在Web Server中)的下一个连接。 在CGI模式中,php-cgi在此便退出了。

在上述情况中,你可以想象CGI通常有多慢。每一个Web请求PHP都必须重新解析php.ini、重新载入全部扩展并重初始化全部数据结构。使用FastCGI,所有这些都只在进程启动时发生一次。一个额外的好处是,持续数据库连接(Persistent database connection)可以工作。

FastCGI的不足

因为是多进程,所以比CGI多线程消耗更多的服务器内存,PHP-CGI解释器每进程消耗7至25兆内存,将这个数字乘以50或100就是很大的内存数。

Nginx 0.8.46+PHP 5.2.14(FastCGI)服务器在3万并发连接下,开启的10个Nginx进程消耗150M内存(15M*10=150M),开启的64个php-cgi进程消耗1280M内存(20M*64=1280M),加上系统自身消耗的内存,总共消耗不到2GB内存。如果服务器内存较小,完全可以只开启25个php-cgi进程,这样php-cgi消耗的总内存数才500M。
上面的数据摘自Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器(第6版)

PHP-CGI

PHP-CGI是PHP自带的FastCGI管理器。

PHP-CGI的不足:

  1. php-cgi变更php.ini配置后需重启php-cgi才能让新的php-ini生效,不可以平滑重启。
  2. 直接杀死php-cgi进程,php就不能运行了。(PHP-FPM和Spawn-FCGI就没有这个问题,守护进程会平滑从新生成新的子进程。)

PHP-FPM

PHP-FPM是一个PHP FastCGI管理器,是只用于PHP的,可以在 http://php-fpm.org/download下载得到。

PHP-FPM其实是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中。必须将它patch到你的PHP源代码中,在编译安装PHP后才可以使用。

现在我们可以在最新的PHP 5.3.2的源码树里下载得到直接整合了PHP-FPM的分支,据说下个版本会融合进PHP的主分支去。相对Spawn-FCGI,PHP-FPM在CPU和内存方面的控制都更胜一筹,而且前者很容易崩溃,必须用crontab进行监控,而PHP-FPM则没有这种烦恼。

PHP5.3.3已经集成php-fpm了,不再是第三方的包了。PHP-FPM提供了更好的PHP进程管理方式,可以有效控制内存和进程、可以平滑重载PHP配置,比spawn-fcgi具有更多有点,所以被PHP官方收录了。在./configure的时候带 –enable-fpm参数即可开启PHP-FPM。

Spawn-FCGI

Spawn-FCGI是一个通用的FastCGI管理服务器,它是lighttpd中的一部份,很多人都用Lighttpd的Spawn-FCGI进行FastCGI模式下的管理工作,不过有不少缺点。而PHP-FPM的出现多少缓解了一些问题,但PHP-FPM有个缺点就是要重新编译,这对于一些已经运行的环境可能有不小的风险(refer),在php 5.3.3中可以直接使用PHP-FPM了。

Spawn-FCGI目前已经独成为一个项目,更加稳定一些,也给很多Web 站点的配置带来便利。已经有不少站点将它与nginx搭配来解决动态网页。

最新的lighttpd也没有包含这一块了(http://www.lighttpd.net/search?q=Spawn-FCGI),但可以在以前版本中找到它。在lighttpd-1.4.15版本中就包含了(http://www.lighttpd.net/download/lighttpd-1.4.15.tar.gz),目前Spawn-FCGI的下载地址是http://redmine.lighttpd.net/projects/spawn-fcgi,最新版本是http://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.gz。

注:最新的Spawn-FCGI可以到lighttpd.net网站搜索“Spawn-FCGI”找到它的最新版本发布地址。

PHP-FPM与spawn-CGI对比

PHP-FPM的使用非常方便,配置都是在PHP-FPM.ini的文件内,而启动、重启都可以从php/sbin/PHP-FPM中进行。更方便的是修改php.ini后可以直接使用PHP-FPM reload进行加载,无需杀掉进程就可以完成php.ini的修改加载
结果显示使用PHP-FPM可以使php有不小的性能提升。PHP-FPM控制的进程cpu回收的速度比较慢,内存分配的很均匀。
Spawn-FCGI控制的进程CPU下降的很快,而内存分配的比较不均匀。有很多进程似乎未分配到,而另外一些却占用很高。可能是由于进程任务分配的不均匀导致的。而这也导致了总体响应速度的下降。而PHP-FPM合理的分配,导致总体响应的提到以及任务的平均。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use PHP and CGI to implement user registration and login functions How to use PHP and CGI to implement user registration and login functions Jul 21, 2023 pm 02:31 PM

How to use PHP and CGI to implement user registration and login functions User registration and login are one of the necessary functions for many websites. In this article, we will introduce how to use PHP and CGI to achieve these two functions. We'll demonstrate the entire process with a code example. 1. Implementation of the user registration function The user registration function allows new users to create an account and save their information to the database. The following is a code example to implement the user registration function: Create a database table First, we need to create a database table to store user information. Can

PHP and CGI file upload and download technology: how to implement file management functions PHP and CGI file upload and download technology: how to implement file management functions Jul 21, 2023 am 11:19 AM

File upload and download technology with PHP and CGI: How to implement file management functions Introduction: File upload and download are one of the common functions in modern web applications. This article will introduce how to implement file upload and download functions using PHP and CGI programming languages, and show some code examples to demonstrate how to manage uploaded and downloaded files. Here’s what we’re going to cover: Basic concepts of file upload using PHP to implement file upload CGI to implement file upload Basic concepts of file download using PHP to implement file download CGI implementation under the file

How to use PHP and CGI to implement the video playback function of the website How to use PHP and CGI to implement the video playback function of the website Jul 22, 2023 pm 07:45 PM

How to use PHP and CGI to implement the video playback function of the website In today's multimedia era, video has become an indispensable part of the website content. In order to provide a good user experience, the website needs to implement video playback function. This article will introduce how to use PHP and CGI to implement the video playback function of the website, and provide code samples for reference. 1. Preparation Before starting, you need to ensure that the server has PHP and CGI modules installed. You can do this by running the phpinfo() function or typing "php

Explore the working principles, similarities and differences between PHP CLI and CGI Explore the working principles, similarities and differences between PHP CLI and CGI Mar 11, 2024 pm 12:39 PM

Working principles and similarities and differences In web development, PHP is a commonly used programming language that can interact with web servers in different ways, the most common of which are through PHPCLI (CommandLineInterface) and PHPCGI (CommonGatewayInterface). This article will explore the working principles, similarities and differences between PHPCLI and CGI, and provide specific code examples to illustrate the differences between them. 1. PHP

PHP and CGI technologies compared: How to choose the right one for your website PHP and CGI technologies compared: How to choose the right one for your website Jul 22, 2023 am 09:45 AM

Comparison of PHP and CGI technologies: How to choose the right website for you With the development of the Internet, CGI (Common Gateway Interface) and PHP (Hypertext Preprocessor) have become one of the most commonly used website development technologies. This article will compare these two technologies to help you choose the right development technology for your website. 1. Overview PHP is a very popular server-side scripting language that is widely used for dynamic website development. It is open source, supports multiple operating systems, and has powerful database connection and processing capabilities. Developers can use simple syntax

The relationship between cgi, fast-cgi and php-fpm (with flow chart) The relationship between cgi, fast-cgi and php-fpm (with flow chart) Oct 08, 2022 pm 02:07 PM

CGI is a protocol and has nothing to do with the process. For example, when the web server (nginx) receives a PHP network request, nginx needs to find the PHP parser according to the configuration file. After simple processing, some of the requested information is handed over to The PHP parser stipulates which protocols are to be transmitted and the format in which they are transmitted. This standard is called the cgi protocol.

Using machine learning to reconstruct faces in videos Using machine learning to reconstruct faces in videos Apr 08, 2023 pm 07:21 PM

Translator | Reviewed by Cui Hao | Sun Shujuan begins with a collaborative study between China and the UK that has devised a new way to recreate faces in video. This technology can enlarge and reduce facial structure with high consistency and no trace of artificial trimming. Typically, this transformation of facial structure is achieved through traditional CGI methods, which rely on detailed and expensive motion capping, rigging and texturing procedures to completely reconstruct the face. Unlike traditional methods, CGI in the new technology is integrated into the neural pipeline as a parameter for 3D facial information and serves as the basis for the machine learning workflow. The author points out: “Our goal is to refine facial contours based on natural faces in the real world.

How to use PHP and CGI to implement website paging and sorting functions How to use PHP and CGI to implement website paging and sorting functions Jul 21, 2023 pm 01:39 PM

How to use PHP and CGI to implement the paging and sorting functions of a website Preface Paging and sorting of a website are very common and important functions, especially for the display and processing of large amounts of data. In this article, we will use PHP and CGI (Common Gateway Interface) technology to implement the paging and sorting functions of the website, with corresponding code examples. 1. Implementation of paging function The paging function mainly obtains a specific range of data by querying the database, and displays the data according to the specified page number and display quantity per page. The following are the steps to implement paging functionality

See all articles