Which PHP mode? Apache vs CGI vs FastCGI

There are multiple ways to execute PHP scripts on a web server. We’re often asked about the difference between these modes, so here it is!
We offer the three most common PHP handlers across our Linux Cloud Hosting range:

  • Apache module
  • CGI
  • FastCGI

Each of these has its own advantages and disadvantages.

Apache Module (mod_php)

Using mod_php to execute PHP scripts on a web server is the most popular method used by our customers and until recently was the default mode we set when you create a new webspace.

When using mod_php the PHP interpreter is embedded in each Apache process that’s spawned on the server. This way every Apache worker is able to handle and execute PHP scripts itself removing the need to deal with any external processes; unlike CGI or FastCGI. This makes it very useful for sites that are ‘PHP heavy’ where lots of requests are likely to contain PHP code (such as WordPress, Drupal, Joomla, etc.) because all the requests can be handled by Apache.

As the interpreter is started along with Apache, it allows it to run very quickly as it can cache certain information and doesn’t need to repeat the same tasks each time a script is executed.

The downside to this is that the footprint for each Apache process is larger as it requires more system resources with the PHP interpreter embedded. Even when serving static content such as images, text and style sheets where no PHP code needs to be executed, the process still contains the PHP interpreter.

Pros

  • PHP code executed by Apache.
  • No external processes required.
  • Very good performance on PHP heavy sites.
  • PHP configuration settings may be customized within .htaccess directives.

Cons

  • Makes each Apache process footprint larger – meaning more RAM used.
  • Loads PHP interpreter for non-PHP content.
  • Files created by PHP scripts are usually owned by the web server so you cannot edit them via FTP later.

CGI

Executing PHP scripts with a CGI application is the legacy way of running applications on a web server, it’s highly inefficient and rarely used. It was originally introduced in the 1990’s but was deemed to be too inefficient to use on anything other than very small sites.

A benefit of running applications on CGI is that it keeps the code execution separate from the web server, which allows for some added security benefits. For example, a buggy or insecure PHP script executed via FastCGI cannot corrupt or affect security of any other files outside of the domain it’s hosted on. It also means that the PHP interpreter is only called when required, thereby allowing static content to be served solely by the web server.

The inefficiencies of running PHP with CGI support spawn from requiring a new process to be created each time any PHP code needs to be executed. As you can imagine, on busier sites or PHP based applications it can be very resource intensive.

Very few Layershift customers use this mode, and we don’t recommend it!

Pros

  • Better security than mod_php (above) as PHP code execution is isolated from web server.

Cons

  • Legacy way of running applications.
  • Very poor performance.

FastCGI

FastCGI was introduced as a middle ground between the PHP Apache Module and the CGI application. It allows scripts to be executed by an interpreter outside of the web server and includes the security benefits of CGI but doesn’t include any of the inefficiencies of CGI.

When executing PHP scripts with FastCGI each request is passed from the web server to FastCGI via a communication socket. This allows for much greater scalability as the web server and the PHP interpreter can be split into their own individual server environments if necessary. However a similar end result can also be achieved using nginx in front of Apache (such that nginx handles basic requests itself and only passes dynamic requests to Apache) so this point alone doesn’t determine the ideal choice for a given scenario.

In a Plesk environment, FastCGI is executed as the domain FTP user and is the default PHP handler we offer on all our Linux services running the latest versions of Plesk Panel.

The downside of running PHP with FastCGI support is that any PHP directives defined in a .htaccess will not be used. As a workaround, it is possible to set PHP directives on a per domain basis with a custom php.ini file on any Plesk Panel server.

Pros

  • Improved security as PHP code execution is isolated from web server.
  • Static content will not be processed by PHP interpreter.
  • Allows files to be managed by your FTP user without altering permissions afterwards.

Cons

  • Cannot use PHP directives in .htaccess. This is expected by many popular scripts.
  • Requires PHP requests to be passed from web server.

Which Should I use?

On smaller sites, it usually comes down to personal preference when deciding which mode of PHP support you want. We often see customers that are running CMS applications (such as WordPress, Drupal, Joomla, etc.) tend to prefer FastCGI as it allows FTP and PHP scripts equal access, meaning file upload and edit functionality within the CMS works as advertised without any special file permission configuration.

This is only an overview of a very complex and in-depth issue. I have presented some of the main considerations to inform your decision, but each site is unique so please contact our support team if you need further guidance. For a guide detailing how to change the PHP handler using Plesk, see our accompanying knowledgebase article.

