Table of Contents
Web服务器和动态语言如何交互--CGI&FastCGI&FPM浅谈,web服务器交互--cgi
Home php教程 php手册 Web服务器和动态语言如何交互--CGI&FastCGI&FPM浅谈,web服务器交互--cgi

Web服务器和动态语言如何交互--CGI&FastCGI&FPM浅谈,web服务器交互--cgi

Jun 13, 2016 am 09:14 AM
amp fastcgi fpm web interaction dynamic and how server language

Web服务器和动态语言如何交互--CGI&FastCGI&FPM浅谈,web服务器交互--cgi

一个用户的Request是如何经过Web服务器(Apache,Nginx,IIS,Light)与后端的动态语言(如PHP等)进行交互并将结果返回给用户的呢?

本文浅谈个人观点,可能有误,欢迎拍砖,共同学习。

 

一. 首先明确几个概念,以便后续说明

CGI:(Common Gateway Interface)Http服务器与后端程序(如PHP)进行交互的中间层。

工作原理及处理方式(fork-and-execute模式):

1.当Web Server有Request到达

2.fork一个CGI进程或线程(配置管理,环境初始化)

3.执行后台脚本

4.将结果返回Web服务器。

5.Web服务器将结果返回给用户。

FastCGI:常驻型(long-live)CGI形式,经过激活后,不会每次都要花时间去fork。

工作原理及处理方式:

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进程管理器分配的其他请求。

 

PHP-FPM:只用于PHP的PHP FastCGI 进程管理器。

PHP5.3.3以后的版本已经集成了PHP-FPM了。

php-fpm提供了更好的PHP配置管理方式,可以有效控制内存和进程、可以平滑重载php配置。

./configure php源码的时候,加—enable-fpm参数可开启PHP_FMP。

 

Spawn-FCGI:一个普通的FastCGI进程管理器。

 

二. PHP中的CGI实现:

PHP的CGI实现本质上是以Socket编程实现一个TCP或者UDP协议服务器。当启动时, 创建TCP/UDP协议的服务器监听,并接受相关请求进行处理。

CGI的生命周期为:模块初始化;SAPI初始化;请求的处理;模块关闭;SAPI关闭;

以TCP协议为例,在TCP的服务端,会执行如下操作:

1.服务端调用Socket函数创建一个TCP用的流式套接字;

2.服务端调用bind函数将服务器的本地地址与前面创建的套接字绑定;

3.服务调用listen函数将新创建的套接字作为监听,等待客户端发起连接,当客户端有多个连接连接到这个套接字时,可能需要排队处理;

4.服务器调用accept函数进入阻塞状态,直到有客户进程调用connect函数而建立起一个连接;

5.当与客户端创建连接后,服务器调用read_stream函数读取客户的请求;

6.处理完数据后,服务器调用write函数向客户端发送应答。

 

三.目前PHP的工作方式(以Apache服务器为例,因为Apache和Php是好兄弟嘛)

1.Apache Handler方式(php作为Apache服务器的Module)

一种改进的CGI方式,把PHP的解释模块编成so扩展,添加到Apache的modules中。

配置方式:

 <p>1.编译PHP时,加上如下参数:</p> <p>cd php-source</p> <p>./configure --prefix=/home/weiyanyan/local/php --with-apxs2=/home/weiyanyan/local/apache/bin/apxs --with-mysql</p> <p>说明:—with-apxs2为apache中apxs相应目录,将在apache根目录下的modules下生成libphp5.so</p> <p>2.在apache的配置文件http.conf中增加  <p>LoadModule php5_module modules/libphp5.so  <p>然后在<IfModule mime_module>节点下增加如下mime配置  <p>AddType application/x-httpd-php .php<br> </p>
Copy after login

2.CGI模式

前提为不能以模块模式运行。(注释掉:LoadModule php5_module modules/libphp5.so)

在httpd.conf增加action:

Action application/x-httpd-php /cgi-bin/php-cgi

如果在/cgi目录找不到php-cgi,可从php的bin里面cp一个。

