php cli 和 php cgi 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9315714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:32:36  来源:igfitidea点击:

What is difference between php cli and php cgi?

phpcgi

提问by user1208865

A very basic question for you. I just found two executable files php-cgi.exeand php.exein the bin folder of the WAMP server on my laptop. I am learning PHP and could not figure out the difference. What is difference between them?

一个非常基本的问题。我只是发现了两个可执行文件php-cgi.exe,并php.exe在我的笔记本电脑WAMP服务器的bin文件夹。我正在学习 PHP,但无法弄清楚其中的区别。它们之间有什么区别?

采纳答案by Brian Driscoll

PHP CLI is the command-line interface for PHP (e.g. for creating standalone applications)
PHP CGI is the common gateway interface for PHP (e.g. for web applications)

PHP CLI 是 PHP 的命令行界面(例如用于创建独立应用程序)
PHP CGI 是 PHP 的通用网关接口(例如用于 Web 应用程序)

回答by Abdala Cerqueira

From http://www.php-cli.com/php-cli-cgi.shtml

来自http://www.php-cli.com/php-cli-cgi.shtml

These are the most important differences between CLI and CGI:

这些是 CLI 和 CGI​​ 之间最重要的区别:

  • Unlike the CGI SAPI, CLI writes no headers to the output by default
  • There are some php.ini directives which are overridden by the CLI SAPI because they do not make sense in shell environments:
    • html_errors: CLI default is FALSE
    • implicit_flush: CLI default is TRUE
    • max_execution_time: CLI default is 0 (unlimited)
    • register_argc_argv: CLI default is TRUE
  • You can have command line arguments with your script! Variable "$argc" provides you with a number of arguments passed to the application. And array "$argv" gives you an array of the actual arguments
  • There are 3 new constant defined for the shell environment: STDIN, STDOUT, STDERR. All of them are file handlers for correspondent shell devices. For example STDIN is the handler for fopen('php://stdin', 'r') . So, you can read a line from STDIN like this: $strLine = trim(fgets(STDIN));. STDIN is already defined for you by PHP CLI!
  • PHP CLI does not change the current directory to the directory of the executed script. The current directory for the script would be the directory where your type PHP CLI command.
  • There are number of USEFUL options available for PHP CLI. Which will allow you to get some valuable information about you php setup, your php script or run it in different modes.
  • In PHP 5 there were some changes in CLI and CGI filenames. In PHP 5, the CGI version was renamed to php-cgi.exe (previously php.exe) and the CLI version now sits in the main directory (previously cli/php.exe).
  • In PHP 5 it was also introduced a new mode: php-win.exe. This is equal to the CLI version, except that php-win doesn't output anything and thus provides no console (no "dos box" appears on the screen). This behaviour is similar to PHP GTK.
  • 与 CGI SAPI 不同,CLI 默认不向输出写入头文件
  • 有一些 php.ini 指令被 CLI SAPI 覆盖,因为它们在 shell 环境中没有意义:
    • html_errors: CLI 默认为 FALSE
    • hidden_​​flush: CLI 默认为 TRUE
    • max_execution_time:CLI 默认为 0(无限制)
    • register_argc_argv:CLI 默认为 TRUE
  • 您可以在脚本中使用命令行参数!变量“$argc”为您提供了许多传递给应用程序的参数。数组 "$argv" 给你一个实际参数的数组
  • 为 shell 环境定义了 3 个新常量:STDIN、STDOUT、STDERR。它们都是对应的 shell 设备的文件处理程序。例如 STDIN 是 fopen('php://stdin', 'r') 的处理程序。因此,您可以像这样从 STDIN 读取一行:$strLine = trim(fgets(STDIN));。PHP CLI 已经为您定义了 STDIN!
  • PHP CLI 不会将当前目录更改为执行脚本的目录。脚本的当前目录将是您键入 PHP CLI 命令的目录。
  • PHP CLI 有许多有用的选项。这将允许您获得一些关于您的 php 设置、您的 php 脚本或以不同模式运行它的有价值的信息。
  • 在 PHP 5 中,CLI 和 CGI​​ 文件名有一些变化。在 PHP 5 中,CGI 版本被重命名为 php-cgi.exe(以前是 php.exe),CLI 版本现在位于主目录(以前是 cli/php.exe)中。
  • 在 PHP 5 中还引入了一种新模式:php-win.exe。这与 CLI 版本相同,只是 php-win 不输出任何内容,因此不提供控制台(屏幕上不显示“dos 框”)。这种行为类似于 PHP GTK。

