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

Comments

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)

Written by Reece45

Accepted Answer

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.

Written by Pascal MARTIN
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki