如何确定是否使用 FastCGI 来运行 php 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/609044/
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-24 23:15:37  来源:igfitidea点击:

How to know for sure if FastCGI is being used to run php scripts

phpiisiis-7fastcgi

提问by Jimmy

I have a hosted site and I'm having trouble configuring Joomla (running Joomla + php + mySQL on IIS7 + win server 2008). I have a similar configuration running on a local machine (Joomla + php + mySQL on IIS7 + vista x64), so I was at least able to follow instructions showed in various tutorials on how to set this up.

我有一个托管站点,但在配置 Joomla 时遇到问题(在 IIS7 + win server 2008 上运行 Joomla + php + mySQL)。我有一个类似的配置在本地机器上运行(Joomla + php + mySQL on IIS7 + vista x64),所以我至少能够按照各种教程中显示的说明进行设置。

This symptom with the hosted site is that I can't turn on any SEO settings in Joomla (not even the first setting, "Search Engine Friendly URLs"). I get either 404 (file not found) or the URL appears correctly rewritten but it's always the home page's content that is displayed. I had a similar issue on my home machine and it turns out to have been because I wasn't using FastCGI to host php, so I decided to investigate that on the hosted site.

托管站点的这个症状是我无法在 Joomla 中打开任何 SEO 设置(甚至不是第一个设置,“搜索引擎友好 URL”)。我得到 404(未找到文件)或 URL 显示正确重写,但始终显示主页的内容。我在我的家用机器上遇到了类似的问题,结果是因为我没有使用 FastCGI 来托管 php,所以我决定在托管站点上进行调查。

Anyway, I noticed in the web.config file of the directory hosting joomla on the hosted site the following line:

无论如何,我在托管站点上托管 joomla 的目录的 web.config 文件中注意到以下行:

<add name="Plesk_Handler_3522909676" path="*.php" verb="*" modules="IsapiModule" scriptProcessor="c:\program files (x86)\parallels\plesk\additional\pleskphp5\php5isapi.dll" resourceType="Either" />

From past experience, I know that php has some issues when not running under fastCGI. I noticed the web.config in the root folder used the following line instead:

根据过去的经验,我知道 php 在不在 fastCGI 下运行时会出现一些问题。我注意到根文件夹中的 web.config 使用了以下行:

<add name="Plesk_Handler_0286090609" path="*.php" verb="*" modules="CgiModule" scriptProcessor="c:\program files (x86)\parallels\plesk\additional\pleskphp5\php-cgi.exe" resourceType="Either" /> 

