Saturday, 21 April 2012

Deploying Rails App with Passenger and NGINX

All the ROR begineer every wonder about how the so called ROR expert set and deploy there application in no time. Feeling sorry don't be because after this post you dont have too.

First of all you may like to pause a minute or so and decide the which server you want to use (nginx , apache) depending on your need.

We would be using the awesome nginx server for out post but dont worry if you have apache in your stack hooking up the same is not much different from nginx.

You can download nginx from here or build the nginx based upon distributor package

For Ubuntu it fairly straight forward
sudo apt-get install nginx
(Note: please check your distribution provider for downloading)

but I recommend you to download the nginx source code as one can customized the nginx while configure it with passenger.

Phusion Passenger - a.k.a. mod_rails or mod_rack -- makes deployment of Ruby web application, such as those built on revolutionary Ruby on Rails web framework

Next we can confidently go ahead and install the phusion-passenger gem using
gem install passenger
Once passenger install

Now it time to go ahead and install configure your nginx to work with passenger just go ahead and ran the below command on your favorite shell
passenger-install-nginx-module
(if you have source code of nginx point it when asked by passenger during the above process) 

Now passenger will popup a customized configuration depending upon your environment

Here what passenger displayed me.

Please edit your Nginx me configuration file (probably /opt/nginx/conf/nginx.conf ) ,
  http {
    ...
    passenger_root  /usr/lib/ruby/gems/1.8/gems/passenger-3.0.11;
    passenger_ruby /usr/lib/ruby1.8;
    ...
   }
   server {
     listen 80;
     server_name www.yourhost.com;
     root /somewhere/public;   # <--- be sure to point to 'public'!
     passenger_enabled on;
   }
Copy the above content in your nginx.config
(dont worry if your unaware where is your nginx.conf file located passenger will come to rescue in that too)
 as it would be required you to set up your application now replace 'somewhere' from the above with absolute path for your application

Here how the final configuration look like
worker_processes  1;
events {
  worker_connections  1024;
}

http {
  passenger_root /usr/lib/ruby/gems/1.8/gems/passenger-3.0.11;
  passenger_ruby /usr/bin/ruby1.8;
  include       mime.types;
  default_type  application/octet-stream;
  sendfile        on;
  keepalive_timeout  65;
  server {
    listen       80;
    server_name  myapplication.com;
    root /home/root/application/public;
    passenger_use_global_queue on;
    passenger_enabled on;
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
      root   html;
    }
  } 
}
Set you Database environment and restart the nginx server
/etc/init.d/nginx restart
(you might want to run this as sudo)

Voila, that it congratulation you have successfully deployed your rails application

Go ahead pat yourself in back for this. :) :) :)

Now enter your application url in your favourite browser you should find it working.
If you can't find it or you land up in error page check nginx error.log
(can be found in nginx.log file) or your application log file for more information it could probably
because you would be missing certain gem or you just missed something from the above list

Hope it help you in someway or other.

Thanks You.

No comments:

Post a Comment

What did I learn today?

Welcome to the what did I learn today series. The intention of this blog spot is to compose the stuff that I learnt day-to-day basics and jo...