Ruby On Rails & FastCGI (fCGI) In OpenBSD

If you’ve ever moved anything into chroot before, then you probably feel confident that you could get Rails done in a couple of minutes. I thought I could, and I was wrong! However, after much head scratching, Googling, and pouring over the same log files about a thousand times, I have succeeded. The good news is that if you know the steps then you can make it all work in just a few minutes after all.

Install stuff

# pkg_add ruby ruby-gems ruby-rails mod_fastcgi fcgi
# gem install fcgi

Almost everything you need is available as a package! However, you will need to install the fcgi Ruby Gem yourself.

Move tons of stuff into chroot

# export CHROOT=/var/www
# cp -p /bin/sh ${CHROOT}/bin
# cp -p /sbin/ldconfig ${CHROOT}/sbin
# cd /usr/lib
# cp -p libc.so.39.3 libcrypto.so.13.0 libm.so.2.3 libncurses.so.10.0 libreadline.so.3.0 libssl.so.11.0 libz.so.4.1 ${CHROOT}/usr/lib
# cd /usr/local/lib # cp -p libfcgi.so.0.0 libruby.so.1.84 ${CHROOT}/usr/lib
# cp -rp ruby ${CHROOT}/usr/lib
# cp -p /usr/local/bin/ruby ${CHROOT}/usr/local/bin

This may take a while, and will take some space. Make sure you get everything, because Rails will give your cryptic errors if something is missing, such as “uninitialized constant RailsFoo (NameError)”.

Run ldconfig in chroot

# export CHROOT=/var/www
# mkdir -p ${CHROOT}/var/run
# chroot ${CHROOT} /sbin/ldconfig /usr/local/lib

This builds ld.so.hints in your chroot. If you don’t do this then libs in /var/www/usr/local/lib won’t be found. An alternative is to put everything in /var/www/usr/lib, but I prefer to have the chroot mirror the original.

Create a new rails skeleton:

# rails /var/www/path/to/rails

Modify /var/www/conf/httpd.conf

LoadModule fastcgi_module /usr/lib/apache/modules/mod_fastcgi.so
<VirtualHost *:80>
    ServerName my.server.name
    DocumentRoot /path/to/rails/public
    ErrorLog /path/to/rails/logs/error_log
    CustomLog /path/to/rails/logs/access_log combined
    <Directory /rails>
        Options ExecCGI FollowSymLinks
        AllowOverride all
        Allow from all
        Order allow,deny
    </Directory>
</VirtualHost>

Obviously replace /path/to/rails with something more appropriate, and tailor the options to your liking.

Check your config and restart httpd

# apachectl configtest
# apachectl stop
# apachectl start

Fire up the browser!

At this point it should be working! What if it’s not working?! Check the httpd logs, both the VirtualHost and the main server logs. Try running outside chroot to see if chroot is the problem or if it’s something else. Chroot in and run it by hand:

# chroot /var/www sh
# ruby /path/to/rails/public/dispatch.fcgi

If Ruby dumps on you then something is wrong. If you get a status 500 server error then—believe it or not—everything is fine, but you have an error in httpd.conf. If you’re getting errors like uninitialized contact RailsFCGIHandler (NameError) then either Ruby is failing to find the files or you’re missing a *.so file. If you install a package or gem later, then move it into chroot you may not get everything that changed. In my case, Ruby wasn’t updating LOAD_PATH because I’d forgotten a few things in places I didn’t expect. Check with something like find /usr/local/lib/ruby -ctime 1…

Corrections and suggestions are welcome!

Bookmark:  Del.icio.us · Digg · Furl · Google · Reddit · Technorati · Yahoo!

Subscribe Now

Subscribe to our blog by RSS or by email.

Related Posts

    No related posts.

Comments

Trackbacks & Pingbacks

  1. ruby on rail tutorials « Happy Every Day.

Leave A Reply

Helpful Hint

To post HTML or other code, wrap your text in the <code> tag.