1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP as Apache Module or PHP as CGI?

Discussion in 'Off Topic' started by James, Feb 6, 2011.

  1. James

    James Well-Known Member

    I'm unsure what the differences are, which one is the better one (in general)?
  2. Trent Gillespie

    Trent Gillespie Active Member

    1. If you run php as a module, it is compiled with the Apache.
    2. If you run php as a cgi, it is treated as a separate executable and must be called each time.
    Apache is more efficient at executing php, than if you were to load it via cgi. In a smaller hosting environment, you won't notice much of a difference. My suggestion would be to use php as a module of Apache. However, if you are wanting to educate yourself, I would suggest trying out both instances of executing php code.
    James likes this.
  3. Jason

    Jason Well-Known Member

    Using the module has a slight performance gain, since it's compiled into the Apache binary, meaning the PHP interpreter runs in the Apache process. So, when Apache spawns a child, each process will already contain a binary image of PHP. On the other hand, CGI is a bit more secure, in that the server manages control and access to the binaries. Although, most security holes are due to the scripts themselves. There are a lot of poorly programmed PHP scripts out there.

    And, while CGI is slower there are solutions to that, such as FastCGI. If you are using a threaded MPM, then you'll want to avoid mod_php, since there are some extensions that are not thread safe, which will likely lead to race conditions.

    Personally, I prefer FastCGI to mod_php. If a FastCGI process dies, a new one is spawned. I've seen instances where mod_php has killed off Apache entirely.
    Darkimmortal and James like this.
  4. CyclingTribe

    CyclingTribe Well-Known Member

    I use mod_php for Apache2 and speed it up with eAccelerator, but since mod_php includes PHP in every Apache instance it increases the overall memory footprint for Apache so have a look at how this impacts your own server (not sure how many concurrent requests / child processes you have on the go or what memory you have in your machine?).

    As Jason has suggested an alternative you could look at is mod_fcgid, running FastCGI as separate processes (much like apache) spawning instances as needed; reducing the footprint of your Apache instances and only using as many CGI instances as required.

    Have a look here for some general info around the subject with some links to respective resources/further reading (http://2bits.com/articles/apache-fcgid-acceptable-performance-and-better-resource-utilization.html)

    Shaun :D
  5. Luke F

    Luke F Well-Known Member

    If you're bothering to set up FastCGI on Apache, you might as well go all the way and switch to Nginx+PHP-FPM
    smsmasters and Trent Gillespie like this.

Share This Page