Deploy Rails Local with Nginx, Passenger, RVM and Homebrew on OS X

This article will show you how to deploy a ruby on rails application in local environment with RVM, Homebrew, Passenger & Nginx on Mac OS X.

http://blog.phusion.nl/wp-content/uploads/2009/04/nginx_passenger_eyecatcher.png

###Prepare

You need some basic knowledge about RVM and homebrew, don’t worry, it’s very easy and just make u happy.

###RVM & Ruby

Ruby Version Manager, you can use it to install and manage multiple versions of ruby, there is a use guide in Chinese write easy. http://lanvige.com/posts/rvm_guide_cn/

We will use ruby-1.9.3-p0 & rails 3.1.3 in this demo.

###Homebrew

Homebrew is the best software install & manage tool on OS X, it also fixed the dependent issue. more about homebrew, see: http://lanvige.com/posts/homebrew_in_action/

###Gems

Install the least version of rails, current version 3.1.3

1
$ gem install rails

Install Passenger gem, version 3.0.11

1
$ gem install passenger

###Nginx

####Build with passenger module

Then use brew to install the least version of nginx.

1
$ brew install nginx

Because we need passenger as application work with nginx, you can build the passenger module later, but brew already provide a argument to build nginx with passenger module, use below line instead.

1
$ brew install nginx --with-passenger

After the nginx build and install successfuly, there will be a brief introduction to configure the init with system, and use launchctl load to start the nginx right now.

1
2
$ cp /usr/local/Cellar/nginx/1.0.10/org.nginx.plist ~/Library/LaunchAgents
$ launchctl load -w ~/Library/LaunchAgents/org.nginx.plist

####Nginx Config File

Homebrew set all the app’s config file at /usr/local/etc/ folder replace /ect/, so edit the /usr/local/etc/nginx/nginx.conf file to add ruby and applications’ info.

####nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
user samsoffes staff;
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
passenger_root /Users/lanvige/.rvm/gems/ruby-1.9.3-p0@rails/gems/passenger-3.0.11;
passenger_ruby /Users/lanvige/.rvm/wrappers/ruby-1.9.3-p0@rails/ruby;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
# Include virtual host configurations
include lanvige.conf;
}

####There are a few things to note:

The first line set which user should nginx run as.

passenger_root to point the passenger path, you can run below line to get the path:

1
$ passenger-config --root

passenger_ruby, the first line set nginx run as staff, not current user, so nginx will can’t find the rails because we never added the rvm to staff’s shell environment, but we have wrapper, it will set ruby/rails/… ‘s path to shell.

include, It’s a best practice to split every site’s config to a single file then combine with include.

####Site Config File

It’s a minest config file with a rails site.

1
2
3
4
5
6
7
8
9
#gzip on;
server {
server_name lanvige.local;
listen 8080;
root /Users/lanvige/repos/portal/public;
passenger_enabled on;
rails_env production;
charset utf-8;
}

listen 8080, we just can use the number bit than 1024 as port to listen, because brew always use current as owner to run the application.

root, the root point to the public folder of your rails application.

rails_evn, this config what environment will run as.

Check the Config file with -t parameters.

1
$ /usr/local/Cellar/nginx/1.0.11/sbin/nginx -t

nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful

###Hosts

Add the lanvige.local to your /etc/hosts so the browser can find the site with name.

1
127.0.0.1 lanvige.local

It works

Input http://lanvige.local:8080/ in your browser try to open the rails site, no accidents, browser will show your rails home page normally.

—-

####Upgraded the passenger

If you run gem update passenger to upgrade a new Passenger version, then need do the follow steps to make sure the new passenger works well.

update the passenger_root to new passenger root in Nginx config file.

Rebuild the PassengerWatchdog

1
2
3
$ cd `passenger-config --root`;
$ rvmsudo rake nginx
Restart the nginx
1
2
$ launchctl unload -w ~/Library/LaunchAgents/org.nginx.nginx.plist
$ launchctl load -w ~/Library/LaunchAgents/org.nginx.nginx.plist

The rails application site work again.

###Upgraded the Nginx

I’m not sure the upgrade will add –with-passenger automatically when you run brew upgrade nginx, and you can’t just build a module for nginx, it means if you want add a new module you need rebuild all nginx source.

I’ll check this later.

Update on 21 Dec 2011, It works! use below line to upgrade nginx with passenger.

1
$ brew upgrade nginx –with-passenger

###More about the init port:

Why we can’t use 80 port with nginx installed by brew.
http://trevorturk.com/2010/09/22/passenger-with-nginx-on-mac-os-x-2nd-edition-2/

Notice that the username is samsoffes and not root. Using the root user is considered bad practice by most. (We’ll create that user in the next step.)Note: you need to make your user a superuser for your tests to run correctly. More on this?here.
http://samsoff.es/posts/running-rails-local-development-with-nginx-postgres-and-passenger-with-homebrew

###Install Passenger Manual

http://www.modrails.com/documentation/Users%20guide%20Nginx.html#_installing_phusion_passenger_for_nginx_manually

-EOF-