I copied that in the web.config in the joomla directory, and got different behavior... but still not working. If I load a .php file in the joomla directory that runs phpInfo(), under Server API it says CGI/FastCGI . Is that positive confirmationthat FastCGI is being used? Why does the handler in the web config point to modules="CgiModule" instead of modules="FastCgiModule" (I'm not even sure that exists, but I just find the mention of CgiModule suspicious).

我将其复制到 joomla 目录中的 web.config 中,并得到了不同的行为......但仍然无法正常工作。如果我在运行 phpInfo() 的 joomla 目录中加载一个 .php 文件,它会在 Server API 下显示 CGI/FastCGI 。这是使用 FastCGI 的肯定确认吗?为什么 web 配置中的处理程序指向 modules="CgiModule" 而不是 modules="FastCgiModule"(我什至不确定是否存在,但我只是发现 CgiModule 的提及可疑)。

It's a hosted site, so as far as I know I don't have access to ApplicationHost.config file...

这是一个托管站点,据我所知,我无权访问 ApplicationHost.config 文件...

回答by Jimmy

Here's a simple test:

这是一个简单的测试:

  1. Create a phpinfo.php file with
  1. 创建一个 phpinfo.php 文件

 <?php phpinfo(); ?> 

inside;

里面;

  1. Request http://yoursite.com/phpinfo.php/foobar?foo=bar

  2. Check the output of phpinfo and look for _SERVER["REQUEST_URI"].

  1. 请求http://yoursite.com/phpinfo.php/foobar?foo=bar

  2. 检查 phpinfo 的输出并查找 _SERVER["REQUEST_URI"]。

If this variable is missing, then CGI is used. If the variable is present and correctly set to /phpinfo.php/foobar?foo=bar, then either ISAPI or FastCGI is used. Look near the top of the output for Server API; it should be set to either ISAPI (which means ISAPI is being used) or CGI/FastCGI (which means FastCGI is being used, since we already ruled out CGI).

如果缺少此变量,则使用 CGI。如果该变量存在并正确设置为 /phpinfo.php/foobar?foo=bar,则使用 ISAPI 或 FastCGI。查看服务器 API 输出的顶部附近;它应该设置为 ISAPI(这意味着正在使用 ISAPI)或 CGI/FastCGI(这意味着正在使用 FastCGI,因为我们已经排除了 CGI)。

回答by damarsan

You can use (on centos) apachectl -M, you'll show what modules are enabled:

您可以使用(在 centos 上)apachectl -M,您将显示启用了哪些模块:

apachectl -M:

apachectl -M:

file_cache_module (shared) mem_cache_module (shared) version_module (shared) fastcgi_module (shared)

file_cache_module (shared) mem_cache_module (shared) version_module (shared) fastcgi_module (shared)

回答by Gregor Macgregor

php_sapi_name() or PHP_SAPI, both work.

php_sapi_name() 或 PHP_SAPI,两者都有效。

http://php.net/manual/en/function.php-sapi-name.php

http://php.net/manual/en/function.php-sapi-name.php

<?php
    if (php_sapi_name() != 'cgi-fcgi'){
        echo 'I knew fastcgi wasn\'t working.';
    }else{
        echo 'Holy crap, something worked for once.';
    }

回答by Asfand Qazi

Unfortunately, the checking \_SERVER["REQUEST_URI"]didn't work for me. Using CGIor FastCGI, it always displayed /phpinfo.php/foobar?foo=bar.

不幸的是,检查\_SERVER["REQUEST_URI"]对我不起作用。使用CGIFastCGI,它总是显示/phpinfo.php/foobar?foo=bar

Neither did seeing if Server API = CGI/FastCGIwas set - that only tells you what interfaces the compiled version of php supports, not what is being used. However, I found another method that may work more reliably.

也没有查看是否Server API = CGI/FastCGI设置 - 这只告诉您编译版本的 php 支持哪些接口,而不是正在使用的接口。但是,我发现了另一种可能更可靠的方法。

Plonk a file in place called phpinfo.phpcontaining the text: <? php phpinfo(); ?>

Plonk 一个名为phpinfo.php的文件,其中包含文本:<? php phpinfo(); ?>

Check for the variable \_ENV["REDIRECT_HANDLER"]:
If it is set to php5-fastcgi(or something else fastcgi-ish) the request most probably went through FastCGI. If it is set to application/x-httpd-php(or I assume something other than the above), you are using CGI.

检查变量\_ENV["REDIRECT_HANDLER"]
如果它设置为php5-fastcgi(或其他 fastcgi-ish)请求很可能通过FastCGI。如果它设置为application/x-httpd-php(或者我假设除上述内容之外的其他内容),则您正在使用CGI

However, a surefire way is by running a little test. You need the ab(Apache Bench) tool for this.
Both with and without CGI, run this from another machine:

然而,一个万无一失的方法是运行一个小测试。ab为此,您需要(Apache Bench) 工具。
无论有没有 CGI,从另一台机器上运行:

ab -c 100 -n 1000 http://yourdomain.com/path/phpinfo.php

Check the line Time taken for tests:. On my box at least, accessing my VPS over a 1.3Mbps ADSL connection, FastCGI completely maxed it out, while with CGI was only able to fill half of it.

检查线路Time taken for tests:。至少在我的机器上,通过 1.3Mbps 的 ADSL 连接访问我的 VPS,FastCGI 完全把它最大化,而 CGI 只能填满它的一半。

Hope this helps.

希望这可以帮助。

回答by cameron

Make sure things are set up initially to where the script fails completely when fastcgi isn't running. Then you'll know, when it works, that fastcgi daemon is the reason.

当 fastcgi 没有运行时,确保最初设置脚本完全失败的地方。然后你就会知道,当它工作时,fastcgi 守护进程就是原因。

回答by tobius

This worked for me.

这对我有用。

/**
 * return phpinfo() results as an array
 *
 * @credit http://php.net/manual/en/function.phpinfo.php#106862
 * @param void
 * @return array
 */
function phpinfo_array(){
    ob_start();
    phpinfo();
    $info_arr = array();
    $info_lines = explode("\n", strip_tags(ob_get_clean(), '<tr><td><h2>'));
    $cat = 'general';
    foreach($info_lines as $line){
        preg_match('/<h2>(.*)<\/h2>/', $line, $title) ? $cat = preg_replace('/\s+/', '_', strtolower(trim($title[1]))) : null;
        if(preg_match('/<tr><td[^>]+>([^<]*)<\/td><td[^>]+>([^<]*)<\/td><\/tr>/', $line, $val)){
            $subcat = preg_replace('/\s+/', '_', strtolower(trim($val[1])));
            $info_arr[$cat][$subcat] = $val[2];
        } elseif(preg_match('/<tr><td[^>]+>([^<]*)<\/td><td[^>]+>([^<]*)<\/td><td[^>]+>([^<]*)<\/td><\/tr>/', $line, $val)){
            $subcat = preg_replace('/\s+/', '_', strtolower(trim($val[1])));
            $info_arr[$cat][$subcat] = array('local' => $val[2], 'master' => $val[3]);
        }
    }
    return $info_arr;
}


// output proper response code
$phpinfo = phpinfo_array();
$configure = (@isset($phpinfo['general']['configure_command'])) ?: null;

// properly account for FastCGI
if ($configure && preg_match('/--enable-fastcgi/', $configure)){
    // fastcgi response
    header('Status: 403 Access is forbidden');
} else {
    // http response
    header('HTTP/1.0 403 Access is forbidden');
}

回答by Hawk Kroeger

You should see a referenced to it from

你应该看到一个对它的引用

<?php
phpinfo();
?>

Server API = CGI/FastCGI

服务器 API = CGI/FastCGI

回答by Hawk Kroeger

Joomla creates a .htaccess file with rewrite rules in it to enable search engine friendly URLs. If you're using Apache you will need to have to set "AllowOverride FileInfo" for the directory containing your joomla installation. If you are using IIS you will need a module such as IISModRewrite. Here are instructions for this: [http://www.micronovae.com/ModRewrite/articles/SEFJoomla.html]

Joomla 创建一个 .htaccess 文件,其中包含重写规则以启用搜索引擎友好的 URL。如果您使用 Apache,您将需要为包含 joomla 安装的目录设置“AllowOverride FileInfo”。如果您使用 IIS,您将需要一个模块,例如 IISModRewrite。以下是相关说明:[ http://www.micronovae.com/ModRewrite/articles/SEFJoomla.html]