avatar

Andres Jaimes

How to setup a FastCGI development environment in CentOS 6

By Andres Jaimes

Install the development environment

Install the C/C++ development tools to your server:

yum install gcc gcc-c++ autoconf automake

Now, before installing fcgi we need to add the epel repository:

rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-7.noarch.rpm

Once it is installed we can proceed to get the fcgi packages:

yum install fcgi-devel spawn-fcgi

Let’s see if we can compile a simple file. Create a file named example.c and copy and paste the following code in it:

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

int main(int argc, char **argv) {
    int count = 0;
    while(FCGI_Accept() >= 0)
        printf("Content-type: text/htmlnn"
               "<html><head><title>FastCGI Hello!</title></head>n"
               "<body><h1>FastCGI Hello!</h1>n"
               "Request number %d running on host <i>%s</i></body></html>n",
               ++count, getenv("SERVER_NAME"));
    return 0;
}

Build it:

gcc -o example example.c -lfcgi

Test it:

./example

Execute the spawn fcgi program so our example is available to respond to connections

spawn-fcgi -p 3000 -f /path/to/example

You can check it is running by using the ps command. To kill it you have to type:

kill process-number

It is important to notice that “example” does not need to be inside the web directory. It can be anywhere on your system. All requests will be dispatched by tcp.

 

Install a web server and configure fcgi

Install nginx

yum install nginx

Edit the configuration file

vi /etc/nginx/conf.d/default.conf

Add the following lines to it

location ~ /fcgi/$ {
    include fastcgi_params;
    fastcgi_pass 127.0.0.1:3000;
}

The “location ~ /fcgi/$” line instruct nginx to forward all requests that include the string “/fcgi/” to 127.0.0.1 port 3000. That is the port our application is listening to.

Start the web server

/etc/init.d/nginx start

We can now test our example application from a web browser. Simply go to http://localhost/fcgi/

We don’t need to create any fcgi directory or file inside our web directory. All requests will be dispatched to our fcgi program by tcp.