program takes 1 second to compile and 2 seconds to connect to a database, you’ll still have significant savings over forking Perl and recompiling the program with each request. cgi-fcgi is a tiny program when
compared to Perl and many other programs.
Building the FastCGI Developer’s Kit
To build the developer’s kit, you’ll need to configure it. The folks at Open Market have provided a nice script that automates the configuration process to run it. Just type ./configure while focused inside the
fcgi-devel-kit directory. After a few moments you should be able to type make and have the libraries built. You will need this kit to build FastCGI-savvy interpreters or C programs.
After you build the kit, you may be interested in installing the library libfcgi/libfcgi.a to some place useful such as /usr/local/lib. Remember to do a ranlib on the library after you move it to refresh it. While you’re
at it, you may want to copy the include directory to /usr/local/include/fcgi. That way it will be easier for you to reference it while building your own programs.
After the kit is built, you may want to try your luck at building the sample HelloWorld.c program listed earlier. Note that you may need to change the location of the fcgi_stdio.h header file to reflect its new
location.
To build the FastCGI program, issue the following commands:
cc -o Hello.fcgi -lfcgi HelloWorld.c
Put the resulting Hello.fcgi on your fcgi-bin directory. Before you can access it, you’ll need to add an AppClass entry into your srm.conf:
AppClass /usr/local/etc/httpd/fcgi-bin/Hello.fcgi
and restart the server with
% kill -HUP `cat /usr/local/etc/httpd/logs/httpd.pid`
The AppClass directive takes care of starting and maintaining the FastCGI application. At this point you should be able to access it by pointing your browser to http://localhost/fcgi-bin/Hello.fcgi.
You should get a similar result to those displayed in Figure C.2.
Figure C.2. The FastCGI version of Hello World!. Notice that it keeps state. My version is fancier than the HelloWorld listing.
Making Perl Fly
You will notice an even bigger improvement on Perl CGIs. This is because FastCGI will keep the Perl program running; the interpreter won’t have to fork, exec, compile, and execute for each request. A
good thing.
Before you can incorporate FastCGI into your Perl programs, you have to build a special version of Perl. The FastCGI Developer’s Kit contains the patches you’ll need to build a version of Perl that supports
FastCGI. After you build this version, there’s no need to keep your old Perl around. A FastCGI-savvy Perl binary will work just fine with regular Perl scripts.
Future versions of Perl may have support for FastCGI right out of the box. Currently there’s an active discussion on the perl5-porters mailing list regarding the issues that need to change in Perl’s
implementation to support FastCGI as a true Perl module?that is, requiring no recompiling. The Tcl7.5 FastCGI module, when it makes its debut, won’t require a rebuilt of Tcl. The new Python module also
doesn’t need a rebuild.
At the time of this writing, the patches available for FastCGI were for version 5.002 of Perl. By the time you read this, they will be updated to the current Perl version, 5.003.
The patch process is simple; you just replace a few files in the standard Perl distribution with files provided in the kit. Here’s the process:
1.1. Put the unarchived Perl and fcgi-devel-kit on a directory side by side and issue the following commands:
% cd perl5.002
% mv perl.c perl.c.dist
% mv proto.h proto.h.dist
% mv Configure Configure.dist
% cp -r ../fcgi-devel-kit/perl-5/perl5.002/*
% cp -r ../fcgi-devel-kit/perl-5/common/*
2.2. Set the environment variable FCGIDIR to the absolute path of the fcgi-devel-kit. In my case, the distribution was in the /tmp/fcgiPerl directory. This variable will tell the configuration program where
to find things:
setenv FCGIDIR /tmp/fcgiPerl/fcgi-devel-kit
3.3. If you don’t use gcc, set the environment variable CC to the name of your compiler:
setenv CC cc
4.4. If you want to install the Perl distribution somewhere other than /usr/local/bin, define the environment variable PERL_PREFIX. I kept the default setting.
5.5. Execute the fcgi-configure script:
% ./fcgi-configure
The fcgi-configure script is a wrapper that automatically sets some of the configure variables without requiring user input. If this fails, you’ll have to run the Configure command in the Perl directory. You
may want to take a look at the documentation that came with the FastCGI Developer’s Kit for any tips to solve the problem.
6.6. Do a make to build the software:
% make
If it all goes smoothly, you can finish the installation with a make install, which will install Perl to the location specified. That’s it for the install! Make sure your scripts reference the correct version of Perl.
Perl FastCGI Programs
The modified version of my HelloWorld.c looks like this:
#!/usr/local/bin/perl
use strict;
use FCGI;
my($timesVisited) = 0;
while(FCGI::accept() *=0){
print “Content-type: text/plain\n\n”;
print **STOP;
*HTML*
*HEAD*
*TITLE*Hello World!*/TITLE*
*/HEAD*
*BODY*
*H1*Hello, this is a FastCGI program!*/H1*
*P*I can tell that you are visiting from $ENV{REMOTE_HOST}*/P*
*P*This page has been accessed: ++$timesVisited*/P*
*/BODY*
*/HTML*
STOP
}
As you can see, this is pretty much the same organization as the C program. The one gotcha with Perl is that if the initial environment to a FastCGI Perl application is empty when the first call to FCGI::accept
returns, the environment will still be empty. The easiest workaround is to add an environment with the AppClass -initial-env directive. See the section entitled, “The AppClass Directive,” for more detailed
information.
The mod_fastcgi Module
The FastCGI module provides Apache compatibility to FastCGI applications. This module is not part of the standard Apache release, so you’ll have obtain a copy from http://www.fastcgi.com. A copy of the
latest version at the time this was written, August 1996, is included on the CD-ROM.
This module processes any file with a MIME-type application/x-httpd-fcgi. Because the ScriptAlias directive may have higher priority over AddType, FastCGI applications should not reside on the cgi-bin
directory; if they do, they may be processed by the mod_cgi module regardless of the extension given. This means that the application/x-httpd-fcgi MIME type is given to files that do not reside in the
ScriptAlias directory and that have a name using the extension defined by the AddType application/x-httpd-cfgi directive. Typically this will be .fcgi. The reason for this is that Apache assigns priority to the
directives based on the order of compilation in the modules.
The AppClass Directive
Syntax:
AppClass executablePath [-processes p] [-listen-queue-Depth q] [-restart-delay secs] [-priority N] [-initial-env key=value]
Default:
AppClass executablePath [-processes 1] [-listen-queue-Depth 5] [-restart-delay 5] [-priority sameAsHTTP]
Context:
srm.conf
Module:
mod_fastcgi
The AppClass directive, provided by mod_fastcgi, is responsible for starting up one or more FastCGI application processes using the executable specified by executablePath.
When a request for the file specified by executablePath is received, the request is handled by the mod_fastcgi module, which connects the request to the appropriate process belonging to the proper
AppClass.
In addition to starting the process, the server will ensure that a process for handling a particular AppClass will be available. Should a process exit because of an error or some other condition, the server will
launch another process capable of handling the requests.
AppClass has several other options:
processes:
Specifies the number of FastCGI processes that the server will spawn. Default value for this setting is 1.
listen-queue-depth:
Sets the depth of the listen queue that is shared by the processes. The listen queue stores additional requests that may be received while the
FastCGI application is processing another. Requests will queue until they reach the limit imposed by listen-queue-depth. Additional requests
beyond the size of the queue are responded with an error to the client. Default value is 5.
restart-delay:
Specifies the number of seconds the server will wait before restarting a dead FastCGI process. The server won’t restart a process more often
than the time specified by this flag. The default value is 5 seconds.
priority:
This flag specifies the execution priority of the FastCGI process. The default priority is inherited from the parent httpd server process.
initial-env:
This flag allows you to specify the initial environment sent to a FastCGI program when the program initializes. You can specify multiple
initial-env flags, one per key=value pair. If not specified, the initial environment is empty. It would be very useful to provide the FastCGI
application with information during its initialization phase.
Example
If you wanted to start the HelloWorld.fcgi program, you would need to type this:
AppClass /usr/local/etc/httpd/fcgi-bin/HelloWorld.fcgi -processes 2
For a Perl program, to circumvent the environment problem, you would have to do this:
AppClass /usr/local/etc/httpd/fcgi-bin/HelloWorld.fcgi -processes 2 -initial- env: DB_PATH_NAME=/proj/accts/db2
Summary
Although FastCGI is not the sole alternative for high performance CGI alternatives, the features and price cannot be beat. It’s especially interesting that existing code can be ported easily without a real learning
curve. This alone makes it very attractive for programmers who have a backlog and don’t have much time to spend experimenting with new tools, yet need to implement a high-performance CGI solution.
FastCGI is a great choice?the learning-and-setup curve is hours, not days like other environments. Performance Enhancement
FastCGI API
What a FastCGI Application Looks Like
FastCGI and C
FastCGI with Perl and Tcl
Installing and Configuring FastCGI
Incorporating FastCGI Functionality into Apache
cgi-fcgi
Building the FastCGI Developer’s Kit
Making Perl Fly
Perl FastCGI Programs