Appendix c
Appendix c
* FastCGI Features
* 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
* The mod_fastcgi Module
+ The AppClass Directive
+ Example
* Summary
_________________________________________________________________
FastCGI
FastCGI Features
Performance Enhancement
Unlike CGIs, which are forked every time there's a request for the
functionality, FastCGI processes are reused. After a request is
fulfilled, the process remains, waiting for additional requests.
FastCGI API
Data is read and written through the standard input, output, and error
streams. FastCGI also provides macros that map files and streams to
native calls supported by your operating system.
Like CGI, you can create FastCGI applications in almost any language.
However, you are currently limited to the ports of the library.
Currently available are Perl, C/C++, Tcl, Java, and very soon Python,
which should be available by the time you read t his. Also in the
works is a multithreaded C/C++ library that has not been released, but
should be available as part of the 1.6 release. The multithreaded
library allows a single application process to handle concurrent
requests, which will allow you to im plement things like HTTP-based
chat applications.
The design of FastCGI also wins big on the learning curve because
unlike server APIs, you are still programming a CGI, so you can
leverage what you already know. The only issues that you will need to
address have to do with reorganizing your applicatio n so that the
initialization code, which is done once, is kept separate from the
application body. FastCGI applications are long-lived; they are kept
alive between transactions. This also means that memory needs to be
managed because unlike CGIs, which ha ve a short life span, FastCGI
processes may execute for undetermined amounts of time.
#include "fcgi_stdio.h"
#include <stdio.h>
{
int timesVisited = 0;
while(FCGI_Accept() >=0)
printf("<HTML>");
printf("<HEAD><TITLE>Hello World!</TITLE></HEAD>");
printf("</HTML>");
This version makes use of the fact that the application is persistent
and will maintain a count of the times the program is run (until the
program dies).
As you can see from this example, FastCGI applications follow this
sequence:
1. Initialization. Persistent connections or data that should be
available from request to request are initialized in this section.
Initialization is done only once. The initial environment for
FastCGI applications is set through the AppClass directive, which
is added by the FastCGI module.
2. The Response Loop. This loop is started by the FCGI_Accept()
routine, implemented in the FastCGI library. A call to this
routine blocks program execution until the program receives a
client request. When it receives one, the routine unblocks, sets
up an environment for the program, and runs one loop through the
body. This routine is also responsible for determining the context
under which the program is running (FastCGI or CGI) and sets the
environment accordingly.
3. Body. This portion of the program gets executed by each request.
This is the meat of your program. Each request will have its own
environment variables, just as in a regular CGI.
4. End of Response. A subsequent call to FCGI_Accept informs the
server that the program has completed a request and is ready for
another. FCGI_Accept once more blocks execution until the next
request.
You can run Perl- and Tcl-interpreted programs under FastCGI. In order
to run them, you'll have to compile a specially modified version of
the interpreter that has been modified to work with FastCGI. You will
not, however, need to maintain both a regul ar and a special version
of the interpreter. The special version will work as expected when
used under a normal context. Future versions of Perl and Tcl will
incorporate changes that will make it possible to use unmodified
versions of the program with Fas tCGI.
Patches for both Perl and Tcl are available. Some prebuilt binaries
are also available at http://www.fastcgi.com.
Both the toolkit and the Apache module are included in the CD-ROM that
accompanies this book; however, you should make sure that they are
still current. At the time of this writing, the Apache module was
still based on a beta version of 1.1. The latest and greatest versions
of the module and developer's kit are available at the FastCGI Web
sites: http://www.fastcgi.com/servers and
http://www.fastcgi.com/applibs, respectively. The version of the
toolkit included on the CD-ROM is 1.5.
# AppClass
% mkdir fcgi-bin
% cd /usr/local/etc/httpd
% mv httpd httpd.prev
% mv src/httpd .
% strip httpd
This will restart the server and force rereading of the new
configuration directives you just added. Watch for any error messages.
If you get an error, more than likely one of the directives you added
is incorrect.
cgi-fcgi
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 mom ents 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.
AppClass /usr/local/etc/httpd/fcgi-bin/Hello.fcgi
Figure C.2. The FastCGI version of Hello World!. Notice that it keeps
state. My version is fancier than the HelloWorld listing.
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 modu le-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 documentatio n 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.
#!/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>
</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 em pty. 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.
Example
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 attra ctive 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.