Posted on 14-11-2012
Filed Under (Technology) by bigbang

There are a lot of advantages to use FastCGI as web server solution, and there is also an easy way to construct such kind of server by using Apache and mod_fcgi module.

Let’s use Ubuntu 11.04 and Apache2.2 to develop a FastCGI Web Server.

1. Configure Apache and mod_fcgid module

First, install Apache from command line under Ubuntu:

$ sudo apt-get install apache2

Then we can also install mod_fcgid module in the same way:

$ sudo apt-get install libfcgi-dev

After the installation of Apache and mod_fcgid module, then we can edit configure file


to modify options about mod_fcgid. BTW: according to Apache edition and installation path, the Apache configure file is under different path, so please check Apache manual for the details.

The example of configuration is:

<Directory /var/www/>
SetHandler fcgid-script
Options +ExecCGI

# Customize the next two directives for your requirements.
Order allow,deny
Allow from all

The above configuration is trying to put FastCGI binary code under path /var/www/ . Or you can choose another path in which Apache can execute the binary code.

2. Install fcig development package

Under Linux operating system, we need to install libfcgi library to use C, C++, Java, Perl to develop FastCGI program. The fcgi library encapsulate FastCGI protocol, so we don’t need to pay attentation to FastCGI protocol details. We can use following command to install it:

$ sudo apt-get install libfcgi-dev

Then create a .c source code file in your work folder, input following C code and compile it:

#include "fcgi_stdio.h"
#include <stdlib.h>

void main(void)
    int count = 0;
    while(FCGI_Accept() >= 0)
        printf("Content-type: text/html\r\n"
               "<title>FastCGI Hello!</title>"
               "<h1>FastCGI Hello!</h1>"
               "Request number %d running on host <i>%s</i>\n",
                ++count, getenv("SERVER_NAME"));

The command to compile the example code is:

$ gcc tiny-fcgi.c -o tiny-fcgi -lfcgi

The binary file of FastCGI program can execute directly (yes, trust me, it can run as normally program as well) and output related result.

Then copy the compiled binary file to Apache FastCGI path.

$ cp tiny-fcgi /var/www/

3. Ping your FastCGI services

Restart Apache service:

$ sudo /usr/sbin/apachectl restart

Then input following address in your favorite web browser, and you can reach your FastCGI service:

4. Ending

The configuration of FastCGI is not complex and I will introduce some advanced techniques of FastCGI later. FastCGI is some kind of old things in IT world, and I think it is suitable for distributed computation using C as computing engine.

The separation of web server and computing engine is good for web developers and computing program developers to work seperatly and enjoy their work alone.

© 2012, The Big Bang on Reading Busy. All rights reserved.
On republishing this post you must provide link to the original post.

Post a Comment