【可以写一个PHP脚本,让其sleep(20);运行之前看机器进程中无php-cgi进程,请求的时候,会有相应的进程产生。经测试一个php-cgi进程可以承载多个请求,具体未深究,因为这种方式已经基本没有人用了。】

3.FastCGI模式

FastCGI模式根据进程管理器的不同可以分为:Apache内置进程管理器,php-fpm进程管理器

Apache内置进程管理器:

 <p>mod_fastcgi的安装  <p>#wget http://www.fastcgi.com/dist/mod_fastcgi-2.4.6.tar.gz  <p># tar -zxvf mod_fastcgi-2.4.6.tar.gz  <p># cd mod_fastcgi-2.4.6  <p># cp Makefile.AP2 Makefile  <p># vim Makefile 将Makefile中的路径改成你的apache的安装路径  <p># make install 安装成功  <p>安装成功后,会自动把mod_fastcgi.so复制到/usr/local/apache/modules目录  </p>
Copy after login

 

首先要添加fastcgi模块到httpd.conf配置文件:

LoadModule fastcgi_module modules/mod_fastcgi.so

这种模式注释不注释LoadModule php5_module modules/libphp5.so这行貌似没什么关系,只要配置了以下模块


   FastCgiServer /home/weiyanyan/local/apache/cgi-bin/php-cgi -processes 20
   AddType application/x-httpd-php .php

   AddHandler php-fastcgi .php

   Action php-fastcgi /cgi-bin/php-cgi

就会自动走到fastcgi模式。

然后重启apache,这个时候用 ps aux|grep php就会发现有很多php-cgi进程在运行。说明配置生效.

 

FPM方式

首先要添加fastcgi模块到httpd.conf配置文件:

LoadModule fastcgi_module modules/mod_fastcgi.so

这种模式注释不注释LoadModule php5_module modules/libphp5.so这行貌似没什么关系,只要配置了以下模块


FastCgiExternalServer /home/weiyanyan/local/apache/cgi-bin/php-cgi -host 127.0.0.1:9000

AddType application/x-httpd-php .php

AddHandler php-fastcgi .php

Action php-fastcgi /cgi-bin/php-cgi

其中在本机9000端口开启了PHP-Fpm服务

FPM的安装简单介绍如下:

cd php-source

./configure --prefix=/home/weiyanyan/local/php --with-apxs2=/home/weiyanyan/local/apache/bin/apxs --with-mysql --enable-fpm

此时在Php的根目录sbin下会有php-fpm运行程序,其配置文件在php根目录下面的/etc/php-fpm.conf

修改完配置,在apache配置对应的端口启动php-fpm即可。

 

[只是写完,未检查,回家过年…]

 

参考:

http://www.phppan.com/2011/05/php-cgi/

http://www.cnblogs.com/fangbo/archive/2011/12/02/2272400.html

http://blog.zyan.cc/nginx_php_v6/

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)

Convert VirtualBox fixed disk to dynamic disk and vice versa Convert VirtualBox fixed disk to dynamic disk and vice versa Mar 25, 2024 am 09:36 AM

When creating a virtual machine, you will be asked to select a disk type, you can select fixed disk or dynamic disk. What if you choose fixed disks and later realize you need dynamic disks, or vice versa? Good! You can convert one to the other. In this post, we will see how to convert VirtualBox fixed disk to dynamic disk and vice versa. A dynamic disk is a virtual hard disk that initially has a small size and grows in size as you store data in the virtual machine. Dynamic disks are very efficient at saving storage space because they only take up as much host storage space as needed. However, as disk capacity expands, your computer's performance may be slightly affected. Fixed disks and dynamic disks are commonly used in virtual machines

How to configure Dnsmasq as a DHCP relay server How to configure Dnsmasq as a DHCP relay server Mar 21, 2024 am 08:50 AM

