Let’s start with setting up a developer account. Please note: “:developername” should be replaced with whatever your unix user name you want to be when developing (“irakli” in my case).

ERROR:  Loading command: install (LoadError)
    no such file to load -- zlib

Install Latest Nginx From Source:

$ sudo apt-get install libc6 libpcre3 libpcre3-dev libpcrecpp0 libssl0.9.8 libssl-dev zlib1g zlib1g-dev lsb-base
$ cd /usr/local/src/
$ sudo wget http://nginx.org/download/nginx-1.0.5.tar.gz
$ sudo tar xzvf nginx-1.0.5.tar.gz 
$ cd nginx-1.0.5

Install Passenger for NginX:

$ sudo gem install passenger
$ sudo apt-get install libcurl4-openssl-dev
$ sudo passenger-install-nginx-module

Important: Please note that Nginx does not support dynamic modules. Adding a module to Nginx means re-compiling it. passenger installer will guide you through the steps to recompile. Please make sure you answer the questions in a way similar to what’s shown below, otherwise your NginX may not be re-compiled properly:

> 2. No: I want to customize my Nginx installation. (for advanced users)

> Where is your Nginx source code located?
> Please specify the directory: /usr/local/src/nginx-1.0.5

> Where do you want to install Nginx to?
> Please specify a prefix directory [/opt/nginx]: /usr
> Extra arguments to pass to configure script: --with-http_ssl_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-http_stub_status_module --with-http_realip_module

Configure Nginx With A Sample Rails App

$ sudo ln -s /usr/conf/nginx.conf /etc/nginx.conf
$ sudo mkdir /var/www/rails.vm
$ sudo chgrp -R webmaster /var/www
$ sudo chmod -R 775 /var/www/
$ cd /var/www/rails.vm
$ rails new firstapp

Where “rails.vm” above (and below in the text) is the domain name of the webapp you intend to build, replace it with your domain if needed. Edit file: /etc/nginx.conf as root and insert the following lines at the end of the “http” section (typically: before the very last closing curly brace “}” in the file, unless Nginx changes the default config file):

  include sites-enabled/*;

create “/usr/conf/sites-enabled” directory and /usr/conf/sites-enabled/rails.vm configuration file with the following content:

server {
  listen 80;
  server_name ruby.sam.ge;
  root /var/www/rails.vm/firstapp/public;   # <--- be sure to add 'public' to the app path!
  
  passenger_enabled on;
  rails_env development;

  gzip on;
  gzip_comp_level 2;
  gzip_proxied any;
  gzip_min_length  1000;
  gzip_disable     "MSIE [1-6]\."
  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

}

Start Nginx if it is stopped with:

$ sudo /usr/sbin/nginx

or if it is already running, reload configuration with:

sudo /usr/sbin/nginx -s reload

you can stop nginx at any point with:

sudo /usr/sbin/nginx -s stop

Once Nginx is running, and if you have rails.vm domain (or whatever domain you ended up configuring in Nginx) properly pointed to the server’s IP, if you go to that domain in your browser, you should see a test Rails page that comes from your sample app. At this point Rails app is properly set up and configured in Nginx.

Install MongoDB

Import GPG Key:

$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10

add this line to your /etc/apt/sources.list

deb http://downloads-distro.mongodb.org/repo/debian-sysvinit dist 10gen

and run:

$ sudo apt-get update
$ sudo apt-get install mongodb-10gen
$ sudo gem install mongo bson bson_ext

For further documentation about Rails3 and Mongo you should take a look at the following two links:

Install MySQL 5.1 (Optional)

$ sudo su -
# echo "deb http://packages.dotdeb.org oldstable all" >> /etc/apt/sources.list
# echo "deb-src http://packages.dotdeb.org oldstable all" >> /etc/apt/sources.list
# echo "deb http://php53.dotdeb.org oldstable all" >> /etc/apt/sources.list
# echo "deb-src http://php53.dotdeb.org oldstable all" >> /etc/apt/sources.list

# cd /usr/local/src
# wget http://www.dotdeb.org/dotdeb.gpg
# cat dotdeb.gpg | sudo apt-key add -
# apt-get update
# exit

$ sudo apt-get install mysql-server-core-5.1 mysql-source-5.1 mysql-server-5.1 mysql-client-5.1
$ sudo apt-get install libmysqlclient15-dev libmysql-ruby
$ sudo gem install mysql

Bonus: Sinatra Sample App

This is a bonus section with quick example of how to use already-installed Nginx and Phusion with Rails stack to run a Sinatra sample app (e.g. to augment your Rails app with more light-weight REST API component on top of Sinatra). You will need a separate nginx configuration file that looks something like:

server {
  listen 80;
  server_name sinatra.vm;
  root /var/www/sinatra.vm/code/public;   

  passenger_enabled on;
  gzip on;
}

Under /var/www/sinatra.vm/code you need following files:

Gemfile:

source 'http://rubygems.org'

gem 'sinatra', '1.2.6'

config.ru:

require 'rubygems'
require 'sinatra'

set :env, :development
disable :run

require File.expand_path('../app', __FILE__) # Name "app" corresponds to app.rb file.

run MyApi::ApplicationClass

app.rb:

require 'sinatra'

module MyApi
  class ApplicationClass < Sinatra::Base
    get '/' do
      'Default Response from API'
    end
  end
end

Once you have these files run following commands under /var/www/sinatra.vm/code

$ mkdir public
$ bundle install
$ sudo /usr/sbin/nginx -s reload

Code Reloading

Phussion Passenger caches code to improve performance. This can become counter-productive in a development environment where you are changing code all the time since you may end-up having to issue “sudo /usr/sbin/nginx -s reload” every time you change your code. There’re several remedies:

For Rails you can just set: “RailsEnv development “ or “RakeEnv development” in the Nginx configuration and code will be reloaded at every request.

For Sinatra, you have two choices, either create “tmp/restart.txt” under the root of your code tree and issue “touch tmp/restart.txt” every time you need code reloaded instead of reloading entire Nginx (still pretty annoying, imho) or create “tmp/always_restart.txt” in which case cache will be reloaded at every http request.