What’s the difference of using PHP as a module of Apache, CGI, Fastcgi and command line?

Asked

Viewed 8,177 times

14

I wanted to know what difference there is between the various "versions" or "ways" to use PHP, as it has the module of Apache, CGI, Fastcgi and command line. So I wonder if there is any difference or "rule" to which I should use and in which situation.

  • 1
  • Difference between PHP and CGI?

  • not php-cgi I took the - because he wasn’t letting me send the question

  • Difference in relation to what?

  • php normal php and php-cgi

  • does not have "normal PHP". It has PHP cgi, fastcgi, PHP module, and PHP command line. PHP is the same, what changes is how to interact with it. Although the answer is weak, it is explained here http://answall.com/q/93308/91

  • minutes thank you very much

  • 2

    @Arthurporto I will see if I find any better explanation and I put the link. To tell the truth, It is bad the response indicated link. Here is something too: https://pt.wikipedia.org/wiki/CGI - The important thing is to know that in CGI, the web server forwards the request to an executable (in the case of PHP, it is precisely PHP cgi), and what comes out of this executable, is returned to the browser.

  • 1

    @Arthurporto edited your question to try to make it more specific to PHP, and I put a bonus on that one for those who put a more elaborate answer, so I hope that in a few days you will have a better answer, both here and there. Is that weekend is slower the site even.

  • thank you very much, vlw

Show 5 more comments

1 answer

40


Note: They are not "versions" or "manners"... they are modes of execution. I will try to highlight the main differences between them, advantages and disadvantages of each one. In summary a hint of the ideal environment for each one.

Apache Module (mod_php)

Using the mod_php to run PHP scripts on a web server, is undoubtedly the most popular method and for a long time was the default execution mode on a web server implementations.

When using the mod_php the PHP interpreter is embedded in each Apache process generated on the server. In this way, each Apache process is able to handle and run PHP scripts by itself eliminating the need to handle any external processes, unlike CGI or Fastcgi.

This is mainly useful on web-sites with a high use of PHP, for example if we use Wordpress, Drupal, Joomla, among others, since all requests can be handled by Apache.

Since the interpreter is started with Apache, this allows it to run quickly, since it can cache certain information without having to repeat tasks each time a script is run.

The big disadvantage of this mode is that every Apache process gets bigger and bigger (it consumes more memory and server resources). Even if Apache is only serving static content, such as images, since it always contains the PHP interpreter, memory and resource consumption is always high.

Perks

  • PHP code executed by Apache;
  • No need for external processes;
  • Excellent performance on web-site/platforms with great use of PHP;
  • PHP configuration settings can be customized within PHP directives .htaccess.

Disadvantages

  • Makes every Apache process bigger (more RAM consumed);
  • Load the PHP interpreter even if it is serving static content (images);
  • The web server is the owner of the files created by scripts PHP instead of the system user (with changes to the configuration you can change).

CGI

Running PHP scripts with a CGI application is the original way to run applications on a web server, it is very inefficient and rarely used. Introduced around 1990 was quickly considered inefficient to use on anything other than very small websites.

The advantage of the CGI execution mode is that it keeps code execution separate from the web server, which allows some additional security benefits. For example, a PHP script with bugs or security holes does not affect any files outside the domain you are in (particularly advantageous for web hosting servers. This also means that the PHP interpreter is only called when necessary, thus allowing static content to be served solely by the web server.

The great inefficiency of CGI mode is the fact that it opens a new process whenever it is necessary to execute PHP code. On web-sites/platforms that make high use of PHP, you quickly get server resources consumed.

Perks

  • Better security than the mod_php since PHP code execution is isolated from the web server.

Disadvantages

  • Underperformance.

Fastcgi

Fastcgi was introduced as a middle ground between the Apache module and the CGI application. It allows programs to be run by an interpreter outside the web server, including the security benefits of CGI mode without any of its inefficiencies.

When executing scripts PHP with Fastcgi each request is transmitted from the web server to Fastcgi through a communication layer. This allows for greater scalability as the web server and PHP interpreter can be divided into their own server environments. However, a similar result may be obtained using Nginx ahead of the Apache.

Fastcgi also has the advantage of running as the system user, which makes it as always the owner of any and all files created during the execution of scripts PHP.

The disadvantage of running PHP with Fastcgi support is that PHP directives defined in a file .htaccess are not respected. Alternatively, it is possible to define PHP directives in a file php.ini.

Perks

  • Increased security, given the execution of PHP code in a web server isolated environment;
  • Static content is not processed by the PHP interpreter;
  • Allows files to be managed by the system user without changing permissions.

Disadvantages

  • Unable to use PHP directives in file .htaccess.

PHP-FPM (Fastcgi Process Manager)

It is an alternative implementation to PHP Fastcgi with some useful additional features for websites of any size, especially for the heavier ones.

Perks

  • Growth of processes in an adaptive way;
  • Basic statistics (similar to mod_status apache);
  • Advanced process management with graceful start/stop;
  • Ability to start workers with different uid, gid, chroot, environment and php.ini (replaces safe_mode)
  • Creates logs for stdout and stderr;
  • Emergency restart in case of accidental destruction of code (cache);
  • Supports accelerated upload;
  • Several improvements to your Fastcgi facet.

Disadvantages

  • Until 2011 it was in experimental version, currently its disadvantage is its little use (few implementations compared to the remaining PHP modes).

CLI (command lines)

This mode of execution is very similar to the CGI mode, bringing the following advantages over the same:

  • By default, do not write headers to output;
  • Some directives of php.ini are superscripted by the CLI because they make no sense in the environment shell:
    • html_errors: default in CLI is FALSE
    • implicit_flush: default on CLI is TRUE
    • max_execution_time: standard on CLI is 0 (unlimited)
    • register_argc_argv: default on CLI is TRUE
  • Allows you to pass command line arguments to the script;
  • We have access to 3 constants defined for the shell environment: STDIN, STDOUT, STDERR;
  • Does not change the current board of directors to the script executed. It means that the current directory is where we type the command to execute the script.

Essentially, it’s a way to execute scripts PHP to perform tasks on the server, either with files, data manipulation, database maintenance and/or other more time consuming or heavy things that cannot run via SAPI.


Summary

To summarize, each mode of execution becomes advantageous to a particular scenario as we can see in the following table:

┌───────────────────────────────────┬──────────────────────────────┐
│ Apache Module (mod_php)           │ Alto desempenho em           │
│                                   │ web-sites simples            │
├───────────────────────────────────┼──────────────────────────────┤
│ CGI                               │ ...não utilizar...           │
│                                   │                              │
├───────────────────────────────────┼──────────────────────────────┤
│ FastCGI                           │ Ideal para qualquer tipo     │
│                                   │ web-sites/aplicações         │
├───────────────────────────────────┼──────────────────────────────┤
│ PHP-FPM (FastCGI Process Manager) │ Supostamente +vantajoso que  │
│                                   │ o FastCGI para qualquer tipo │
│                                   │ de web-sites/aplicações!     │
├───────────────────────────────────┼──────────────────────────────┤
│ CLI (linhas de comandos)          │ Para scripts que realizam    │
│                                   │ tarefas no servidor          │
└───────────────────────────────────┴──────────────────────────────┘
  • Great answer, I only had a doubt, Apache Module refers to Apache2handler?

  • 1

    @Guilhermenascimento Not exactly, the Apache2Handler may not be installed. Apache module mod_php is just one of many modules present in Apache.

  • I understand, is that I think that the idea of AP was referring to the ways to use PHP, maybe quoting apache2handler would be interesting (although prefer PHP-FPM), but I’ve already left the +1 guaranteed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.