The Passionate Craftsman

Ruby, PHP, MySql, Software engineering and more.

Friday, 3 February 2012

CGI script with Apache and C programming language

Dynamic web pages where born in 1993 when CGI was introduced. The most common language used was Perl, but C was used too. Today I just tried to write a CGI script for the first time in my life and I did it with C, like a real man. Find your CGI path, mine is:

$ sudo cat /etc/apache2/httpd.conf | grep ScriptAlias
ScriptAliasMatch ^/cgi-bin/((?!(?i:webobjects)).*$) "/Library/WebServer/CGI-Executables/$1"

So in my Mac I can put my CGI scripts here: /Library/WebServer/CGI-Executables/.

I created a file, test.c with this content:


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

time_t now;
struct tm *d;
char li[13];
d = localtime(&now);
strftime(li, 15, "%d/%m/%Y", d);
printf("Today is %s\n", li);

return 0;

I compiled the file and made it executable:

cc testc.c -o testc && chmod +x testc

And is saw the outcome here http://localhost/cgi-bin/testc.

This is the output:

Hello world

The C programming language

Today is 03/02/2012

CGI is very simple, you just need to print the content type with newline and carriage return, then you can output your dynamic HTML content.

Today FastCGI has replaced CGI and it is very popular and probably it is the best way to create dynamic web pages with the most performance possible: C + FastCGI. Another option, more sophisticated and with different way of use it is uWSGI, created by the Italian hosting company Yesterday I read an article about the usage of C++ as programming language for Facebook, in 2009 they could shutdown 22500 servers if they where using C++ instead of PHP. We all know that Facebook has created a PHP compiler to compile PHP in C++: HipHop

P.S.: charset shout be UTF-8 :-)

Labels: , ,


At 1 February 2013 at 02:01 , Blogger shipra sagar said...

Hello all,I am new and I would like to ask that what are the benefits of sql training,What all topics should be covered in it?
And has anyone studied from this course of C tutorial online?? or tell me any other guidance...
would really appreciate help

At 1 February 2013 at 02:17 , Blogger Riccardo Tacconi said...

Hi, I prefer this course: (free). The author has a video course too (non-free).

At 1 April 2017 at 23:06 , Blogger Arun Kumar said...

@shipra Here is a very nice c programming tutorial


Post a Comment

Subscribe to Post Comments [Atom]

<< Home