Quick Intro to FastCGI Setup

FastCGI is an extension to the well known Common Gateway Interface (CGI). CGI can be very slow because it starts a new process for every incoming request. FastCGI keeps the process in memory and uses it to handle many requests.

I needed to write a simple Python FastCGI script which would be hosted on Apache2. The module for Apache is mod_fastcgi. There are a few dependencies for the fastcgi module, but I'm using gentoo; so, I just needed to emerge mod_fastcgi. The mistake I made the first time was not including apache2 in my use variable. If you don't do that, you'll end up getting apache1.3 installed.

And that's it. All you have to do now is make a few configuration changes. Take a look /etc/apache2/httpd.conf. You need to add the module. I also commented out both cgi modules to be sure I'm not using them.

#
# CGI Modules
#
# These modules provide the ability to execute CGI Scripts.
#
#LoadModule cgi_module                    modules/mod_cgi.so
#LoadModule cgid_module                   modules/mod_cgid.so
########################### mine #######################
LoadModule fastcgi_module                modules/mod_fastcgi.so
########################################################

The first line tells Apache to check the time on the script and reload it if it has changed. You should only use this in development mode because it will slow things down. I'm not going to discuss everything under directory because I'm not an Apache expert. The * in the path tells it to look at all directories under home. The full directory of my scripts would be /home/goldfita/www/fcgi-bin. And the script name itself should end in .fcgi. The url to access the script is http://127.0.0.1/~goldfita/fcgi-bin/script.fcgi.

######################## mine #############
FastCgiConfig -autoUpdate
<IfModule mod_fastcgi.c>
  <Directory "/home/*/www/fcgi-bin/">
    AllowOverride None
    Options ExecCGI
    SetHandler fastcgi-script
    AddHandler fastcgi-script .fcgi
    Order allow,deny
    Allow from all
  </Directory>
</IfModule>
######################## mine #############

You might have noticed the www. By default this is public_html, but that's too long for me to type. If you want to change it too, you'll also have to modify it in UserDir and under directory in mod_userdir.c.

Add -D USERDIR to APACHE2_OPTS in /etc/conf.d/apache2. Download fcgi.py and put it in your Python lib directory. Note that there is an error in the source. The function request.out.write takes one argument, not two. There is a example as well to get you started. You should be done. Restart Apache.


home