


What is the difference between cli and cgi running modes in php
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.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