原文:http://blog.layershift.com/which-php-mode-apache-vs-cgi-vs-fastcgi

Which PHP mode? Apache vs CGI vs FastCGI的更多相关文章

  1. CGI、FastCGI、CLI、Apache、ISAPI之PHP运行环境对比

    1.运行模式 关于PHP目前比较常见的五大运行模式: 1)CGI(通用网关接口 / Common Gateway Interface) 2)FastCGI(常驻型CGI / Long-Live CGI ...

  2. CGI与FastCGI nginx+PHP-FPM

    本文转载自CGI与FastCGI 1.当我们在谈到cgi的时候,我们在讨论什么 最早的Web服务器简单地响应浏览器发来的HTTP请求,并将存储在服务器上的HTML文件返回给浏览器,也就是静态html. ...

  3. CGI与FastCGI

    当我们在谈到cgi的时候,我们在讨论什么 最早的Web服务器简单地响应浏览器发来的HTTP请求,并将存储在服务器上的HTML文件返回给浏览器,也就是静态html.事物总是不 断发展,网站也越来越复杂, ...

  4. php CGI、Fastcgi、PHP-FPM的详细介绍与之间的关系

    以下PHP CGI.Fastcgi.PHP-FPM的一些信息归纳和汇总----->详细介绍与之间的关系 一:CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的 web ...

  5. 什么是CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI?

    什么是CGI CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用 ...

  6. CGI、FastCGI和PHP-FPM关系图解

    CGI.FastCGI和PHP-FPM关系图解   webapp即是php解析器等 当Web Server收到 index.php 这个请求后,会启动对应的 CGI 程序,这里就是PHP的解析器.接下 ...

  7. CGI、FastCGI 知识总结

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  8. CGI与FastCGI 转

    CGI与FastCGI 当我们在谈到cgi的时候,我们在讨论什么 最早的Web服务器简单地响应浏览器发来的HTTP请求,并将存储在服务器上的HTML文件返回给浏览器,也就是静态html.事物总是不 断 ...

  9. 转:什么是CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI?

    什么是CGI CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用 ...

随机推荐

  1. dir_colors linux颜色配置

    在控制台下,用ls,就会发现,shell将不同类型的文件项目显示为不同的颜色.者可以提高效率,不用ls -l便能大概的把各个文件的类型情况了解一下. 你有没有想过更改这个着色配置呢? 其实,在/etc ...

  2. Emgu学习笔记(一)安装及运行Sample

    1.简单说明 Emgu是Dot Net平台对OpenCV的封装,本质上没有增加新功能,是通过Dot Net的平台调用技术直接调用OpenCV C++语言写的库,使用我们可以方便用.net平台通过Ope ...

  3. 【转】mysql行列转换方法总结

    转:http://blog.chinaunix.net/uid-7692530-id-2567582.html 在某些数据库中有交叉表,但在MySQL中却没有这个功能,但网上看到有不少朋友想找出一个解 ...

  4. Loader之二:CursorLoader基本实例

    参考APIDEMO:sdk\samples\android-19\content\LoaderCursor 1.创建主布局文件,里面只包含一个Fragment. <FrameLayout xml ...

  5. xss框架(二)基础框架实现

    简述 自上一篇博客介绍浏览器通信以来已经过去将近两个月了,兜兜转转挖了不少坑,也走了很多弯路.期间研究saml2.0和单点登录等技术都最后无疾而终. 只有xss框架这部分坚持了下来,这个框架还有很多事 ...

  6. hyper中安装wdOS-1.0-x86_64(wdlinux)遇到的网卡问题

    11/23 0:30 by vmaxhyper中安装wdOS-1.0-x86_64(wdlinux) 日志: 遇到的问题: 1.装完找不到网卡eth0,只有一只loopback的Lo. 原因:cent ...

  7. SQL Server 索引整理与堆重组。

    重新组织索引: alter index idx_OrderID      on dbo.OrderDetail      reorganize | reorganize;---可以rebuild 也可 ...

  8. 为openwrt编译xd-h3c

    西电老校区,openwrt上用的认证软件. 在package下新建一目录"xd-h3c",在里面新建一个Makefile,内容如下: include $(TOPDIR)/rules ...

  9. 如何实现Linux下的U盘(USB Mass Storage)驱动

    如何实现Linux下的U盘(USB Mass Storage)驱动 版本:v0.7 How to Write Linux USB MSC (Mass Storage Class) Driver Cri ...

  10. 电子科大POJ "任意阶矩阵相乘"

    任意阶矩阵的乘法 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) C-sourc ...