» Running Ruby on Rails on Nginx

If you want to set up Ruby on Rails on Ubuntu Lucid from scratch, there are quite some articles online to choose from. I found most of them involve compiling, only highlight 1 aspect, or are a bit outdated.
On top of that, getting it right can be hard as there are a number of issues related to Ruby and Debian/Ubuntu.
This is an attempt to put all the sweet info in 1 place.

Ruby

export PATH="${PATH}:/var/lib/gems/1.8/bin/"
echo 'export PATH="${PATH}:/var/lib/gems/1.8/bin/"' >> /etc/bash.bashrc
aptitude install ruby rubygems vim-ruby rub y-dev libzlib-ruby 
libyaml-ruby libreadline-ruby libncurses-ruby rdoc ri libcurses-ruby 
libruby libruby-extras libfcgi-ruby build-essential libopenssl-ruby 
libdbm-ruby libdbi-ruby libxml-ruby libxml2-dev

Rails

Simple:

gem install -v=2.3.5 rails

gem install rails should have worked but 2.3.6 - 2.3.8 (current at the of writing) have issues with mongrel

Or, if you want to live on the edge and try the latest:

gem install rails --pre

Or with RVM

RVM is a command line tool which allows us to easily install, manage and work with multiple ruby environments from interpreters to sets of gems. See installation instructions and a full tutorial on that.

App

My new app is called myapp.example.com

cd /var/www
rails new myapp.example.com
cd myapp.example.com

Have a look around and see what you can find .

Thin

Thin will be the Ruby server

gem install thin
thin install
/usr/sbin/update-rc.d -f thin defaults
thin config -C /etc/thin/myapp.example.com -c /var/www/myapp.example.com --servers 3 -e development # or: -e production for caching, etc

Or Mongrels

If you don't like Thin..

aptitude install mongrel mongrel-cluster
mongrel_rails cluster::configure -e development -p 3000 -N 3 -c /var/www/myapp.example.com -a 127.0.0.1 # or: -e production for caching, etc
mkdir /etc/mongrel_cluster
sudo ln -nfs /var/www/myapp.example.com/config/mongrel_cluster.yml /etc/mongrel_cluster/myapp.example.com.yml
#sudo ln -nfs /var/www/myapp.example.com/config/mongrel_cluster.yml /etc/mongrel-cluster/sites-enabled/myapp.example.com.yml

Nginx

Nginx will be the Web server, proxing ruby requests to thin, running on ports 3000-3002 If you haven't installed it yet, do

aptitude install nginx

Now that you have Nginx, create a vhost. Edit /etc/nginx/sites-enabled/myapp.example.com and type:

upstream myapp {
    server 127.0.0.1:3000;
    server 127.0.0.1:3001;
    server 127.0.0.1:3002;
}
server {
    listen   80;
    server_name .example.com;
 
    access_log /var/www/myapp.example.com/log/access.log;
    error_log  /var/www/myapp.example.com/log/error.log;
    root       /var/www/myapp.example.com;
    index      index.html;
 
    location / {
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header  Host $http_host;
        proxy_redirect    off;
        try_files /system/maintenance.html $uri $uri/index.html $uri.html @ruby;
    }
 
    location @ruby {
        proxy_pass http://myapp;
    }
}

Databases

First set up SQLite

aptitude install -y libdbd-sqlite3-ruby sqlite3 libsqlite3-dev libsqlite3-ruby
gem install sqlite3-ruby

MySQL?

Optionally if you want to use MySQL install the following (but do sqlite anyway):

aptitude install -y libmysqlclient-dev
gem install mysql

Then change your /var/www/myapp.example.com/config/database.yml and make it say something along the lines of

development:
    adapter: mysql
    host: localhost
    database: myapp
    username: myapp
    password: xxxxxxx

Note! database.yml doesn't accept tabs. If you are in vim, you might need to do:

:set expandtab
#:set tabstop=4 # how many spaces should tabs be replaced withs
:retab

Also, make you app require the mysql gem by adding the following to ./Gemfile

gem 'mysql', '2.8.1'

I am assuming you already have a mysql-server running. If not, you also need to aptitude install mysql-server first.

Nice gems

  • gem install uuidtool
  • gem install ruby-debug
  • gem install ruby-graphviz
  • gem install json
  • gem install activemerchant

Bring App Live

Let's restart our daemons to see if it worked:

For Thin:

/etc/init.d/thin restart && /etc/init.d/nginx reload; tail -f log/*.log

For Mongrel:

mongrel_cluster_ctl restart && /etc/init.d/nginx reload; tail -f log/*.log

Add this line to above the 2 default routes in config/routs.rb:

map.root :controller => "home"

Create a home controller, add a view for it, and remove the 'Welcome aboard' html.

script/generate controller home index
rm public/index.html
echo '<h1>HeyO!</h1><object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/9X2u2cdvJSg?fs=1&amp;hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/9X2u2cdvJSg?fs=1&amp;hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>' > app/views/home/index.erb

If you don't get any errors, point your browser to the Vhost you created, and you should see a pleasant surprise.

Our work is done here.

New to Ruby?

Here's some resources to help you further:

If you think I missed something, please do leave a comment and help me improve this article.

You Probably Shouldn't Follow Me


Like this Article?

I'd appreciate it if you leave a comment, spread the word, or consider a small donation


tags: ruby, ror, nginx, programming, ubuntu, thin, mongrel, sqlite, mysql, gems
category: Programming - Ruby - Ruby on Rails
read: 22,606 times

Add Comment

Comments have been automatically closed because of the age of the article. If you need to, you can still contact me on the subject.

Comments

#8. Pedro Assunção on 21 September 2011

Gravatar.com: Pedro AssunçãoThin might fail to install due to missing openssl rvm package, you might need to do what says in this site to make it work: https://rvm.beginrescueend.com/packages/openssl/

Thanks for sharing the instructions :)

#7. Andolasoft on 28 July 2011

Gravatar.com: AndolasoftI was very glad to discover this site on yahoo.I wished to say many thanks to you with regard to this fantastic article!! I definitely enjoyed every little bit of it and I've you bookmarked to check out new stuff you post.

#6. kevin-yonathan on 14 July 2011

Gravatar.com: kevin-yonathanIsn't it better to run ruby on its native web server like webrick? what do you think?

What about the performance differences between those two?

#5. networking-dummies on 07 July 2011

Gravatar.com: networking-dummiesi'm new on ruby on rail

#4. LostOn Ahill on 03 June 2011

Gravatar.com: LostOn AhillEach time I reboot, I have to re-type '/etc/init.d/thin start'

When I attempt to configure thin to autostart, I get the below feedback:
$ /usr/sbin/update-rc.d -f thin defaults
update-rc.d: using dependency based boot sequencing
... [more] update-rc.d: warning: thin stop runlevel arguments (0 1 6) do not match LSB Default-Stop values (S 0 1 6)
insserv: warning: current stop runlevel(s) (0 1 6) of script `thin' overwrites defaults (0 1 6 S).

#3. codingexplorer.blogspot.com on 12 April 2011

Gravatar.com: codingexplorer.blogspot.comnice post brother.
I'm new on RoR, and your post helps me so much
thank you

<a href='http://codingexplorer.blogspot.com'>codingexplorer.blogspot.com</a>

#2. Dai ly quang cao on 30 October 2010

Gravatar.com: Dai ly quang caoThanks for the information, I’m in the process of learning blog commenting myself. It has been hard work!

#1. Ruby on Rails Development on 07 October 2010

Gravatar.com: Ruby on Rails DevelopmentI see you have an expertise RoR..keep working guy