1
votes

From the client browser to the Apache httpd webserver there is a clear defined protocol, HTTP, via TCP/IP (sockets). I'm having hard time to understand what channel/protocol is used from the Apache httpd webserver to Perl or PHP or CGI? Is it Inter-Process Communication via sockets or pipse or message queue or signals? Could someone shed light on what is really going on behind the scene? Is it as if the Apache httpd webserver executes another program (Perl or PHP) and captures the output and then resends it to client browser?

From Linux Server Security:

The CGI protocol doesn't specify how the web server should communicate with the CGI program. There have been two main solutions:

Standalone CGI programs

Apache receives a CGI request, opens a two-way pipe to an external program, sends it the CGI input data, and returns the program's output to the client. As a separate process, the program can crash without bringing down the web server. The down side is that it's relatively slow to start a new process.

Built-in CGI programs

The program is rewritten as an Apache module and incurs its startup cost only when an Apache process starts. This is much faster than an external program and has access to Apache's internals and other modules. The most popular modules for CGI in Apache are the interpreter engines for Perl (mod_perl) and PHP (mod_php).

1
probably belongs on server fault... not a programming questionJeffrey Kevin Pry
@Jeffrey, I don't agree, this question is about the basics of CGI programming.Fozi
but it he is asking about how the process works not how to implement it using code...Jeffrey Kevin Pry
@Jeffrey and how is he supposed to start coding if he does not understand the underlying process? Why would he need to know anything about this process if all he wanted is to use a CGI that he downloaded somewhere?Fozi
i didn't argue about his question, i think he should post this on server fault and then subsequent questions about implementation should be posted here.Jeffrey Kevin Pry

1 Answers

6
votes

Communication with CGI programs is done via two channels: simple IO redirection (that is, STDIN and STDOUT) and environment variables.

The HTTP server sends the request to the CGI's STDIN. It reads the HTTP response from the CGI's STDOUT and sends it (slightly modified) to the client browser. Additional server data (like the request size or some server parameters) are set to environment parameters that usually start with HTTP_.