CGI (Common Gateway Interface) is a web technology and protocol that defines a way for a web server (HTTP server) to interact with external applications, e.g. PHP. CGI enhances the web server capabilities to enable dynamic content generation and processing.
CGI is used as an interface between the web server and the additionally installed applications generating dynamic web content. These applications are called CGI scripts and are written in different script and programming languages such as PHP, Perl, Python, etc.
Back at the dawn of the World Wide Web the first web servers provided pages containing pre-written HTML code. This content could not have been changed, unless its developer had not modified the .html file. Web content used to be static and the available webpage code was passed from the web server to the browser.
With the CGI technology, however, the web server does not respond with an existing HTML file, but executes an application where it transfers the HTTP request data. The application then accepts and processes the arguments as it returns the output data to the web server. Only then the server returns a response to the request while forwarding the generated HTML code.
cgi-bin directory
Normally web pages, files and all of the documents which are transferred from the web server to the browser are stored in a specific public directory such as home/user/public_html. When the browser requests certain content, the server checks this directory and sends the required file to the browser.
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.
For example, when the CGI script http://mysitename.com/cgi-bin/file.pl is accessed, the server will run the appropriate Perl application through CGI. The data generated from script execution will be sent by the application to the web server. The server, on the other hand, will transfer data to the browser. If the server did not have CGI, the browser would have displayed the .pl file code itself.
The server can be configured to recognize not only CGI scripts in the cgi-bin directory, but also a specific file extension as a CG script. For example, all files with a .php extension can be treated as CGI scripts. In this case they should not be necessarily stored in the cgi-bin directory. In the web server settings you can choose the application to be used when a file having .php extension is accessed.
FastCGI
FastCGI is a newer web technology, an improved CGI version as the main functionality remains the same.
The need to develop FastCGI is that Web was arisen by applications’ rapid development and complexity, as well to address the scalability shortcomings of CGI technology. To meet those requirements Open Market introduced FastCGI – a high performance version of the CGI technology with enhanced capabilities.
We distinguished some differences between the two web technologies, to describe FastCGI features in details.
CGI CGI runs an application which works with the requested script for every HTTP request. After the application finishes processing and returns the output data, CGI terminates the application process. Within the next request CGI runs the application. Every next start might use more time and resources than generating the output data itself. The more processes are running, the more resources such as RAM or computing power will be exploited. The webpage loading time is now defined not only by the server load, but also from application load time. | FastCGI What makes a difference from CGI is that with FastCGI the running process of the application lasts longer and it is not immediately terminated. After the application finishes processing and returns the output data, the process is not terminated and is being used for processing further requests. This results in reduced server load and less page loading time. Another difference is that FastCGI can be used on a remote server, a different one from the web server. The CGI applications main function is to process the HTTP request data and to return a HTTP response. This is the so called “Responder” role in FastCGI. Besides, an application can also perform the roles of Authorizer and Filter. |
PHP and Apache HTTP Server
There are two common ways in which an Apache web server can handle PHP.
1. By using CGI or FastCGI the server runs an executable binary that is the PHP interpreter. This is an isolated process, performed outside the web server’s process.
After changes in the PHP settings (in the php.ini file), a PHP process alone can be restarted without this influencing the web server.
2. By using PHP as a module on the Apache server (mod_php), the PHP interpreter is in the web server code. The PHP process is part of the web server process.
If there are any changes in PHP settings, the web server needs to be restarted so that they take effect. PHP directives (php_flag, php_value…) can be placed in the server’s .htaccess file.