Updated on: Dec. 20th, 2011
A step-by-step guide for getting Ruby on Rails (1.9.x/3.x) working on Ubuntu 11.x and Nginx.

Basics

Update the system:

Shell
1
2
sudo apt-get update
sudo apt-get upgrade

Install the essentials: Build system, NodeJS, Nginx

Shell
1
2
3
4
5
6
7
8
# Build essentials
sudo apt-get install build-essential bison openssl libreadline5 libreadline-gplv2-dev curl git-core zlib1g zlib1g-dev libssl-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libmysqlclient-dev
# NodeJS
sudo apt-get install nodejs
# Nginx
sudo apt-get install nginx
# Links (for testing)
sudo apt-get install links

Security

Add a bit of security:

Shell
1
2
3
4
5
6
7
8
# Install Uncomplicated Firewall
apt-get install ufw
# Permit incoming ports TCP 80 (http), and 443 (https)
sudo ufw add 'Nginx Full'
# Permit incoming port TCP 22 (ssh)
sudo ufw add 'OpenSSH'
# Enable UFW (fingers crossed)
sudo ufw enable

Ruby

Install Ruby using RVM.

Shell
1
sudo bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )

NOTE: I recommend relaunching your shell session to allow RVM to load properly.

Install Ruby:

Shell
1
2
3
4
# I'm targeting 1.9.2; you can substitute with version of choice.
rvmsudo rvm install 1.9.2
# And, set 1.9.2 as system default
rvmsudo rvm use 1.9.2 --default

Rails, and thin

Install Rails:

Shell
1
rvmsudo gem install rails

And, thin:

Shell
1
rvmsudo gem install thin

Install the thin daemon:

Shell
1
rvmsudo thin install

Because RoR and gang were installed via RVM, we need to make certain that thin (daemon) loads through the proper environment. This can be accomplish by creating a RVM wrapper for thin.

Shell
1
2
3
# Again, targeting 1.9.2p290 environment.
# Substitute as necessary.
rvmsudo rvm wrapper 1.9.2@/usr/local/rvm/gems/ruby-1.9.2-p290 daemon192 thin

This will create the following script /usr/local/bin/daemon192_thin. Edit /etc/init.d/thin, and change the following line from:

Shell
1
DAEMON=/usr/local/rvm/gems/ruby-1.9.2-p290/bin/thin

…to:

Shell
1
DAEMON=/usr/local/rvm/bin/daemon192_thin

thin daemon Configuration

For the purposes of this section, it’s assumed that target RoR application is in the following directory: /var/www/myapp.
Create a thin configuration file (Note: Remember to update path for your application):

Shell
1
rvmsudo thin config -C /etc/thin/myapp.yml --servers 4 -e production -c /var/www/myapp

This will generate a thin configuration file that will spawn (4) thin processes (cluster).
To start, and stop the thin damon:

Shell
1
2
3
4
# Start
service thin start
# Stop
service thin stop

Nginx

Simple nginx configuration to proxy thin cluster.

Shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
upstream thin_cluster {
server 127.0.0.1:3000;
server 127.0.0.1:3001;
server 127.0.0.1:3002;
server 127.0.0.1:3003;
}
server {
listen 80;
server_name myapp.domain;
access_log /var/www/myapp/log/access.log;
error_log  /var/www/myapp/log/error.log;
root   /var/www/celox.me/public/;
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;
if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}
if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}
if (!-f $request_filename) {
proxy_pass http://thin_cluster;
break;
}
}
}

Johanns

Tagged with:
 

Comments are closed.