PHP-CGI and PHP-FPM

The Difference between PHP-CGI and PHP-FPM

Confused to use PHP-CGI and PHP-FPM for your website on your web server? In this article, we will share some information about PHP-CGI and PHP-FPM. When running PHP through a web server. There are two distinct options: to run it using PHP’s CGI or to run it as a PHP-FPM for the web server. Let’s know some basic information about PHP-CGI and PHP-FPM.

PHP-CGI

It is a specification “protocol” for transferring data between a web server and a CGI program. A CGI program is any program designed to receive and return data conforming to the CGI specification. Basically, it’s a way to run a server-side script (PHP, Perl, Python) when an HTTP request arrives.

Running PHP scripts alongside a CGI application is the legacy way of running applications on a web server. Which is very inefficient and rarely used.

An advantage of running applications in CGI is that it keeps code execution separate from the web server. Which allows for some additional security benefits. For example, a buggy or insecure PHP script executed via PHP-CGI cannot corrupt or affect the security of any other file outside the domain. Where it is hosted. This means that the PHP interpreter is only called when needed, resulting in static content only being served by the web server.

The inefficiency of running PHP with CGI support stems from the need to create a new process every time any PHP code needs to be executed. As you can imagine, this can be very resource intensive on busy sites or PHP-based applications.

Running PHP as a CGI means that you basically tell your web server the location of the PHP executable and the server runs that executable. The script you call it, every time you visit a page. That means every time you load a page, PHP has to read php.ini and set the settings, load all of its extensions, and then it has to start parsing the script – many times over.

There is one key benefit to using the CGI version though, and that is that PHP reads its settings every time you load a page. With PHP running as a module, you don’t make any changes to the php.ini file until you restart your web server, which prefers the CGI version if you’re testing a lot of new settings and want immediate feedback.

Advantages

  1. Better security than FPM because PHP code execution is isolated from a web server.

Disadvantages

  1. Legacy way of running applications.
  2. Very poor performance for busy websites.

PHP-FPM

PHP FastCGI Process Manager (PHP-FPM) is an alternative FastCGI daemon for PHP that allows a website to handle a strict load. PHP-FPM maintains pools (workers who can respond to PHP requests) to accomplish this. For multi-user PHP environments, PHP-FPM is faster than conventional CGI-based approaches, such as SUPHP. It does not overload the memory of a system with PHP from the Apache process.

PHP-FPM features include:

  1. Adaptive process spawning.
  2. Basic statistics (ala Apache’s mod_status)
  3. Advanced process management with graceful stop/start
  4. Ability to initiate workers with various uid/gid/chroot/environment and different php.ini (replaces safe_mode)
  5. Stdout and stderr logging.
  6. Emergency restart in case of accidental opcode cache destruction.
  7. Expedited upload support.
  8. Support for a “sloglog”.
  9. Enhancements to FastCGI, such as fastcgi_finish_request() – a special function to finish requests and flush all data while continuing to do something time-consuming (video conversion, statistics processing, etc.).

Advantages

  • It has a modern and optimized way of running the application.
  • Powerful performance for busy websites and low resource consumption.
  • Small memory footprint, graceful reload without stopping other queries.

Disadvantages

  1. Less security than PHP-CGI.
  2. Requires more configuration than PHP-CGI.

Which should be preferred?

In our opinion, if you have a heavy and busy website and low-end server resources and you don’t want to upgrade the server then you should use PHP-FPM because it is a low-resource hogger. If CGI is used then essential web server processes can end up in a deadlock which can lead to website downtime.

If you have a basic website and low traffic, you should use PHP-CGI because it has better PHP code execution and won’t use many resources on small and low-traffic websites.

 

Scroll to Top