What's the difference from using PHP as an Apache module, CGI, FastCGI, and command line?

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

Author: Wallace Maxters, 2016-06-04

1 answers

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

Apache Module (mod_php)

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

When using mod_php the PHP interpreter is embedded in every Apache process generated on the server. In this way, each Apache process is able to handle and execute PHP scripts on its own eliminating the need to handle any external processes, unlike what happens with CGI or FastCGI.

This becomes mainly useful in web-sites with a high use of PHP, for example if we use WordPress, Drupal, Joomla, among others, given that all requests can be manipulated by Apache.

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

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

Advantages

  • 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 .htaccess directives.

Disadvantages

  • causes each Apache process is larger (more RAM consumed);
  • loads the PHP interpreter even if it is serving static content (images);
  • the web server is the owner of the files created by the 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 it was quickly considered inefficient to use on anything other than very small sites.

The advantage of CGI execution mode is that it keeps code execution separate from the web server, which allows for some additional security benefits. For example, a PHP script with bugs or security flaws does not affect any files outside of the domain in which it is located (particularly advantageous for web hosting. This also means that the PHP interpreter is only called when needed, 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 consumed server resources.

Advantages

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

Disadvantages

  • poor performance.

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 of the web server, including the Security benefits of CGI mode without any of its inefficiencies.

When running 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 by their own server environments. However, a similar result can be obtained by using nginx ahead of Apache.

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

The disadvantage of running PHP with FastCGI support is that PHP directives defined in a .htaccess file are not respected. Alternatively, you can define PHP directives in a php.ini file.

Advantages

  • greater security, given the execution of PHP code in an isolated environment of the web server;
  • the static content is not processed by the PHP interpreter;
  • allows files to be managed by the system user without changing permissions.

Disadvantages

  • cannot use PHP directives in the .htaccess file.

PHP - FPM (FastCGI Process Manager)

Is an alternative implementation to PHP FastCGI with some additional features useful for web-sites of any size, especially for the most heavy.

Advantages

  • growth of processes adaptively;
  • basic statistics (similar to Apache's mod_status);
  • 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 code destruction (cache);
  • supports accelerated upload;
  • several improvements to its FastCGI facet.

Disadvantages

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

CLI (command lines)

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

  • by default, does not write headers for the output;
  • some php.ini directives are overwritten by the CLI because they don't make sense in the shell :
    • html_errors: default on CLI is FALSE
    • implicit_flush: default on CLI is TRUE
    • max_execution_time: default on CLI is 0 (Unlimited)
    • register_argc_argv: default on CLI is TRUE
  • allows you to pass arguments from command line for the script ;
  • We have access to 3 constants defined for the shell environment: STDIN, STDOUT, STDERR;
  • does not change the current directory to the directory of the executed script. It means that the current directory is where we type the command to run the script.

Essentially, it is a way to run PHP scripts for performing 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 execution mode becomes advantageous for a particular scenario as we can observe 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          │
└───────────────────────────────────┴──────────────────────────────┘
 40
Author: Zuul, 2020-06-11 14:45:34