FCGI Wrap

So, you want to use CGI. OK! Let’s make this as simple as possible for you.

Install on Debian or Ubuntu

There are packages for Debian and Ubuntu now. Simply type

aptitude install fcgiwrap

Then, have a look at /usr/share/doc/fcgiwrap/README.Debian. There’s an example configuration at /usr/share/doc/fcgiwrap/examples/nginx.conf.

What I did was creating a local copy of the example configuration (so it does not get overwritten upon updates)

cp /usr/share/doc/fcgiwrap/examples/nginx.conf /etc/nginx/fcgiwrap.conf

and add it to the the site’s configuration in the “server” section with

# fast cgi support
include /etc/nginx/fcgiwrap.conf;

After installing the package, also have a look at the ngx_http_fastcgi_module documentation or the FastCGI Example.

Manual Install

The first step here is to install this stuff.

If you’re on an apt based system:

aptitude install git-core build-essential libfcgi-dev autoconf libtool automake

Get the source:

cd /usr/local/src/
git clone git://github.com/gnosek/fcgiwrap.git

Compile this:

cd /usr/local/src/fcgiwrap
autoreconf -i
./configure
make
mv fcgiwrap /usr/local/bin/

Setup Scripts

  • /etc/init.d/fcgiwrap
    #!/usr/bin/perl
    
    use strict;
    use warnings FATAL => qw( all );
    
    use IO::Socket::UNIX;
    
    my $bin_path = '/usr/local/bin/fcgiwrap';
    my $socket_path = $ARGV[0] || '/tmp/cgi.sock';
    my $num_children = $ARGV[1] || 1;
    
    close STDIN;
    
    unlink $socket_path;
    my $socket = IO::Socket::UNIX->new(
        Local => $socket_path,
        Listen => 100,
    );
    
    die "Cannot create socket at $socket_path: $!\n" unless $socket;
    
    for (1 .. $num_children) {
        my $pid = fork;
        die "Cannot fork: $!" unless defined $pid;
        next if $pid;
    
        exec $bin_path;
        die "Failed to exec $bin_path: $!\n";
    }
    

    Don’t forget chmod +x /etc/init.d/fcgiwrap.

  • /etc/rc.local

    I decided not to try to make an overly complicated init script and sit with the simple one. I just added sudo -u www-data /etc/init.d/fcgiwrap to /etc/rc.local before the exit 0 line.

What Happens

The sudo command will launch the fcgiwrapper init script as the www-data user. The script binds a listener thread to /tmp/cgi.sock. This is what you need to use in fastcgi_pass: fastcgi_pass unix:/tmp/cgi.sock;