Home  >  Article  >  Backend Development  >  What is the difference between cli and cgi running modes in php

What is the difference between cli and cgi running modes in php

青灯夜游
青灯夜游Original
2023-01-30 16:25:394141browse

Difference: CLI refers to the command line running mode, that is, typing commands in the console or shell to execute PHP script code; while CGI is the running mode of the public gateway interface, which is when Apache encounters a PHP script The PHP program will be submitted to the CGI application (php-cgi.exe) for interpretation, and the result after interpretation will be returned to Apache, and then returned to the corresponding requesting user.

What is the difference between cli and cgi running modes in php

The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer

Can PHP be successfully run on the Apache server? Let’s see how we configure how PHP runs. There are three main ways to run PHP so far:

mod_php, run by module loading, which may not be easy for beginners to understand, actually integrates PHP into the Apache server and runs it in the same process run.

CGI, run in CGI mode. CGI is called the public gateway interface in English. When Apache encounters a PHP script, it will submit the PHP program to the CGI application (php -cgi.exe), the interpreted results are returned to Apache, and then returned to the corresponding requesting user.

FastCGI, runs in FastCGI mode. This form is an enhanced version of CGI. CGI is a single-process, multi-threaded running method. The program will be destroyed after execution, so the configuration and environment variables need to be loaded every time fork-and-execute (create-execute). FastCGI is different. FastCGI is like a long-live CGI. It can be executed all the time. As long as it is activated, it will not take time to fork every time. The FastCGI process manager initializes itself, starts multiple CGI interpreter processes (multiple php-cgi.exe visible in the task manager) and waits for connections from the Web Server.

1. What is the CLI running mode of php?

Command line

CLI: It is the command line. For example, you can type the command in the console or shell:

php -f index.php

and then get the output

Command-line interface (English: command-line interface, abbreviation: CLI) is the graphical user interfaceThe most widely used user interface before its popularity, it usually does not support mouse. The user inputs instructions through the keyboard, and the computer executes the instructions after receiving them. Some people also call it Character User Interface (CUI). It is generally believed that the command line interface (CLI) is not as convenient for users to operate as the graphical user interface (GUI). Because command line interface software usually requires the user to memorize the operating commands, however, due to its own characteristics, the command line interface saves computer system resources compared to the graphical user interface. Under the premise of memorizing the commands, using the command line interface is often faster than using the graphical user interface. Therefore, operating systems with graphical user interfaces retain optional command line interfaces.

2. What is the CGI operating mode in php?

Public Gateway Interface

Run in CGI mode. CGI is called Public Gateway Interface in English. Apache will submit the PHP program to CGI when it encounters a PHP script. The application (php-cgi.exe) interprets, and the interpreted results are returned to Apache, and then returned to the corresponding requesting user.

CGI 是Web 服务器运行时外部程序的规范,按CGI 编写的程序可以扩展服务器功能。CGI 应用程序能与浏览器进行交互,还可通过数据库API 与数据库服务器等外部数据源进行通信,从数据库服务器中获取数据。格式化为HTML文档后,发送给浏览器,也可以将从浏览器获得的数据放到数据库中。几乎所有服务器都支持CGI,可用任何语言编写CGI,包括流行的C、C ++、VB 和Delphi 等。CGI 分为标准CGI 和间接CGI两种。标准CGI 使用命令行参数或环境变量表示服务器的详细请求,服务器与浏览器通信采用标准输入输出方式。间接CGI 又称缓冲CGI,在CGI 程序和CGI 接口之间插入一个缓冲程序,缓冲程序与CGI 接口间用标准输入输出进行通信。

    公共网关接口”(Common Gateway Interface),HTTP服务器 与你的或其它机器上的程序 进行 “交谈”的一种工具 ,其程序 须运行在网络 服务器 上。在服务器 环境中,为“程序 ”提供标准 的接口,通过这个接口,“程序 ”可以对服务器 与客户端 交换的信息 做一些事情 。“程序 ”的语 言并没有要求。程序 对接口进行 操作。服务器 要支持 CGI就要提供CGI中要求的环境变量 ,或者还有别的。

个人理解:CGI规定了php与web server交流的规则,相当于执行了response = exec("php -f index.php -url=xxx -cookie=xxx -xxx=xxx")。

3、php的运行模式FastCGI是什么?

以FastCGI的方式运行。这种形式是CGI的加强版本,CGI是单进程,多线程的运行方式,程序执行完成之后就会销毁,所以每次都需要加载配置和环境变量fork-and-execute(创建-执行)。而FastCGI则不同,FastCGI 像是一个常驻 (long-live) 型的 CGI,它可以一直执行着,只要激活后,不会每次都要花费时间去 fork 一次。FastCGI进程管理器自身初始化,启动多个CGI解释器进程 (在任务管理器中可见多个php-cgi.exe)并等待来自Web Server的连接。

4、php的运行模式mod_php是什么?

模块加载

以模块加载的方式运行,初学者可能不容易理解,其实就是将PHP集成到Apache服务器,以同一个进程运行。

5、php的cgi运行模式如何配置 ?

cgi的方式运行,需要做如下的配置php配置文件

cgi.force_redirect = 0 //本来是 1 并且去掉注释符号;

修改apache的配置,去掉原来的模块配置

AddType application/x-httpd-php .php
LoadModule php5_module "C:/php5/php5apache2_2.dll"
PHPinidir "C:/php5/php.ini"

6、cgi是什么?

最早的Web服务器简单地响应浏览器发来的HTTP请求,并将存储在服务器上的HTML文件返回给浏览器,也就是静态html。事物总是不 断发展,网站也越来越复杂,所以出现动态技术。但是服务器并不能直接运行 php,asp这样的文件,自己不能做,外包给别人吧,但是要与第三做个约定,我给你什么,然后你给我什么,就是握把请求参数发送给你,然后我接收你的处 理结果给客户端。那这个约定就是 common gateway interface,简称cgi。这个协议可以用vb,c,php,python 来实现。cgi只是接口协议,根本不是什么语言。下面图可以看到流程  

推荐学习:《PHP视频教程

The above is the detailed content of What is the difference between cli and cgi running modes in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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