Ruby on Rails
fastcgi (Version #256)

What is FastCGI?

FastCGI is a language independent, webserver-independent, scalable, open extension to CGI that provides high performance without the limitations of server specific APIs.

Using FastCGI with Rails

Unix/Linux/BSD

If you are using Apache you will need mod_fcgi, and the FastCGI? Development kit; These can be obtained from http://www.fastcgi.com . You will also need ruby-fcgi.

You can do the install manually or using your package manager.

Package Managers

Download and install mod_fcgi from your distributions package management system of choice. A good resource is http://rpm.pbone.net/
  1. Gentoo:
    
    emerge fcgi ruby-fcgi mod_fastcgi
    

    Then edit /etc/apache(2)/modules/_mod_fastcgi.conf as described below. Add -D FASTCGI to /etc/conf.d/apache(2)
  2. Debian/Ubuntu:
    Under debian and ubuntu, run
     
    
    apt-get install libapache-mod-fastcgi libfcgi-ruby1.8
    

    (for apache 2, replace libapache with libapache2).
  3. Fedora
    Under Fedora, there is no package for mod_fcgi. See RailsOnFedora for a step by step guide to getting FCGI working in Fedora.

Manually
  1. Install the FastCGI? Devel kit.
    
    $ wget <a href="http://www.fastcgi.com/dist/fcgi-2.4.0.tar.gz">www.fastcgi.com/dist/fcgi-2.4.0.tar.gz</a> 
    $ tar zxvf fcgi-2.4.0.tar.gz
    $ cd fcgi-2.4.0
    $ ./configure
    $ sudo make install
    
  2. Install ruby-fcgi. To be able to compile this gem, you might need libfcgi-dev:
    
    $ sudo apt-get install libfcgi-dev
    $ sudo gem install fcgi
    

    some systems may need: (FreeBSD for example)
    
    $ sudo gem install fcgi -- --with-fcgi-include=/usr/local/include --with-fcgi-lib=/usr/local/lib
    

    (ToDo: expand on this)

  1. add the following to your apache httpd. conf
    
    LoadModule fastcgi_module /usr/lib/apache2/modules/mod_fastcgi.so
    <IfModule mod_fastcgi.c>
        FastCgiIpcDir /tmp/fcgi_ipc/
        AddHandler fastcgi-script .fcgi
    </IfModule>
    

    You may have mod_fastcgi.so installed in a different place. If so, use the correct path.
  2. reload apache with
    
    $ apache2ctl reload
    

    Use apachectl if you are not using apache2
  3. change directories to your rails app
  4. in public/dispatch.fcgi you may have to change
    
    require 'fcgi'
    

    to
    
    require 'rubygems'
    require_gem 'fcgi'
    

    NOTE: in Rails 1.0.0 there is no require ‘fcgi’ in the dispatch.fcgi, so this change is not needed. If you are running a old rails version just update all your installed gems using :
    
    $ sudo gem update
    
  5. in public/.htaccess change
    
    RewriteRule ^(.*)$ /dispatch.cgi?$1 [QSA,L]
    

    to
    
    RewriteRule ^(.*)$ /dispatch.fcgi?$1 [QSA,L]
    

thanks to ((Jarkko Laine))

For me (Mefiboset) this did not work, I used this instead:


RewriteRule ^(.*)$ /dispatch.fcgi$1 [L]

Make sure your rails environment is correct!

If your app (or an opensource app like typo or hierarki) is working fine using a webbrick server normaly started with:


$ ./script/server -e production


but the fastcgi version keeps returning error messages, check out the environment settings in the file config/environment.rb, it should contain a line like this:

ENV['RAILS_ENV'] ||= 'production'

It took me a long night without sleep to find this out.

Also note that testing dispatch.fcgi from the command line can reveal interesting errors:


$ public/dispatch.fcgi

If you get Ruby errors you should fix these first, however if you see a

Status: 500 Internal Server Error

this may be a good sign: it seems the normal response if dispatch.fcgi is run from the command line !.

If you get errors

  • The dispatch.fcgi or dispatch.cgi has an incorrect path to Ruby. This won’t be the case if you used the ocs-install-rails script, but if you developed on another machine, please make sure the path is #!/usr/bin/ruby in all scripts that include a Ruby path at the top (including the dispatch scripts in public folder and the generators and other utilities in the script folder)
  • Make sure your app folder owner is the same as the webserver (ie. nobody)
  • Make sure that the dispatch.fcgi and dispatch.cgi scripts have 0755 (-rwxr-xr-x) permissions.
  • Make sure you are accessing the Rails app with a trailing / after the URL, like yoursite.com/rails/ instead of yoursite.com/rails
  • If you have previously tested the application, you may need to clear old session files, which are found in tmp/sessions
  • You may also need to chmod tmp/sessions 777 or sort it so the fcgi process can write session files into that directory

See Also

For Windows systems

Resources

Troubleshooting

If you get in your log files something along the lines of “dispatch.fcgi has failed to remain running for 30 seconds given 3 attempts, its restart interval has been backed off to 600 seconds” then you need to edit public/dispatch.fcgi and dispatch.rb to change the location of ruby on the first line to the correct location on your deployment machine – this is especially important if you’ve developed on Windows and are deploying on Unix

If you get error: undefined method `is_cgi?’

because native fcgi.so not loaded correctly.
fcgi.so depends on libfcgi.so.0

add this to /etc/ld.so.conf:


/usr/local/lib

then type: /sbin/ldconfig
retry again

Side Note: The above says to use apachectl or apache2ctl to restart apache. However, several linux distros use scripts in /etc/init.d to control daemon processes like apache. If you have such a directory, check to see if there’s an apache script in there!.

What is FastCGI?

FastCGI is a language independent, webserver-independent, scalable, open extension to CGI that provides high performance without the limitations of server specific APIs.

Using FastCGI with Rails

Unix/Linux/BSD

If you are using Apache you will need mod_fcgi, and the FastCGI? Development kit; These can be obtained from http://www.fastcgi.com . You will also need ruby-fcgi.

You can do the install manually or using your package manager.

Package Managers

Download and install mod_fcgi from your distributions package management system of choice. A good resource is http://rpm.pbone.net/
  1. Gentoo:
    
    emerge fcgi ruby-fcgi mod_fastcgi
    

    Then edit /etc/apache(2)/modules/_mod_fastcgi.conf as described below. Add -D FASTCGI to /etc/conf.d/apache(2)
  2. Debian/Ubuntu:
    Under debian and ubuntu, run
     
    
    apt-get install libapache-mod-fastcgi libfcgi-ruby1.8
    

    (for apache 2, replace libapache with libapache2).
  3. Fedora
    Under Fedora, there is no package for mod_fcgi. See RailsOnFedora for a step by step guide to getting FCGI working in Fedora.

Manually
  1. Install the FastCGI? Devel kit.
    
    $ wget <a href="http://www.fastcgi.com/dist/fcgi-2.4.0.tar.gz">www.fastcgi.com/dist/fcgi-2.4.0.tar.gz</a> 
    $ tar zxvf fcgi-2.4.0.tar.gz
    $ cd fcgi-2.4.0
    $ ./configure
    $ sudo make install
    
  2. Install ruby-fcgi. To be able to compile this gem, you might need libfcgi-dev:
    
    $ sudo apt-get install libfcgi-dev
    $ sudo gem install fcgi
    

    some systems may need: (FreeBSD for example)
    
    $ sudo gem install fcgi -- --with-fcgi-include=/usr/local/include --with-fcgi-lib=/usr/local/lib
    

    (ToDo: expand on this)

  1. add the following to your apache httpd. conf
    
    LoadModule fastcgi_module /usr/lib/apache2/modules/mod_fastcgi.so
    <IfModule mod_fastcgi.c>
        FastCgiIpcDir /tmp/fcgi_ipc/
        AddHandler fastcgi-script .fcgi
    </IfModule>
    

    You may have mod_fastcgi.so installed in a different place. If so, use the correct path.
  2. reload apache with
    
    $ apache2ctl reload
    

    Use apachectl if you are not using apache2
  3. change directories to your rails app
  4. in public/dispatch.fcgi you may have to change
    
    require 'fcgi'
    

    to
    
    require 'rubygems'
    require_gem 'fcgi'
    

    NOTE: in Rails 1.0.0 there is no require ‘fcgi’ in the dispatch.fcgi, so this change is not needed. If you are running a old rails version just update all your installed gems using :
    
    $ sudo gem update
    
  5. in public/.htaccess change
    
    RewriteRule ^(.*)$ /dispatch.cgi?$1 [QSA,L]
    

    to
    
    RewriteRule ^(.*)$ /dispatch.fcgi?$1 [QSA,L]
    

thanks to ((Jarkko Laine))

For me (Mefiboset) this did not work, I used this instead:


RewriteRule ^(.*)$ /dispatch.fcgi$1 [L]

Make sure your rails environment is correct!

If your app (or an opensource app like typo or hierarki) is working fine using a webbrick server normaly started with:


$ ./script/server -e production


but the fastcgi version keeps returning error messages, check out the environment settings in the file config/environment.rb, it should contain a line like this:

ENV['RAILS_ENV'] ||= 'production'

It took me a long night without sleep to find this out.

Also note that testing dispatch.fcgi from the command line can reveal interesting errors:


$ public/dispatch.fcgi

If you get Ruby errors you should fix these first, however if you see a

Status: 500 Internal Server Error

this may be a good sign: it seems the normal response if dispatch.fcgi is run from the command line !.

If you get errors

  • The dispatch.fcgi or dispatch.cgi has an incorrect path to Ruby. This won’t be the case if you used the ocs-install-rails script, but if you developed on another machine, please make sure the path is #!/usr/bin/ruby in all scripts that include a Ruby path at the top (including the dispatch scripts in public folder and the generators and other utilities in the script folder)
  • Make sure your app folder owner is the same as the webserver (ie. nobody)
  • Make sure that the dispatch.fcgi and dispatch.cgi scripts have 0755 (-rwxr-xr-x) permissions.
  • Make sure you are accessing the Rails app with a trailing / after the URL, like yoursite.com/rails/ instead of yoursite.com/rails
  • If you have previously tested the application, you may need to clear old session files, which are found in tmp/sessions
  • You may also need to chmod tmp/sessions 777 or sort it so the fcgi process can write session files into that directory

See Also

For Windows systems

Resources

Troubleshooting

If you get in your log files something along the lines of “dispatch.fcgi has failed to remain running for 30 seconds given 3 attempts, its restart interval has been backed off to 600 seconds” then you need to edit public/dispatch.fcgi and dispatch.rb to change the location of ruby on the first line to the correct location on your deployment machine – this is especially important if you’ve developed on Windows and are deploying on Unix

If you get error: undefined method `is_cgi?’

because native fcgi.so not loaded correctly.
fcgi.so depends on libfcgi.so.0

add this to /etc/ld.so.conf:


/usr/local/lib

then type: /sbin/ldconfig
retry again

Side Note: The above says to use apachectl or apache2ctl to restart apache. However, several linux distros use scripts in /etc/init.d to control daemon processes like apache. If you have such a directory, check to see if there’s an apache script in there!.