When the server receives a request from the user it can serve the file directly, but sometimes it needs to translate the code using the appropriate application. Otherwise it would be showing the users the source code of the website. A server like Apache needs to be configured to handle specific content with an appropriate application like PHP.
A common set of rules of passing the information between the server and the application is the Common Gateway Interface. This standard lets programmers write applications in different languages that can plug into the server and interact with it. The interface guarantees consistency of data passed between the server and the application. An alternative to a CGI application is Microsoft’s Active Server Page (ASP).
A CGI program is one that conforms to the above specified protocol. The advantage of using CGI over a server module is keeping the execution of the application separate from the server.
If CGI is installed on the server a cgi-bin directory might be added to the public directory of the website and every page from that directory is treated as a CGI script, so the server runs the files through a relevant application instead of showing the code directly to the user. The server can also be configured to treat files with specific extensions as CGI files.
If CGI is installed on the server, the specific cgi-bin directory is also added there, for example home/user/public_html/cgi-bin. CGI scripts are stored in this directory. Each file in the directory is treated as an executable program. When accessing a script from the directory, the server sends request to the application, responsible for this script, instead of sending file’s content to the browser. After the input data processing is completed, the application sends the output data to the web server which forwards the data to the HTTP client.
When PHP is run in a CGI mode the server knows the location of the executable file and the loads it, with all its settings from the php.ini file every time it needs to interpret a php file. With this kind of workflow comes a lot of repeated work. The advantage is slightly better security, because the code is isolated from the server and the ability to load the php settings directly, without having to restart the server.
The original CGI wasn’t very efficient and the FastCGI interface was introduced to deal with some of thess performance issues. One of the differences with the original is that the process spawned to handle the request is kept alive longer, so it can handle also some future requests.
FPM which stands for FastCGI Process Manager is a FastCGI implementation which has some features geared toward heavily trafficked websites. It maintains a pool of workers that can handle PHP requests and should avoid the memory overload from PHP in Apache processes. You can check out a list of features on the php.net website.
In case of using an Apache Module every child process that Apache spawns will contain its own copy of PHP interpreter. The downside of this solution is that the footprint of every spawned process is higher, so it consumes more server resources. In order to apply new PHP settings a server restart is required, but on the other hand PHP-specific settings can be included in the .htaccess files.