Tag/tag.png

Needs Updating
This article needs updating to include the latest versions of Ubuntu. More info...

Introduction

fcgiwrap is a simple server for running CGI applications over FastCGI. It hopes to provide clean CGI support to Nginx (and other web servers that may need it).

fcgiwrap can be used together with Nginx to serve CGI or Perl scripts (.cgi).

Installation

Install the fcgiwrap package from the Universe repository.

Configuration

To be able to spawn a fcgi socket, we are going to write a script for it and than add a init script to /etc/init.d/ to automatize this process.

Here is the spawn-fcgi script which will create a socket to pass .cgi to it:

cat > /usr/bin/spawn-fcgi
#!/usr/bin/perl

use strict;
use warnings FATAL => qw( all );

use IO::Socket::UNIX;

my $bin_path = '/usr/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 to make it executable:

chmod +x /usr/bin/spawn-fcgi

The above script will be used to automatize the re-spawning of the FastCGI socket:

cat > /etc/init.d/spawn-fcgi
#!/bin/bash
C_SCRIPT=/usr/bin/spawn-fcgi
USER=www-data
GROUP=www-data
RETVAL=0
case "$1" in
        start)
                echo "Starting fastcgi"
                sudo -u $USER $C_SCRIPT
                chown $USER:$GROUP /tmp/cgi.sock
                RETVAL=$?
  ;;
        stop)
                echo "Stopping fastcgi"
                killall -9 fcgiwrap
                RETVAL=$?
  ;;
        restart)
                echo "Restarting fastcgi"
                killall -9 fcgiwrap
                $sudo -u $USER $C_SCRIPT
                RETVAL=$?
  ;;
        *)
                echo "Usage: $0 {start|stop|restart}"
                exit 1
  ;;
esac
exit $RETVAL

Set the permissions to make the script executable and install the init script:

chmod +x /etc/init.d/spawn-fcgi
update-rc.d spawn-fcgi defaults

Now we can start and stop the script using invoke-rc.d:

invoke-rc.d spawn-fcgi start
invoke-rc.d spawn-fcgi stop

After you start the spawn-fcgi you should see a new file in /tmp folder with the name cgi.sock. That file is our socket to pass .cgi scripts to.

Configuring nGinx

Create the configuration file for nGinx to use fastCGI:

cat > /etc/nginx/fastcgi_params
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;
fastcgi_param  REMOTE_USER        $remote_user;

The created above file now can be added to nGinx vhosts to allow them using fcgiwrap. Here's an example of such a vhost file configured to work for Namazu:

server {
        listen   80;
        server_name  localhost;
        access_log  /var/log/nginx/access.log;

        location / {
                root   /storage/archive;
                autoindex on;
                index  index.html index.htm;
        }

        location ~ \.cgi$ {
                root    /storage/archive/cgi-bin;
                rewrite ^/cgi-bin/(.*)\.cgi /$1.cgi break;
 
                include /etc/nginx/fastcgi_params;
 
                fastcgi_pass   unix:/tmp/cgi.sock;
                fastcgi_param  SCRIPT_FILENAME  /storage/archive/cgi-bin$fastcgi_script_name;
        }

}

See Also

FcgiWrap (last edited 2017-09-04 19:05:53 by ckimes)