Articles Photographs

Merb, SQLite, and Nginx on Ubuntu

Categories
created: 4/3/2008 updated: 5/22/2008

Merb

Merb is the best framework for building Ruby web applications. It has a concise core that you can augment with RubyGems. It is faster, smaller, and more flexible than alternate Ruby frameworks, such as Ruby on Rails.
gem install merb

Merb also has some helpers that are very useful.
gem install merb_has_flash merb_helpers

SQLite

I love the simplicity of working with an SQLite database. The libsqlite3-dev package is an unapparent prerequisite of DataMapper.
aptitude install sqlite3 libsqlite3-dev libsqlite3-ruby -y

I use DataMapper for the model.
gem install datamapper do_sqlite3 merb_datamapper

I use Thin for serving.
gem install thin

Generate a merb application
merb-gen app appname

Create SQLite databases
cd appname
mkdir db
vim config/init.rb
a

### Uncomment for DataMapper ORM
use_orm :datamapper


esc :wq
merb-gen
mv config/database.yml.sample config/database.yml
vim config/database.yml


---
:development: &defaults
:adapter: sqlite3
:database: db/development.sqlite3.db

:test:
<<: *defaults
:database: db/test.sqlite3.db

:production:
<<: *defaults
:database: db/production.sqlite3.db


esc :wq
merb-gen resource resourcename column1name:string column2name:string
rake dm:db:automigrate
rake MERB_ENV=test dm:db:automigrate
rake MERB_ENV=production dm:db:automigrate


Start a local merb application for development
cd applocation/appname
merb


Stop a local merb application
cntl c

Start a production merb application at a specified location, with 2 thin app servers, as daemons, starting at port 4000
merb -e production -m applocation/appname -c 2 -a thin -d -p 4000

Stop any merb daemons within an application
cd applocation/appname
merb -K all


Nginx

I use the http server Nginx, because it is small, easy to configure, and very fast.
aptitude install nginx -y

Add a site that proxies to two web applications.
vim /etc/nginx/sites-available/sitename

upstream sitename {
server 127.0.0.1:4000;
server 127.0.0.1:4001;
}

server {
listen 80;
server_name www.sitename.com;
rewrite ^(/.*) http://sitename.com$1 permanent;
}

server {
listen 80;
server_name sitename.com;
location / {
proxy_pass http://sitename;
}
}


Enable the site
ln -s /etc/nginx/sites-available/sitename /etc/nginx/sites-enabled/sitename
/etc/init.d/nginx restart


Comments New
meekish
Hey Jamie, I just found this article through the Merb wiki. Sorry your edit didn't come up at first; Defensio thought it was spam; it's there now. Hope you enjoy the wiki!
Jamie Hoover
Thank you for including in the new wiki, meekish. Congratulations on getting it live! I sure hope it winds up better maintained than a certain other Ruby framework's wiki. ;)
Rasmus
Hi, I am trying to get merb up and running, following this article. I am running on Ubunto 8.04 (beta). A couple of points should be noted:
From a fresh install, you need to add ruby-dev and sqlite-dev (and possible some other pacakages), else some of the gems will have problems installing.
I belive that ubuntu build-essentials should be installede.
I you copy past the database.yml from your recipe, then make sure that all parameters is indented correctly.
Rasmus
The newst version of merb do not create the db folder for you. So if you get a "Unable to connect to database with provided connection string." try to check if the db folder exists.
Rasmus
last note: merb raise an exception that i can not find merb-core, include "require "rubygems"" in the header of the merb file