We help IT Professionals succeed at work.

Linux cgi-bin

I'm trying to become familiar with cgi-bin web servers on Linux.  I know that the cgi binary generally is compiled using g++. In very basic terms, can someone explain the "basics"?

1.  How does an html file call the cgi binary?
2.  What is the basic structure of the cgi code?  Java servlets use send/receive responses/requests?  Does cgi do something similar?
3.  Why is it popular among webhosts?
Watch Question

Top Expert 2007
Firstly, you need to understand CGI is simply a set of rules for the webserver to pass data to external programs.

CGI programs can be written in any language you like, eg: Perl, Python, C, C++, Shell etc.  The most common language used for CGI programs is Perl.

1.  You invoke a CGI progam by either specifying the URL directly, eg: http://example.com/cgi-bin/prog.cgi or by using a HTML form that invokes the CGI program.

2.  There's no particular structure as CGI isn't a programming language.  The structure of the program will vary depending on what language you use.

3.  CGI goes back to the very early days of the web when it was the only way of running external programs.  These days, the use of CGI is waning due to various other technologies such as PHP, Java, Flash etc.  That said, there are still a lot of sites that heavily use CGI.