13

What are the differences between mod_php and cgi php script?

I mean, why it's better (is it?) to use mod_php instead simple php scripts, running them as CGIs?

Thanks

Simone Margaritelli
  • 4,584
  • 10
  • 45
  • 70
  • 1
    You can also run PHP using FastCGI that eliminates most of CGI's overhead, and allows you to run a threaded apache MPM (PHP tends not to like threaded MPMs) – Reece45 May 02 '10 at 16:10
  • @Reece45, MPM?͏͏͏͏͏͏͏͏͏͏ – Pacerier Apr 03 '15 at 12:54
  • @Pacerier Its an apache-specific module (Multi-Processing Module) that implements different methods to handle multiple requests at once. See http://httpd.apache.org/docs/2.4/mpm.html for more details. – Reece45 Apr 03 '15 at 13:07
  • @Reece45, Do you use that? – Pacerier Apr 06 '15 at 13:47

3 Answers3

12

When using CGI : a PHP process is launched by Apache, and it is that PHP process that interprets PHP code -- not Apache itself.

In theory, a distinct PHP process has to be created for each request -- which makes things slower : Apache has more work to do to answer a request.
(Well, as pointed out by @AlReece45 in a comment, this can be made better using FastCGI)


When using PHP as an Apache module (mod_php, or mod_php5), the PHP interpreter is kind of "embedded" inside the Apache process : there is no external PHP process.

Which means :

  • No forking to answer a request (faster)
  • Better communication between Apache and PHP


Generally speaking, I would say that mod_php is the solution that's used the most.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • 3
    Enter stage right, FastCGI. FastCGI has the advantages of CGI, and scales much better. :) mod_php can be death on a loaded server. – Xorlev May 02 '10 at 16:11
  • @Xorlev for the loaded server think of nginx + phpfpm ;) – Your Common Sense May 02 '10 at 17:14
  • 3
    @Mike: any language can cause death for humankind in the wrong hands. If you know a language well enough, and are a senior programmer (in the broad sense, not just one language), then no language is bad. Each language has its own pros and cons and there's no silver bullet for all problems. – Seb Mar 16 '11 at 01:19
  • When you use mod_php, the apache processes use more memory, it can be a really bad idea if you are serving many static files, which don't use PHP, using up all your available memory and bringing your server to its knees – carlosvini Aug 26 '14 at 15:38
  • @carlosvini, Surely there's a way to config that based on the request? E.g. don't bother loading the PHP part if you see a request for .gif files. – Pacerier Apr 03 '15 at 12:56
  • @Pascal, What do you mean by *"kind of "embedded" inside the Apache process"*? How does that work? – Pacerier Apr 03 '15 at 12:58
  • Interestingly, the benefit of CGI/FastCGI in a shared environment is explained in this answer: https://serverfault.com/a/36873 – Éric Apr 01 '22 at 11:02
3

Plain CGI requires process to be spawned for each request at the time of request.

mod_php requires you to use bloated apache instead of slick nginx or lighttpd. Besides, "better communication between Apache and PHP" mentioned by Pascal may harm apache (it harms anyone who develops in php!;-)).

FastCGI lets you separate php from the web server (possibly run it on the different host).

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
1

Also, php.net just released a vulnerability today where source code disclosure is possible if you are using mod_cgi to run PHP and your PHP version is older than PHP 5.3.12 or PHP 5.4.2.

http://www.php.net/archive/2012.php#id2012-05-03-1

Patch by upgrading or applying a mod_rewrite rule.

Wilk
  • 7,873
  • 9
  • 46
  • 70
xs411
  • 11
  • 1