Installing nginx with passenger and RVM

In this post I am going to tell you how to install the nginx server with passenger to host rails applications.

Following are my configurations:

OS: Ubuntu 11.10

Ruby: 1.8.7

I am using RVM

Don�t install nginx with sudo apt-get install nginx command, I face lot of problems with it..

First install Ruby 1.8.7 with RVM

Next install passenger which ever version you want, I prefer latest.

gem install passenger

The above command will install passenger for your system, now we can install either apache2 or nginx as HTTP servers. I am installing nginx here.

After installing passenger you need to run the following command

rvmsudo passenger-install-nginx-module
This is important step while installing, Don�t run above command with sudo or without rvmsudo it won�t work as expected..

The script will ask you if you want a default installation or a custom/advanced on.


I just did the default (option 1).


The script downloads and compiles nginx. It will ask you where you want it installed. I suggest /usr/local/nginx.


Don't just say /usr/local as that will create conf, html and logs directory right in /usr/local. You really want these in a nginx specific directory.


All being well the compilation and installation will go smoothly and your fresh installation of nginx will be modified for use with passenger.
If I remember the installation correctly it actually fires up nginx and leaves it running.


Check that with 'ps ax | grep nginx'


Go to your browser.
'http://localhost' should give a 'welcome to nginx' page. Look in /usr/local/nginx/logs/ for logging output.


Configuration
I set up a symlink in /usr/local/sbin to the nginx binary so that it can be found in my regular PATH

sudo ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx

You start nginx with 'sudo nginx' and shut it down with 'sudo killall nginx'. It needs to be run via sudo.
Configuring nginx is done in /usr/local/nginx/conf/nginx.conf and if you are used to Apache config files this will be a breath of fresh air. The main changes I made from the default was to set myself as the 'user' and set the worker_processes to 2.
To set up a Rails application simply add a server block like this:
server {
listen 80;
server_name myapp.rvg.com;
root /Users/rvg/projects/test/public;
passenger_enabled on;
rails_env development;
}
Be sure to set the rails_env unless you are in production (the default) otherwise it will not work. 
Restart nginx and with any luck you'll be able to access your application.
I made a few missteps doing my installation - such as thinking I needed to install nginx myself - but overall this went very smoothly.