Apache + FastCGI

Tue, 29 Sep 2009 , modified Wed, 30 Sep 2009

Here is a sample Apache + FastCGI configuration. I have it working on my local machine, so all the configs are ArchLinux compatible :)

Configuring mod_fastcgi

    LoadModule fastcgi_module modules/mod_fastcgi.so
    <IfModule mod_fastcgi.c>
        # Make sure it points to the right suexec
        FastCgiWrapper /usr/sbin/suexec
        # Make sure it is writable by the server
        FastCgiIpcDir /var/lib/http/fastcgi

        AddHandler fastcgi-script .fcgi
    </IfModule>

Configuring virtual host

    <VirtualHost *:80>
        ServerName bootylicious.local
        ServerAdmin root@localhost

        SuexecUserGroup vti vti 

        DocumentRoot "/srv/http/vhosts/bootylicious/htdocs/"

        ScriptAlias / "/srv/http/vhosts/bootylicious/cgi-bin/bootylicious.fcgi/"

        ErrorLog "/var/log/httpd/error_log"
        CustomLog "/var/log/httpd/access_log" common

        <Directory "/srv/http/vhosts/bootylicious/htdocs/">
            Options Indexes FollowSymLinks

            AllowOverride All 

            Order allow,deny
            Allow from all 
        </Directory>

        <Directory "/srv/http/vhosts/bootylicious/cgi-bin/">
            AllowOverride None
            Options ExecCGI None FollowSymLinks
            Order allow,deny
            Allow from all 
        </Directory>
    </VirtualHost>

Testing FastCGI

To make sure FastCGI is working place this test script into your cgi-bin/ directory and point your browser to it.

Don't forget to change ScriptAlias temporarily to use this script:

    ScriptAlias / "/srv/http/vhosts/bootylicious/cgi-bin/test.fcgi/"

The counter should increment with every request:

    #!/usr/bin/perl

    use strict;
    use warnings;

    use FCGI;

    our $count = 0;

    while (FCGI::accept() >= 0) {
        print(
            "Content-type: text/html\r\n\r\n",
            "<title>FastCGI Hello! (Perl)</title>\n",
            "<h1>FastCGI Hello! (Perl)</h1>\n",
            "Request number ",
            ++$count,
            " running on host <i>$ENV{SERVER_NAME}</i>"
        );  
    }

Configuring Bootylicious

    "server" : "fastcgi"

This will use embedded Mojo::Server::FastCGI pure perl implementation.

If you've installed Mojo::Server::FCGI change the configuration to this:

    "server" : "fcgi"