tags:

views:

188

answers:

2

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

+3  A: 

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
Enter stage right, FastCGI. FastCGI has the advantages of CGI, and scales much better. :) mod_php can be death on a loaded server.
Xorlev
@Xorlev for the loaded server think of nginx + phpfpm ;)
Col. Shrapnel
php in whatever form can cause death for humankind! ;-)
Michael Krelin - hacker
+2  A: 

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