I've tried quite a few ways to deploy Ruby on Rails apps for Booko. Here's how to do it with Passenger.
Methods tried so far:
- Apache with mod_proxy and mongrel cluster
- Apache with mod_proxy and thin
- Nginx with thin
I only gave nginx a small run - I needed PHP for the blog (I've since moved Booko to it's own host, so this wouldn't be a problem) and it seemed great - but so far, I think REE + mod_rails is the best. It's the most convenient to install/setup, it seems to be very fast, and it is quite nice to manage. You can restart your Rails application with a _touch tmp/restart.txt - you know it's restarted because the file disappears - that's pretty nice. Naturally, you can restart Apache too.
So, this is a post on how to install Ruby Enterprise Edition and Phusion Passenger (aka: mod_rails) on Ubuntu 8.04 LTS in a VPS, specifically, Slicehost. It's not hard, but this should make it even faster. You can read all about Ruby Enterprise Edition, but basically it's a version of Ruby tuned to be fast and lean. It's made by the guys who've created Phusion Passenger, or mod_rails and they work well together. I'll be assuming that you want to use this version of Ruby for everything (ie - command line work as well as for mod_rails). IE, when you call irb or ruby or rake from the command line, you want to use REE.
Create a local user
If you've just built your slice, create yourself a user account and set up ssh. Login:
Use vi as your command line editor because you're a real man:
echo "set -o vi" >> .bashrc
Create your user account:
useradd -c "Dan Milne" -d "/home/dkam/" -m -s "/bin/bash" -G sudo dkam
We're going to add your user to the sudo group so you can work without being root.
chown -R user /home/dkam
Set the password for your user. Next, we'll let anyone in the sudo group use sudo. I'm actually from a world where wheel was the admin group - I'm presuming sudo can fulfil this role in Ubuntu.
echo "%sudo ALL=(ALL)ALL" >> /etc/sudoers
Now, you can log out and reconnect as your user and if you wish.
Getting the software installed
This section will assume you're the root user (for easy of cut and pasting commands).
Get your Ubuntu distro up-to-date, then we'll install the required software:
aptitude update aptitude dist-upgrade -y aptitude install vim git-core screen build-essential apache2-prefork-dev \ apache2-mpm-prefork mysql-server mysql-client libmysqlclient15-dev libreadline5-dev -y
You'll need the build-essentials for compiling stuff, apache2 to serve your site, mysql because REE builds the mysql gem for you, and readline stuff so that your irb and script/console stuff will work nicely. vim, git-core and screen aren't strictly required, but part of my bag of tricks.
mkdir source && cd source wget http://rubyforge.org/frs/download.php/47937/ruby-enterprise-1.8.6-20081205.tar.gz tar xfvz ruby-enterprise-1.8.6-20081205.tar.gz ./ruby-enterprise-1.8.6-20081205/installer
Accept all the defaults. Once that's installed, add it to your path and re-read your bashrc file:
echo "export PATH=/opt/ruby-enterprise-1.8.6-20081205/bin:\$PATH" >> ~/.bashrc . ~/.bashrc
Make sure you're using the correct ruby binaries:
root@demo:~/source# which gem /opt/ruby-enterprise-1.8.6-20081205/bin/gem
If that says "/usr/bin/gem" you need to make user your PATH variable includes your new Ruby install at the beginning. Make sure your gems are up-to-date:
gem update gem update --system
Then, install passenger (that's mod_rails for those not following along):
Once again, accept the defaults. Now, we'll set up the Apache config - rather than tacking it on the end of an Apache config file, we'll do this the nice way. We'll create a module file, then symlink it into the mods-enabled directory.
cd /etc/apache2/mods-available/ echo "LoadModule passenger_module /opt/ruby-enterprise-1.8.6-20081205/lib/ruby/gems/1.8/gems/passenger-2.0.5/ext/apache2/mod_passenger.so PassengerRoot /opt/ruby-enterprise-1.8.6-20081205/lib/ruby/gems/1.8/gems/passenger-2.0.5 PassengerRuby /opt/ruby-enterprise-1.8.6-20081205/bin/ruby PassengerMaxPoolSize 3 PassengerDefaultUser www-data" > passenger.load
Now that file is created, we'll link it into the mods-enabled directory:
cd ../mods-enabled/ ln -s ../mods-available/passenger.load Next, we'll make a small ruby site: cd /var/www/ rails htdocs
Next, edit vim /etc/apache2/sites-enabled/000-default , changing the DocumentRoot to
Restart Apache and navigate to your host's IP address - you should see a Welcome Aboard message. And we're done! Have fun.