php 什么是 mod_php?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2712825/
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
What is mod_php?
提问by SpikETidE
While going through a Zend tutorial, I came across the following statement:
在阅读Zend 教程时,我遇到了以下语句:
Note that the php_flag settings in .htaccess only work if you are using mod_php.
请注意,.htaccess 中的 php_flag 设置仅在您使用 mod_php 时才有效。
Can someone explain what that means?
有人能解释一下这是什么意思吗?
回答by Pascal MARTIN
mod_phpmeans PHP, as an Apache module.
mod_php表示 PHP,作为Apache 模块。
Basically, when loading mod_phpas an Apache module, it allows Apache to interpret PHP files (those are interpreted by mod_php).
基本上,当mod_php作为 Apache 模块加载时,它允许 Apache 解释 PHP 文件(那些由 解释mod_php)。
EDIT :There are (at least)two ways of running PHP, when working with Apache :
编辑:使用 Apache 时,有(至少)两种运行 PHP 的方式:
- Using CGI: a PHP process is launched by Apache, and it is that PHP process that interprets PHP code -- not Apache itself
- Using PHP as an Apache module(called
mod_php): the PHP interpreter is then kind of "embedded" inside the Apache process : there is no external PHP process -- which means that Apache and PHP can communicatebetter.
- 使用CGI:一个 PHP 进程由 Apache 启动,它是解释 PHP 代码的 PHP 进程——而不是 Apache 本身
- 使用 PHP 作为Apache 模块(称为
mod_php):PHP 解释器被“嵌入”在 Apache 进程中:没有外部 PHP 进程——这意味着 Apache 和 PHP 可以更好地通信。
And re-edit, after the comment: using CGI or mod_phpis up to you : it's only a matter of configuration of your webserver.
并在评论后重新编辑:使用 CGI 或mod_php由您决定:这只是您的网络服务器配置的问题。
To know which way is currently used on your server, you can check the output of phpinfo(): there should be something indicating whether PHP is running via mod_php(or mod_php5), or via CGI.
要知道您的服务器上当前使用的是哪种方式,您可以检查以下输出phpinfo():应该有一些内容表明 PHP 是通过mod_php(或mod_php5)还是通过 CGI 运行。
You might also want to take a look at the php_sapi_name()function : it returns the type of interface between web server and PHP.
您可能还想看看这个php_sapi_name()函数:它返回 Web 服务器和 PHP 之间的接口类型。
If you check in your Apache's configuration files, when using mod_php, there should be a LoadModuleline looking like this :
如果您检查 Apache 的配置文件,在使用时mod_php,应该有LoadModule一行如下所示:
LoadModule php5_module modules/libphp5.so
(The file name, on the right, can be different -- on Windows, for example, it should be a .dll)
(右侧的文件名可以不同——例如,在 Windows 上,它应该是.dll)
回答by SpikETidE
This answer is taken from TuxRadar:
这个答案来自TuxRadar:
When running PHP through your web server, there are two distinct options: running it using PHP's CGI SAPI, or running it as a module for the web server. Each have their own benefits, but, overall, the module is generally preferred.
Running PHP as a CGI means that you basically tell your web server the location of the PHP executable file, and the server runs that executable, giving it the script you called, each time you visit a page. That means each time you load a page, PHP needs to read php.ini and set its settings, it needs to load all its extensions, and then it needs to start work parsing the script - there is a lot of repeated work.
When you run PHP as a module, PHP literally sits inside your web server - it starts only once, loads its settings and extensions only once, and can also store information across sessions. For example, PHP accelerators rely on PHP being able to save cached data across requests, which is impossible using the CGI version.
The obvious advantage of using PHP as a module is speed - you will see a big speed boost if you convert from CGI to a module. Many people, particularly Windows users, do not realise this, and carry on using the php.exe CGI SAPI, which is a shame - the module is usually three to five times faster.
There is one key advantage to using the CGI version, though, and that is that PHP reads its settings every time you load a page. With PHP running as a module, any changes you make in the php.ini file do not kick in until you restart your web server, which makes the CGI version preferable if you are testing a lot of new settings and want to see instant responses.
通过 Web 服务器运行 PHP 时,有两种不同的选择:使用 PHP 的 CGI SAPI 运行它,或者将它作为 Web 服务器的模块运行。每个都有自己的好处,但总的来说,该模块通常是首选。
将 PHP 作为 CGI 运行意味着您基本上告诉您的 Web 服务器 PHP 可执行文件的位置,并且服务器运行该可执行文件,并在您每次访问页面时为其提供您调用的脚本。这意味着每次加载页面时,PHP 都需要读取 php.ini 并设置其设置,它需要加载其所有扩展,然后它需要开始解析脚本——有很多重复的工作。
当您将 PHP 作为模块运行时,PHP 确实位于您的 Web 服务器中——它只启动一次,只加载一次设置和扩展,还可以跨会话存储信息。例如,PHP 加速器依赖于 PHP 能够跨请求保存缓存数据,这在使用 CGI 版本时是不可能的。
使用 PHP 作为模块的明显优势是速度 - 如果从 CGI 转换为模块,您将看到速度的大幅提升。许多人,尤其是 Windows 用户,没有意识到这一点,并继续使用 php.exe CGI SAPI,这是一种耻辱——该模块通常快三到五倍。
不过,使用 CGI 版本有一个关键优势,那就是每次加载页面时 PHP 都会读取其设置。当 PHP 作为模块运行时,您在 php.ini 文件中所做的任何更改都不会生效,直到您重新启动 Web 服务器,如果您正在测试大量新设置并希望看到即时响应,这使得 CGI 版本更可取。
回答by cdated
Your server needs to have the php modules installed so it can parse php code.
您的服务器需要安装 php 模块,以便它可以解析 php 代码。
If you are on ubuntu you can do this easily with
如果你在 ubuntu 上,你可以很容易地做到这一点
sudo apt-get install apache2
sudo apt-get install php5
sudo apt-get install libapache2-mod-php5
sudo /etc/init.d/apache2 restart
Otherwise you may compile apache with php: http://dan.drydog.com/apache2php.html
否则你可以用 php 编译 apache:http: //dan.drydog.com/apache2php.html
Specifying your server OS will help others to answer more specifically.
指定您的服务器操作系统将有助于其他人更具体地回答。
回答by Ondrej Slinták
It means that you have to have PHP installed as a module in Apache, instead of starting it as a CGI script.
这意味着您必须将 PHP 作为模块安装在 Apache 中,而不是将其作为 CGI 脚本启动。
回答by Tejas Sarade
Just to add on these answers is that, mod_php is the oldest and slowest method available in HTTPD server to use PHP. It is not recommended, unless you are running old versions of Apache HTTPD and PHP. php-fpm and proxy_cgi are the preferred methods.
除了这些答案之外,mod_php 是 HTTPD 服务器中可用的最古老和最慢的使用 PHP 的方法。不推荐使用,除非您运行的是旧版本的 Apache HTTPD 和 PHP。php-fpm 和 proxy_cgi 是首选方法。
回答by kta
mod_php is a PHP interpreter.
mod_php 是一个 PHP 解释器。
From docs, one important catch of mod_php is,
从文档中,mod_php 的一个重要问题是,
"mod_php is not thread safe and forces you to stick with the prefork mpm (multi process, no threads), which is the slowest possible configuration"
“mod_php 不是线程安全的,并迫使您坚持使用 prefork mpm(多进程,无线程),这是最慢的配置”

