Running cgi scripts on server

Hi,

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 test.pl

#!/usr/bin/perl
# test.pl -- 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/adam.ns52.small-dns.net.error.log will spit out this error:

[Sun Mar 21 04:11:59 2010] [error] [client 121.121.147.128] suexec policy violation: see suexec log for more details
[Sun Mar 21 04:11:59 2010] [error] [client 121.121.147.128] 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: http://adam.ns52.small-dns.net/cgi-bin/test.pl but, in some servers, they may have to accessed thru sumthing like these: http://domain.com/~username/cgi-bin/test.pl well, in my case, perhaps sumthing like http://adam.ns52.small-dns.net/~adam/test.pl 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:

#include
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:/adam.ns62.small-dns.net/cgi-bin/test_c.cgi 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 : http://www.cs.tut.fi/~jkorpela/forms/cgic.html#hello

Kudos Google! :D

//alak

Comments

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