How to tame nginx to play nice with rvm’s rubies?

It’s a good practice to test your website with nginx within your local rvm environment. This tutorial will show you how to install nginx and make it work with your rvm’s ruby version. My system is OS X 10.6.4 and rvm 0.1.41.

Prerequisites

For this tutorial, I compile and install RVM 0.1.41 from GitHub source. You could find all instructions on the author website.

Ruby 1.8.7 or Ruby Enterprise Edition is the minimum requirement. I highly recommend you go for REE for its optimization.

rvm install ree -C --enable-pthread,--enable-shared

OR

rvm install 1.8.7 -C --enable-pthread,--enable-shared

After above step, you should have:

rvm list

rvm rubies

=> ree-1.8.7-2010.02 [ x86_64 ]
 ruby-1.8.7-p299 [ x86_64 ]

You then could set your default Ruby instance:

rvm use 1.8.7 --default

OR

rvm use ree --default

Installation

First, I grab the latest HEAD version of phusion-passenger. Please make sure you running Ruby 1.8.7 to  be able to build the gem package.

cd /tmp
git clone git://github.com/FooBarWidget/passenger.git
cd passenger

Next, we need some tweak to the passenger-install-nginx-module script to match our custom ruby binary path. Use your favorite text-editor and change line 369 of bin/passenger-install-nginx-module:

nano bin/passenger-install-nginx-module
369: "    passenger_ruby #{File.exists?(File.join(ENV['HOME'],'.rvm/bin/passenger_ruby')) ? File.join(ENV['HOME'],'.rvm/bin/passenger_ruby') : RUBY};\n")

OPTIONAL: At the time of the writing, phusion-passenger builds nginx 0.7.67 and pcre 8.02 by default. You could change the source to use version 8.10 of pcre by changing line 153, 152 of bin/passenger-install-nginx-module:

nano bin/passenger-install-nginx-module
152: basename = "pcre-8.10.tar.gz"
153: dirname = "pcre-8.10"

We build the gem package and install passenger

rake package:gem
gem install pkg/passenger-2.2.15.gem

Then we need to set up the environment settings for passenger, rvm make it easy with 1 command:

rvm 1.8.7 --passenger

OR

rvm ree --passenger

What this does is create symlinks to your Ruby binary within .rvm/bin. If you miss this step, the setup WILL NOT work.

Execute the installation script to build nginx passenger module and chose option 1

rvmsudo passenger-install-nginx-module

when asked for location to install it to, leave it default to /opt/nginx

Once the installation is completed, the installer will tell you to append 2 lines of code into /opt/nginx/nginx.conf, In my case, I use REE so you would see 2 lines referenced to REE.

  http {
      ...
      passenger_root /Users/MAC/.rvm/gems/ree-1.8.7-2010.02/gems/passenger-2.2.15;
      passenger_ruby /Users/MAC/.rvm/rubies/ree-1.8.7-2010.02/bin/ruby;
      ...
  }

Testing

Create a testing Rails site, rails 2.3.8 all runs with this setup

cd ~/Sites
rails example

add a vhost to your nginx.conf

	
server {
	listen 2000;
	server_name localhost;
	root /Users/MAC/Sites/example/public;
	passenger_enabled on;
	rails_env development;
}

Then start nginx

sudo /opt/nginx/sbin/nginx

then use your favorite browser to check http://0.0.0.0:2000 and you see if it works for you. If something goes wrong, make sure you uncomment line ‘error_log logs/error.log;’ in nginx.conf and check your nginx/logs/error.log for detail.

Have fun guys.

Advertisements

About Jones Lee

Nothing much about me..

3 responses to “How to tame nginx to play nice with rvm’s rubies?

  1. mike

    When I run rake package:gem I get this error:


    rake aborted!
    could not find template "./doc/template/horo"

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: