PoiNtEr->: Hello World To CGI in Linux(Ubuntu) using C program

                             Difference between a dream and an aim. A dream requires soundless sleep, whereas an aim requires sleepless efforts.

Search This Blog

Sunday, January 22, 2012

Hello World To CGI in Linux(Ubuntu) using C program


Configure Your Apache2 For CGI
First question that comes to Our mind is "what is CGI??"
Ans:The Common Gateway Interface (CGI) is a standard  method for web server software to delegate the generation of web pages to executable files. Such files are known as CGI scripts; they are programs, often stand-alone applications, usually written in a scripting language.
A web server that supports CGI can be configured to interpret a URL that it serves as a reference to CGI scripts. A common convention is to have a cgi-bin/ directory at the base of the directory tree and treat all executable files within it as CGI scripts. Another popular convention is to use filename extensions; for instance, if CGI scripts are consistently given the extension .cgi, the web server can be configured to interpret all such files as CGI scripts


I think thats enough of definition now come to implementation part.

So First we need to configure apache to make CGI works...So here it goes


Create a /var/www/cgi-bin/:

Code:
mkdir -p /var/www/cgi-bin/
Open httpd.conf

Code:
vi httpd.conf
Append following code:

Code:
<IfModule mod_alias.c>
ScriptAlias /cgi-bin/ /var/www/cgi-bin/

<Directory /var/www/cgi-bin/>
AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
</IfModule>

Restart apache.

Now we make a simple C hello world program and put the compiled binary of that program in our cgi-bin folder.


#include<stdio.h>
main()
{
printf("Content-type:text/html\n\n");
printf("<h2>Hello World!!</h2>");
}


compile above program {let program name is cgi.c}

gcc -o cgi cgi.c


Now copy the cgi(output binary of above program) file to /var/www/cgi-bin folder 


Now Open Up your Browser and put url in address bar and thats it ...




No comments:

Post a Comment