回答by Mahmoud Zalt

This might give you a broader understanding of their difference:

这可能会让您更广泛地了解它们的区别:

CGI:(common gateway interface) It is a specification "protocol" for transferring information between a Web server and a CGI program.

CGI:(通用网关接口)它是一种规范“协议”,用于在Web 服务器和CGI 程序之间传输信息。

A CGI program is any program designed to accept and return data that conforms to the CGI specification.

CGI 程序是设计用于接受和返回符合 CGI 规范的数据的任何程序。

Basically it's a way to run a server side script (PHP, Perl, Python,...) when a HTTP request comes.

基本上,这是一种在 HTTP 请求到来时运行服务器端脚本(PHP、Perl、Python 等)的方法。

CGI is very slow in comparison to other alternatives.

与其他替代品相比,CGI 非常慢。



FastCGI:is a better CGI.

FastCGI:是一个更好的CGI。

Fast CGI is a different approach with much faster results.

快速 CGI 是一种不同的方法,具有更快的结果。

It is a CGI with only a few extensions.

它是一个只有几个扩展的 CGI。

FastCGI implementation isn't available anymore, in favor of the PHP-FPM.

FastCGI 实现不再可用,而是支持 PHP-FPM。



PHP-FPM:(FastCGI Process Manager), it's a better FastCGI implementation than the old FastCGI.

PHP-FPM:(FastCGI 进程管理器),它是比旧的 FastCGI 更好的 FastCGI 实现。

It runs as a standalone FastCGI server.

它作为独立的 FastCGI 服务器运行。

In general it's a PHP interface for the web servers (Apache, Nginx..) to allows Web Server to interact with PHP.

通常,它是 Web 服务器(Apache、Nginx ..)的 PHP 接口,允许 Web 服务器与 PHP 交互。

Unlike the PHP-CLIwhich is a command line interface for PHP to allows Users to interact with PHP via terminal.

PHP-CLI不同,PHP-CLIPHP的命令行界面,允许用户通过终端与 PHP 交互。



mod_php:an Apache module to run PHP.

mod_php:运行 PHP 的 Apache 模块。

It execute PHP scripts inside the Web Server directly as part of the web server without communicating with a CGI program.

它作为 Web 服务器的一部分直接在 Web 服务器内执行 PHP 脚本,而无需与 CGI 程序通信。



mod_SuPHP:is similar to mod_php but can change the user/group that the process runs under.

mod_SuPHP:与 mod_php 类似,但可以更改进程运行所在的用户/组。

Basically it address some problems of mod_php related to permissions.

基本上它解决了一些与权限相关的 mod_php 问题。

回答by Niet the Dark Absol

php-cgiis intended for a webserver. Among other things it handles HTTP headers for you.

php-cgi用于网络服务器。除其他外,它还为您处理 HTTP 标头。

The CLI version is intended to run on a command line (hence "Command Line Interface"). This one does not handle headers, or any other server-related things.

CLI 版本旨在在命令行上运行(因此称为“命令行界面”)。这个不处理标题,或任何其他与服务器相关的事情。

回答by Powerlord

php-cliis meant for running PHP on the command line. php-cgidoes additional things for you, such as HTTP headers and certain security modifications.

php-cli用于在命令行上运行 PHP。 php-cgi为您做其他事情,例如 HTTP 标头和某些安全修改。

Having said that, consider installing a FastCGI module and using PHP's FastCGI interface. This should run PHP noticably faster than php-cgi. I believe the standard Apache FastCGI module is mod_fcgid.

话虽如此,请考虑安装一个 FastCGI 模块并使用PHP 的 FastCGI 接口。这应该比 php-cgi 运行 PHP 快得多。我相信标准的 Apache FastCGI 模块是mod_fcgid.

回答by Kavi Siegel

CLI is for command line scripts, CGI is for web requests

CLI 用于命令行脚本,CGI 用于 Web 请求