Ruby on Rails
Debian mod_fastcgi Notes

NOTE: This document is incomplete. Need to finish converting mod_fastcgi instructions to work with Apache 1.x. That module is only recommended with Apache 1.x for rails.

Before we begin…

Remember that:

Three methods

1. install libapache2-mod-fcgid (mod_fcgid). mod_fcgid is an actively maintained GPL project designed to replace mod_fastcgi. It is designed for Apache 2.x and is reported to be more reliable. Only available for Apache 2.x.

2. install libapache-mod-fastcgi (mod_fastcgi). Unless you do not want to use unmaintained or non-free packages, this is the recommended method for Apache 1.3.x. On Apache 2.x, use libapache2-mod-fcgid (first approach) which is actively maintained upstream.

3. compile mod_fastcgi from source.

All three methods follow.

Installing libapache2-mod-fcgid

The latest version of mod_fcgid is 1.09 (released on April 25, 2006). It is recommended that you do not use earlier versions because 1.09 adds configuration option DefaultMinClassProcessCount(default 3).

Other instructions on using mod_fcgid with Rails are at:

FIRST STEP:

Install the module by executing:

apt-get install libapache2-mod-fcgid

SECOND STEP: verify configuration

NOTE: mod_fcgid will kill dispatch.fcgi process that expires based on IdleTimeout or ProcessLifeTime but you can use DefaultMinClassProcessCount to make the specified number of processes ignore IdleTimeout.

Example of /etc/apache2/mods-enabled/fcgid.conf (symbolic link)


<IfModule mod_fcgid.c>
  AddHandler fcgid-script .fcgi
  SocketPath /var/lib/apache2/fcgid/sock
  DefaultInitEnv  RAILS_ENV production
  IdleTimeout 600
  ProcessLifeTime 3600
  MaxProcessCount 8
  DefaultMinClassProcessCount 3
  DefaultMaxClassProcessCount 3
  IPCConnectTimeout 8
  IPCCommTimeout 48




Example /etc/apache2/mods-enabled/fcgid.load (symbolic link)

LoadModule fcgid_module /usr/lib/apache2/modules/mod_fcgid.so
</pre>


THIRD STEP: restart apache2

invoke-rc.d apache2 force-reload
</pre>

Installing libapache-mod-fastcgi for Apache 1.x

Again, this requires getting a package from the non-free repository. If you are objected to this, please use the second method.

Setup apt to access the non-free repository

To access the non-free repositories, you must modify your /etc/apt/sources.list file. You may already have the non-free repository added, in which case, skip this step.

For all the sources you have, append “non-free” at the end. It should look like this (your host, and distribution vary):

deb <a href="http://mir2.ovh.net/debian/">http://mir2.ovh.net/debian/</a> unstable main contrib non-free

Then update your package lists:

apt-get update

Install libapache-mod-fastcgi

NOTE: this is for Apache 1.x users. For Apache 2.x, use mod_fcgid.

Install the module by executing:

apt-get install libapache-mod-fastcgi

Enable the module

Run the apache-provided aenmod command.

aenmod fastcgi

Configure mod_fastcgi

If you want to set the initial environment variable RAILS_ENV globally, this is the time to do it. Modify /etc/apache/mods-available/fastcgi.conf. If you don’t want to enabled this globally, you can set it in config/environment.rb that is located in the specific rails project instead.

Restart apache

apachectl -k graceful

And you’re set!

Compiling mod_fastcgi-2.4.2

WARNING: mod_fastcgi is not recommended for use with Apache2 and rails. This module is unreliable on Apache 2.x.

If you are compiling mod_fastcgi from source on a Debian platform and using the default Apache2 setup, you will need to retrieve the Apache2 source from the Debian repositories:

sudo apt-get install apache2-threaded-dev

Then you will be able to :


   cd ..
   tar -zxvf mod_fastcgi-2.4.2.tar.gz
   cd mod_fastcgi-2.4.2
   cp Makefile.AP2 Makefile
   make top_dir=/usr/share/apache2
  sudo make install top_dir=/usr/share/apache2

Configuring Apache

Edit your /etc/apache2/sites-enabled to something such as this:


NameVirtualHost  *:80
<VirtualHost *:80>
ServerName localhost 
ServerAdmin <a href="mailto:your@email.addy">your@email.addy</a>
DocumentRoot /var/www/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
# This directive allows us to have apache2's 
                # default start page
                # in /apache2-default/, but still have / go 
                # to the right place
RedirectMatch ^/$ /apache2-default/
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error,
        # crit, alert, emerg.
LogLevel warn
CustomLog /var/log/apache/access.log combined
ServerSignature On
Alias /doc/ "/usr/share/doc/" 
<Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
       # this is for a localhost setup, your needs may be 
       # different on a production server
</Directory>

</VirtualHost>

<IfModule mod_fastcgi.c>
    FastCgiIpcDir /tmp/fcgi_ipc/
    FastCgiServer /app/public/dispatch.fcgi -initial-env  
RAILS_ENV=production -processes 15 -idle-timeout 60
 # As per DHH from RoR mailing list 5/17/05
</IfModule>

<VirtualHost *:80>
     ServerName rails
     DocumentRoot /path/to/rails/app/public/
     ErrorLog /path/to/rails/app/log/apache.log
     ServerAdmin <a href="mailto:your@email.addy">your@email.addy</a>
<Directory /path/to/rails/app/public/ >
       Options ExecCGI FollowSymLinks
       AllowOverride all
       Allow from all
       Order allow,deny
</Directory>
</VirtualHost>

Then, create a file /etc/apache/mods-enabled/fcgi.load and add the following lines to it:


LoadModule fastcgi_module /usr/lib/apache2/modules/mod_fastcgi.so
AddHandler fastcgi-script .fcgi

If you are using mod_fcgid (on Apache 2) you MUST modify your .htaccess for following handler. If you don’t, all you will see is your dispatch.fcgi as text for whatever page you call.


#COMMENTED OUT#AddHandler fastcgi-script .fcgi
#COMMENTED OUT#AddHandler cgi-script .cgi
AddHandler fcgid-script .fcgi
[...]
#COMMENTED OUT#RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

  1. last line must be here after rules

Last but not least: remember to give permission to the webserver to write in the tmp directory (the easiest is a chmod 666 tmp)