09 Nov 2018

Difference between PHP-CGI and PHP-FPM

Confused to use PHP-CGI or PHP-FPM for your websites on your web server? We will share some information about PHP-CGI and PHP-FPM in this article.

When running PHP through the web server, there are two distinct options: running it using PHP’s CGI, or running it as a PHP-FPM, for the web server.

Let’s learn some basic information about PHP-CGI and PHP-FPM

PHP-CGI

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

Executing PHP scripts with a CGI application is the legacy way of running applications on a web server, it’s highly inefficient and rarely used.

A benefit of running applications on CGI is that it keeps the code execution separate from the web server, which allows for some added security benefits. For example, a buggy or insecure PHP script executed via PHP-CGI cannot corrupt or affect the security of any other files outside of the domain it’s hosted on. It also means that the PHP interpreter is only called when required, thereby allowing static content to be served solely by the web server.

The inefficiencies of running PHP with CGI support spawn from requiring a new process to be created each time any PHP code needs to be executed. As you can imagine, on busier sites or PHP based applications it can be very resource intensive.

Running PHP as a CGI means that you basically tell your web server the location of the PHP executable file, and the server runs that executable, giving it the script you called, each time you visit a page. That means each time you load a page, PHP needs to read php.ini and set its settings, it needs to load all its extensions, and then it needs to start work parsing the script – there is a lot of repeated work.

There is one key advantage 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, any changes you make in the php.ini file do not kick in until you restart your web server, which makes the CGI version preferable if you are testing a lot of new settings and want to see instant responses.

Pros

  • Better security than FPM as PHP code execution is isolated from a web server.

Cons

  • The legacy way of running applications.
  • Very poor performance for busier websites.

PHP-FPM

PHP FastCGI Process Manager (PHP-FPM) is an alternative FastCGI daemon for PHP that allows a website to handle strenuous loads. PHP-FPM maintains pools (workers that can respond to PHP requests) to accomplish this. PHP-FPM is faster than traditional CGI-based methods, such as SUPHP, for multi-user PHP environments. It does not overload a system’s memory with PHP from Apache processes.

PHP-FPM features include:

  • Adaptive process spawning.
  • Basic statistics (ala Apache’s mod_status)
  • Advanced process management with graceful stop/start
  • Ability to start workers with different uid/gid/chroot/environment and different php.ini (replaces safe_mode)
  • Stdout & stderr logging.
  • Emergency restart in case of accidental opcode cache destruction.
  • Accelerated upload support.
  • Support for a “slowlog”.
  • Enhancements to FastCGI, such as fastcgi_finish_request() – a special function to finish request & flush all data while continuing to do something time-consuming (video converting, stats processing, etc.)

Pros

  • It has a modern and optimized way of running applications.
  • Robust performance for busier websites and low resources consumption.
  • Smaller memory footprint, graceful reload without stopping other queries.

Cons

  • Low security as compared to PHP-CGI.
  • Requires more configuration than PHP-CGI.

Which should be preferred?

According to us if you have a heavy and busier website and have low-end server resources and don’t wish to upgrade server then PHP-FPM should be used as it is low resource hogger. If CGI is used then essential web server process might end up in the deadlock situation which may lead to website downtime.

If you have a basic website and low traffic then you should use PHP-CGI as it has better PHP code Execution and will not use many resources on small and low traffic websites.