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

4
  • 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) Commented May 2, 2010 at 16:10
  • @Reece45, MPM?͏͏͏͏͏͏͏͏͏͏ Commented Apr 3, 2015 at 12:54
  • @Pacerier Its an apache-specific module (Multi-Processing Module) that implements different methods to handle multiple requests at once. See httpd.apache.org/docs/2.4/mpm.html for more details. Commented Apr 3, 2015 at 13:07
  • @Reece45, Do you use that? Commented Apr 6, 2015 at 13:47

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Enter stage right, FastCGI. FastCGI has the advantages of CGI, and scales much better. :) mod_php can be death on a loaded server.
@Xorlev for the loaded server think of nginx + phpfpm ;)
@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.
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, 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.
|
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).

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.