What are the benefits of running PHP as a CGI binary compared to running PHP as an Apache module? Are there:

  1. Security benefits?
  2. Stability benefits?
  3. Performance benefits?

I've always installed and used PHP as an Apache module so I'm curious if there's any reason for me to use PHP as a CGI binary for future work...


  • 1
    @Aaron - Whether you compiler PHP from source or not, you'll still end up with an Apache module. Your question should be "Should I use PHP installed as CGI binary or module?" – Francois Deschenes Jun 19 '11 at 2:11
  • Thanks Francois. I've updated the question to reflect your corrections. I guess I misunderstood what I was trying to ask... – Aaron Jun 19 '11 at 2:15
  • 2
    Generally: CGI is slower than the mod_php interface. But FastCGI is another story. And the security advantage of using either comes into play if you also utilize mod_suphp/suexec. – mario Jun 19 '11 at 2:19
  • Is there a noticeable speed difference when running PHP under FastCGI vs. PHP as a module? – Aaron Jun 19 '11 at 2:24
  • Btw, this question is [::::] stackoverflow.com/questions/3953793/mod-php-vs-cgi-vs-fast-cgi – gaRex Jun 19 '11 at 3:11

When PHP is used as a module, it's more-or-less compiled into Apache's code itself. It's loaded for every request which means that it's loaded even for images, scripts, stylesheets and any other files that aren't PHP. It makes serving those pages slower. On the other hand, it makes PHP faster since Apache doesn't have to spawn additional processes.

When PHP is used as a CGI binary, it's more secure but it's slower. Apache can spawn processes under different users which, for example, can increase the security of a shared hosting environment. It also increases stability because the PHP processes are separate from Apache's and in the event of a segfault or other hard crash, PHP processes won't affect Apache. Using PHP as a CGI means that you can't use .htaccess file configure PHP using php_flag or php_value.

The question is, what's more important to you. Speed or security.

If you search for the benefits of using PHP in either CGI or modules on Google, I'm sure you'll find plenty of great articles that may cover things I may have missed.

| improve this answer | |

Typically you have 3 options: - php as webserver`s module (apache or iis) -> mod_php - cgi (old style each time start-up binary) - fast-cgi (more optimized binary)

By fast-cgi we means that it's apache`s fast-cgi without any fpm proxies and etc.

In both CGI modes your HTTP auth scripts will be not working.

Fastcgi is better than just CGI -- @see gugl for it, but 500 errors may happen.

By stability & performance mod_php is better.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.