The role of a DHCP relay is to forward received DHCP packets to another DHCP server on the network, even if the two servers are on different subnets. By using a DHCP relay, you can deploy a centralized DHCP server in the network center and use it to dynamically assign IP addresses to all network subnets/VLANs. Dnsmasq is a commonly used DNS and DHCP protocol server that can be configured as a DHCP relay server to help manage dynamic host configurations in the network. In this article, we will show you how to configure dnsmasq as a DHCP relay server. Content Topics: Network Topology Configuring Static IP Addresses on a DHCP Relay D on a Centralized DHCP Server

Best Practice Guide for Building IP Proxy Servers with PHP Best Practice Guide for Building IP Proxy Servers with PHP Mar 11, 2024 am 08:36 AM

In network data transmission, IP proxy servers play an important role, helping users hide their real IP addresses, protect privacy, and improve access speeds. In this article, we will introduce the best practice guide on how to build an IP proxy server with PHP and provide specific code examples. What is an IP proxy server? An IP proxy server is an intermediate server located between the user and the target server. It acts as a transfer station between the user and the target server, forwarding the user's requests and responses. By using an IP proxy server

What should I do if I can't enter the game when the epic server is offline? Solution to why Epic cannot enter the game offline What should I do if I can't enter the game when the epic server is offline? Solution to why Epic cannot enter the game offline Mar 13, 2024 pm 04:40 PM

What should I do if I can’t enter the game when the epic server is offline? This problem must have been encountered by many friends. When this prompt appears, the genuine game cannot be started. This problem is usually caused by interference from the network and security software. So how should it be solved? The editor of this issue will explain I would like to share the solution with you, I hope today’s software tutorial can help you solve the problem. What to do if the epic server cannot enter the game when it is offline: 1. It may be interfered by security software. Close the game platform and security software and then restart. 2. The second is that the network fluctuates too much. Try restarting the router to see if it works. If the conditions are OK, you can try to use the 5g mobile network to operate. 3. Then there may be more

How to install PHP FFmpeg extension on server? How to install PHP FFmpeg extension on server? Mar 28, 2024 pm 02:39 PM

How to install PHPFFmpeg extension on server? Installing the PHPFFmpeg extension on the server can help us process audio and video files in PHP projects and implement functions such as encoding, decoding, editing, and processing of audio and video files. This article will introduce how to install the PHPFFmpeg extension on the server, as well as specific code examples. First, we need to ensure that PHP and FFmpeg are installed on the server. If FFmpeg is not installed, you can follow the steps below to install FFmpe

Exploring the boundaries of agents: AgentQuest, a modular benchmark framework for comprehensively measuring and improving the performance of large language model agents Exploring the boundaries of agents: AgentQuest, a modular benchmark framework for comprehensively measuring and improving the performance of large language model agents Apr 11, 2024 pm 08:52 PM

Based on the continuous optimization of large models, LLM agents - these powerful algorithmic entities have shown the potential to solve complex multi-step reasoning tasks. From natural language processing to deep learning, LLM agents are gradually becoming the focus of research and industry. They can not only understand and generate human language, but also formulate strategies, perform tasks in diverse environments, and even use API calls and coding to Build solutions. In this context, the introduction of the AgentQuest framework is a milestone. It not only provides a modular benchmarking platform for the evaluation and advancement of LLM agents, but also provides researchers with a Powerful tools to track and improve the performance of these agents at a more granular level

How to change the language display of vivox60pro vivox60pro system language setting method How to change the language display of vivox60pro vivox60pro system language setting method Mar 23, 2024 am 09:06 AM

1. Click [System Management] in the phone settings menu. 2. Click the [Language] option. 3. Select the system language you want to use.

Is PHP front-end or back-end in web development? Is PHP front-end or back-end in web development? Mar 24, 2024 pm 02:18 PM

PHP belongs to the backend in web development. PHP is a server-side scripting language, mainly used to process server-side logic and generate dynamic web content. Compared with front-end technology, PHP is more used for back-end operations such as interacting with databases, processing user requests, and generating page content. Next, specific code examples will be used to illustrate the application of PHP in back-end development. First, let's look at a simple PHP code example for connecting to a database and querying data:

See all articles