Ruby on Rails
Fast CGI and Apache2 for Red Hat Linux 9 (Version #16)

These are instructions for installing and configuring FastCGI? for Apache2 on RedHat? Linux without re-compiling Apache.

Install the FastCGI? development kit

$ wget <a href="http://fastcgi.com/dist/fcgi-2.4.0.tar.gz">http://fastcgi.com/dist/fcgi-2.4.0.tar.gz</a> 
$ tar zxvf fcgi-2.4.0.tar.gz
$ cd fcgi-2.4.0
$ ./configure
$ make install

The FastCGI? dev kit will install into /usr/local/lib by default.

Install ruby-fcgi


$ gem install fcgi -- --with-fcgi-include=/usr/local/include
--with-fcgi-lib=/usr/local/lib

in Mandriva 2005 I have needed to also add -include-dependencies to this.

Install mod_fastcgi for Apache2

You will need the latest httpd and httpd-devel packages (as of this writing that is version 2.0.40-21.11):


$ rpm -q httpd
 > httpd-2.0.40-21.11 
$ rpm -q httpd-devel
 > package httpd-devel is not installed 
$ up2date httpd-devel

Once httpd and httpd-devel are up to date you can install the mod_fastcgi package:


$ wget <a href="http://www.wesmo.com/redhat/i386/mod_fastcgi-2.4.2-1.i386.rpm">http://www.wesmo.com/redhat/i386/mod_fastcgi-2.4.2-1.i386.rpm</a>
$ rpm -ivh mod_fastcgi-2.4.2-1.i386.rpm

Next edit your httpd.conf file (by default in /etc/httpd/conf) to include these lines under your other LoadModule? directives:

LoadModule fastcgi_module modules/mod_fastcgi.so

<IfModule mod_fastcgi.c>
  FastCgiIpcDir /tmp/fcgi_ipc
  FastCgiServer /var/www/html/proj/public/dispatch.fcgi \
    -initial-env RAILS_ENV=development \
    -idle-timeout 60
</IfModule>

Be sure the /tmp/fcgi-ipc directory specified above exists (might not), is writable and executable by apache:


$ mkdir /tmp/fcgi_ipc
$ chown apache.apache /tmp/fcgi_ipc -R
$ chmod 755 /tmp/fcgi_ipc -R

Configure your virtual host next:


<VirtualHost *:80>
  ServerName server
  DocumentRoot /var/www/html/proj/public/
  ErrorLog /var/www/html/proj/log/server.log

  <Directory /var/www/html/proj/public/>
    AllowOverride all
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>

Be sure the \ErrorLog is writable and executable by apache.

Now configure your Rails application to use the FastCGI? dispatcher.

Change directory to your Rails application.

Edit public/dispatch.fcgi (in rails 0.13.1 this is within the rails installation: lib/fcgi_handler.rb) and it should include only these lines (minus comments):


require File.dirname(__FILE__) + "/../config/environment" 
require 'fcgi_handler'

RailsFCGIHandler.process!

Now edit public/.htaccess and change the line:


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

to:


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

Make dispatch.fcgi and .htaccess executable:


$ chmod 755 public/dispatch.fcgi
$ chmod 755 public/.htaccess

Check that the Ruby fcgi? gem can be loaded:

sh $ cd public 
sh $ ruby -d ./dispatch.fcgi

If you get the following error:

...
Exception `MissingSourceFile' at /usr/lib/site_ruby/1.8/rubygems/custom_require.rb:21 - no such file to load -- fcgi
Exception `LoadError' at /usr/lib/ruby/gems/1.8/gems/fcgi-0.8.6.1/./fcgi.so:0 - libfcgi.so.0: cannot open shared object file: No such file or directory - /usr/lib/ruby/gems/1.8/gems/fcgi-0.8.6.1/./fcgi.so
...

It means that Ruby’s fcgi was not configured against /usr/local/lib (see above).

Now restart Apache:


$ /etc/rc.d/init.d/httpd restart

These are instructions for installing and configuring FastCGI? for Apache2 on RedHat? Linux without re-compiling Apache.

Install the FastCGI? development kit

$ wget <a href="http://fastcgi.com/dist/fcgi-2.4.0.tar.gz">http://fastcgi.com/dist/fcgi-2.4.0.tar.gz</a> 
$ tar zxvf fcgi-2.4.0.tar.gz
$ cd fcgi-2.4.0
$ ./configure
$ make install

The FastCGI? dev kit will install into /usr/local/lib by default.

Install ruby-fcgi


$ gem install fcgi -- --with-fcgi-include=/usr/local/include
--with-fcgi-lib=/usr/local/lib

in Mandriva 2005 I have needed to also add -include-dependencies to this.

Install mod_fastcgi for Apache2

You will need the latest httpd and httpd-devel packages (as of this writing that is version 2.0.40-21.11):


$ rpm -q httpd
 > httpd-2.0.40-21.11 
$ rpm -q httpd-devel
 > package httpd-devel is not installed 
$ up2date httpd-devel

Once httpd and httpd-devel are up to date you can install the mod_fastcgi package:


$ wget <a href="http://www.wesmo.com/redhat/i386/mod_fastcgi-2.4.2-1.i386.rpm">http://www.wesmo.com/redhat/i386/mod_fastcgi-2.4.2-1.i386.rpm</a>
$ rpm -ivh mod_fastcgi-2.4.2-1.i386.rpm

Next edit your httpd.conf file (by default in /etc/httpd/conf) to include these lines under your other LoadModule? directives:

LoadModule fastcgi_module modules/mod_fastcgi.so

<IfModule mod_fastcgi.c>
  FastCgiIpcDir /tmp/fcgi_ipc
  FastCgiServer /var/www/html/proj/public/dispatch.fcgi \
    -initial-env RAILS_ENV=development \
    -idle-timeout 60
</IfModule>

Be sure the /tmp/fcgi-ipc directory specified above exists (might not), is writable and executable by apache:


$ mkdir /tmp/fcgi_ipc
$ chown apache.apache /tmp/fcgi_ipc -R
$ chmod 755 /tmp/fcgi_ipc -R

Configure your virtual host next:


<VirtualHost *:80>
  ServerName server
  DocumentRoot /var/www/html/proj/public/
  ErrorLog /var/www/html/proj/log/server.log

  <Directory /var/www/html/proj/public/>
    AllowOverride all
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>

Be sure the \ErrorLog is writable and executable by apache.

Now configure your Rails application to use the FastCGI? dispatcher.

Change directory to your Rails application.

Edit public/dispatch.fcgi (in rails 0.13.1 this is within the rails installation: lib/fcgi_handler.rb) and it should include only these lines (minus comments):


require File.dirname(__FILE__) + "/../config/environment" 
require 'fcgi_handler'

RailsFCGIHandler.process!

Now edit public/.htaccess and change the line:


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

to:


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

Make dispatch.fcgi and .htaccess executable:


$ chmod 755 public/dispatch.fcgi
$ chmod 755 public/.htaccess

Check that the Ruby fcgi? gem can be loaded:

sh $ cd public 
sh $ ruby -d ./dispatch.fcgi

If you get the following error:

...
Exception `MissingSourceFile' at /usr/lib/site_ruby/1.8/rubygems/custom_require.rb:21 - no such file to load -- fcgi
Exception `LoadError' at /usr/lib/ruby/gems/1.8/gems/fcgi-0.8.6.1/./fcgi.so:0 - libfcgi.so.0: cannot open shared object file: No such file or directory - /usr/lib/ruby/gems/1.8/gems/fcgi-0.8.6.1/./fcgi.so
...

It means that Ruby’s fcgi was not configured against /usr/local/lib (see above).

Now restart Apache:


$ /etc/rc.d/init.d/httpd restart