Ruby on Rails
HowtoSetupChrootedApacheWithRailsOnOpenBSD

There is a seemingly more up-to-date guide (for OpenBSD 4.1) here: http://www.crucialwebhost.com/blog/ruby-on-rails-fastcgi-fcgi-in-openbsd/

Just a short howto on what you will have to do to make RubyOnRails work in a chrooted environment on \OpenBSD. This assumes you already have a working installation of Ruby and Rails.

To better understand the behavior of a chrooted Apache on \OpenBSD, see this page.

Start by creating the following directories:
/var/www/etc
/var/www/tmp
/var/www/usr/lib
/var/www/usr/libexec
/var/www/usr/local/bin
/var/www/usr/local/lib

/var/www/tmp needs to be writable by the www user or group.

Now copy the ruby binary to /var/www/usr/local/bin/
also copy the directories under /usr/local/lib/ruby to /var/www/usr/local/lib

You will also need to copy

/usr/local/lib/libruby.so.1.8*
to /var/www/usr/lib/

Then some configuration files, you will need at least your
/etc/resolv.conf to be copied to /var/www/etc/resolv.conf 
but you might want /etc/hosts as well.
The last files to copy is:
/usr/libexec/ld.so to /var/www/usr/libexec/
and the following libraries from /usr/lib/ and /usr/local/lib/ to /var/www/usr/lib/

Remember that the \FastCgi Process Manager will be chrooted to /var/www so if you wish to use the \FastCgiServer directive, you should give the path to dispatch.fcgi relative to the chroot example:

FastCgiServer /users/foo/rails/public/dispatch.fcgi -initial-env RAILS_ENV=production

Common Problems

Question:
I did all of the above but I get the following in my log files:

<a href="http://wiki.rubyonrails.org/rails/pages/FastCgiServer" class="existingWikiWord">FastCgiServer</a>: "/users/foo/rails/public/dispatch.fcgi" stat(/users/foo/rails/public/dispatch.fcgi) failed: No such file or directory

However, if I use

FastCgiServer /usr/local/www/users/foo/rails/public/dispatch.fcgi -initial-env RAILS_ENV=production

then Apache starts up, when when I try to access the application the following appears in the Apache error log:

[Sat Jun 18 14:15:45 2005] [alert] [client 127.0.0.1] (2)No such file or directory: 
<span class="newWikiWord">FastCGI<a href="http://wiki.rubyonrails.org/rails/pages/FastCGI">?</a></span>: failed to connect to (dynamic) server "/users/foo/rails/public/dispatch.fcgi":
something is seriously wrong, any chance the socket/named_pipe directory was removed?, 
see the <span class="newWikiWord">FastCgiIpcDir<a href="http://wiki.rubyonrails.org/rails/pages/FastCgiIpcDir">?</a></span> directive
[Sat Jun 18 14:15:45 2005] [error] [client 127.0.0.1] <span class="newWikiWord">FastCGI<a href="http://wiki.rubyonrails.org/rails/pages/FastCGI">?</a></span>:
incomplete headers (0 bytes) received from server "/users/foo/rails/public/dispatch.fcgi" 

Answer:
There is a default “inter-process communication” directory compiled into fcgi for FCGI sockets (usually /tmp/fcgi or /tmp/fcgi_ipc). This folder will be created automatically by mod_fcgi; HOWEVER, if you did not set your Group and User in /var/www/conf/httpd.conf file before you add_module fcgi, then it is possible that this directory will be created with a permission set that doesn’t allow Apache to access it once it’s there.

So: either a) once the directory is created, change its ownership or permissions, or else b) move the two lines that begin with “User” and “Group” in your Apache conf file to near the beginning and then delete the fcgi_ipc directory so it’s created correctly from the start.

Question:
I have followed these rules and I can not make it work chrooted (It works if I use “http -u”, i.e. not a chrooted apache). The apache error log shows the following:

[Fri Aug 26 16:15:21 2005] [error] [client 127.0.0.1] <span class="newWikiWord">FastCGI<a href="http://wiki.rubyonrails.org/rails/pages/FastCGI">?</a></span>: comm with (dynamic) server "/users/foo/rails/public/dispatch.fcgi" aborted:
(first read) idle timeout (30 sec)
[Fri Aug 26 16:15:21 2005] [error] [client 127.0.0.1] <span class="newWikiWord">FastCGI<a href="http://wiki.rubyonrails.org/rails/pages/FastCGI">?</a></span>:
incomplete headers (0 bytes) received from server "/users/foo/rails/public/dispatch.fcgi" 

Answer:
Make SURE that ALL the the library files listed above in the instructions are in /var/www/usr/lib. ALL of them, not just the ones you think you need, because there are library dependencies that Rails has that are not obvious from a first glance or an ldd.