Setting up Ruby on Rails app on a creaky old Debian Stable box - quick guideSo, I had a simple Ruby on Rails app which needed deploying. The server (the same one this blog lives on) is running Debian Stable, with Apache 2.0.x. Now, at some point, I realise, I should upgrade to Apache 2.2.x and do the whole Mongrel/mod_proxy_balance thing, or else use lighttpd. But for the moment, I have a slightly exotic Apache config that I don't have time to upgrade. So I decided to go with a FastCGI setup.
So, first things first. You'll need ruby, obviously. 'sudo apt-get install ruby' should do the trick. You'll also need fastcgi. So do 'sudo apt-get install libapache2-mod-fastcgi' (the Apache bit) and 'sudo apt-get install libfcgi-ruby1.8' (the fastcgi daemon for ruby). Do 'sudo a2enmod fastcgi' to enable fastcgi. You'll also need mod_rewrite; install and enable that if you don't have it.
You'll also need rubygems, the Ruby package manager thing. Get it here, and install by doing 'sudo ruby setup.rb all'. You can then do 'sudo gem install rails --include-dependencies'. After some time, you will have a rails installation.
Okay, so getting there. Next step is the Apache configuration. Put your application where-ever you put the documentroots for your virtual servers. Create a new virtual server configuration as normal, but with the DocumentRoot set to (your app path)/public and the following added:
Options ExecCGI +FollowSymLinks
allow from all
Outside the Virtual server declaration, put:
FastCgiServer /var/www_virt/state/public/dispatch.fcgi -idle-timeout 120 -initial-env RAILS_ENV=production -processes 2
(with correct paths).
Nearly there. Your rails application has a default .htaccess file, but it's customised for cgi or fastcgi. Comment out those AddHandlers, as we're going to use a static fastcgi setup (explanation below).
In the rewrite section, change the redirect from dispatch.cgi to dispatch.fcgi.
In (your app)/config/environment.rb, uncomment the line "ENV['RAILS_ENV'] ||= 'production'". This will force your application into production mode. Make sure that the application directory belongs to the Apache user. Restart Apache and try to access your site. If all has gone well, it will come up.
This all seems to work okay, and it's really quite fast; give ab (the Apache benchmarker) a go.
Most tutorials around seem to recommend using dynamic fastcgi, often using the fcgi module. The problem with this is that it takes a while to launch the fastcgi process when the site is first accessed, or when it times out. Also, if it has trouble loading the process, you may get a 500 error. Here, two processes are started; for a tiny site you might load one while for a more popular site more could be used. The major limiting factor is memory. Each fastcgi process takes up 20 or 30 megabytes when in use, though it will presumably be swapped out if no-one's using the site for a while.