Running cgi scripts on server


Hosting cgi-bin directory exist for sumthing, and its for running cgi scripts! :D

How? well, this is for my own benefit, and if this helps you guys out there, good. First, a perl script, just create a simple hello world as below, and save it as

# -- my first perl script!

print "Content-type: text/html\n\n";
print "Hello, world!\n";

Ok now, some explanations:

#!/usr/bin/perl <-- This line is th full path of the binary to run the script, in this case, is perl, and my server full path is /usr/bin/perl, but this may vary for different servers, some may have /usr/locl/bin/perl or perhaps /sbin/perl.. im not sure myself. haha OUH, IMPORTANT! for linux server, just CHMOD it to 700 cause in my case, if i dont do that, my /var/log/httpd/domains/ will spit out this error:

[Sun Mar 21 04:11:59 2010] [error] [client] suexec policy violation: see suexec log for more details
[Sun Mar 21 04:11:59 2010] [error] [client] Premature end of script headers: test.cgi

Then, next, upload it to your hosting cgi-bin/ folder, and you can access it trough web url such as mine: but, in some servers, they may have to accessed thru sumthing like these: well, in my case, perhaps sumthing like Erm, but of course depends on server configurations.

Done. Good. Now another example, now in C. Say we have some codes in C language such as below:

int main(void) {
printf("Content-Type: text/plain;charset=us-ascii\n\n");
printf("Hello world\n\n");
return 0;

So, how do i use this? well, to use it, i have to ompile it first, so i ompiled it to test_c.cgi:

[adam@ns52 cgi-bin]$ /usr/bin/gcc test_c.c -o test_c.cgi
[adam@ns52 cgi-bin]$ ./test_c.cgi
Content-Type: text/plain;charset=us-ascii

Hello world

adam@ns52 cgi-bin]$

Ok. done. now just move that compiled binary to cgi-bin/ folder, chmod it to 700 and can access it thru web: http:/ and TADAA~ :D

Hm.. some useful usage, you can pass variables to the cgi scripts so it can be computed/procesed and the cgi script will vomit out the result to http page. Mire and more example and detailed explanation here :

Kudos Google! :D



Popular posts from this blog

Check SecStore KeyPhrase of Java Stack

Windows 2003 W2k3 Getting Current MTU Size

Python: thread.error: can't start new thread