Deploying Rails with Phusion Passenger and Nginx in 5 minutes

When I first started with RoR, I used to wonder how developers deployed their application with such ease. Here’s a guide which should come in handy if you want to deploy a Rails app with Nginx, without making the whole process look tedious. I could get this done in 5 minutes, how about you?

Phusion Passenger / mod_rails

Phusion Passenger is a robust module for Apache and Nginx servers, and is preferred over any other module for deploying web apps developed using the Rails framework. But if you’re running a Microsoft Windows platform, you might have to look for other alternatives since Phusion Passenger is compatible with anything but Windows. For this tutorial, I’ll be using Ubuntu, but you can go anything unix-like.

Choose between nginx and Apache

Apache and Nginx are widely adopted web servers. Although Apache is the best known process based web server, Nginx is most suited if you’re in need of a fast, and responsive server. It performs miracles when the server is under heavy load.

For this post I’ll be relying on Nginx server, but if you have Apache in your stack, the method of hooking up is not much different from Nginx.

Deploying Rails App with Phusion Passenger and Nginx

First, download the latest stable version of Nginx from nginx.org. You will need its source code to install the Nginx module later. I’ll be using nginx-1.0.15 which is the latest stable release at the time of writing this tutorial.

cd /tmp
wget http://nginx.org/download/nginx-1.0.15.tar.gz
tar -xvzf nginx-1.0.15.tar.gz

Assuming that you have Ruby, Rails, and RubyGems installed, enter the following code to install Phusion passenger gem.

gem install passenger

Now you’ll have to configure your Nginx to work with Passenger. Run the following command in your favorite shell.

passenger-install-nginx-module

The interactive installer will guide you through and you will have a completely functional Nginx server in just a few minutes. I’ve found the installer rather fun to use. If anything goes wrong, you’ll be notified about it, and the installer will display the probable cause and also the solution to overcome it. What more should you expect?

When prompted, go for the custom Nginx installation method (option 2). This will be to your advantage in the long run and if you’d like to add some additional parameters to Nginx’s config script, this is the perfect choice. For example, if your application requires SSL support and you have OpenSSL source code lying in your server, you can painlessly compile them together by passing a parameter to the configuration file.

When prompted, specify the location of the nginx source code which we downloaded earlier. In our case, it is /tmp/nginx-1.0.15. The next few steps are self explanatory.

If everything goes well, you should see something along the lines “Nginx with Passenger support was successfully installed.” The installer might edit your Nginx configuration file in order to add some snippet of code, or you might have to do it manually. Either way, you’ll be notified about it.

I was asked to configure it myself and here is what it prompted me to do. (The code given below may vary from user to user).

Please edit your Nginx 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; #
passenger_enabled on;
}

Copy the above content into your nginx.config and replace ‘somewhere’ (mentioned above) with the absolute path to your application.

Here’s what (my) final configuration looks 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 your Database environment, and restart nginx server using the following

sudo /etc/init.d/nginx restart


Voilà! That’s it and congratulations. You have successfully deployed your rails application.

By entering your application URL in your browser and hopefully you should find it working. If you cant see it running or you land on an error page, check Nginx’s error.log (can be found at nginx.log file) or your application log file for more information. It could be because you missed certain gem or certain dependencies were not satisfied.

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • http://www.3web.cl nano

    So you use the nginx source only for compiling the module? what about the nginx you’re going to be using? is it an apt-get install nginx? or a cd /tmp/nginx-1.0.15 && make && make install ?

  • http://www.techmixer.com/ Chris Howart

    @nano;

    “passenger-install-nginx-module” will install the nginx module (using the source). You will be using that module for your web server.

    Hope that helps :)

  • Halil Özgür

    How would you compare this to Heroku, or in general, cloud services to custom deploys?