Saturday, October 17, 2009

LAPR - RoR on Hardy VM w/Postgres

Well, if there is LAMP, LAPP and LAMR. What about LAPR - Linux, Apache, PostgreSQL and Ruby on Rails? My friend Ben said he tried out Ruby (he's a Python guy), but it didn't support Postgres very well. I wanted to see for myself, (having just arrived from Missouri- the ShowMe state). First I tried installing Postgres on an existing RoR VMWare appliance, but for some reason, could not get Ruby talking to the gem.

Next I tried the opposite approach. Converting an existing LAPP iso into LAPR. I am more familiar with installing Rails on Ubuntu than installing PG. That went much smoother, after, of course gnashing my teeth on the previous install.

Turnkey[1] makes some nice appliances for testing things out and for deployments. They have a Rails stack, a LAMP and the aforementioned LAPP stack. (They also have a Django Python one I'm dying to try out). They are based on Ubuntu and seem somewhat affiliated with them. They seem to all use Hardy because that is the latest LTS release. There is also a way to deploy your app in the 'cloud". For some appliance vendors this means an Amazon EC2 instance. For Turnkey, they are using VPS.NET (see the site for details).

What I like about Turnkey appliances is they are distributed as (Live/Installable) .isos and not pre-built vdks or other VM images. They will work anywhere even burned to a CD and booted from there. They also offer a Core appliance where you can build your own and utilize the web based interface they've built.

Once installed or booted from the Live CD, you point your web browser at the provided IP address or SSH into it. (using the root password you set up during the install). From there you apt-get install away! Now, Turnkey keeps their appliances up-to date daily with security updates, but not with application updates. I had to apt-get update and then apt-get upgrade. On my LAPP instance, this seemed to update PostrgeSQL to 8.3.8.

As I am wont to do, I installed Ruby and Rails using the tried and true method outlined on the Download page for Rails [2]. This involves compiling from source. To do this on Ubunt, see my earlier Post: Ruby with readline on Ubuntu [3].

Once you have Rails installed, you need to install the sqlite3-ruby gem before testing it out, as Rails is configured to use Sqlite3 out of the box. But to install it, you have to first install Sqlite3 and its development library.
sudo apt-get install sqlite3 libsqlite3-dev
sudo gem install sqlite3-rub
At this point, create a Rails app and start the Webrick server. Point your browser at the ip address:3000 and you should see the familiar Rails Welcome page. Clicking on "About your application environment" should reveal all the various gem versions including the DB adapter:Sqlite3.

Next, set up a model using the scaffold generator:
./script/generate scaffold Post name:sting comment:text
rake db:migrate
Going to ip address:3000/posts should show the empty posts model you just created. Now, on to installing Postgres support:

sudo apt-get install libpq-dev
sudo gem install pg

The correct gem to install is 'pg', no matter what you may have heard. Next setup config/database.yml:
adapter: postgresql
database: app
username: postgres
encoding: utf8
pool: 5
timeout: 5000
Turnkey LAPP ships with the postgres user having no password. Also, Postgres defaults to US-ASCII encoding, but Ruby expects UTF-8. (Ruby 1.9 lets you specify different encodings).

Finally, do another:
rake RAILS_ENV=production db:create db:migrate
# close and restart the server in production mode:
./script/server -e production
Now, enter any posts in your web browser, and they should show up in the phpPgAdmin screen.

You have successfully created a LAPR appliance. Go collect your fame and fortune!