Tutorial

How To Install Rails and nginx with Passenger on Ubuntu

Published on June 2, 2012
How To Install Rails and nginx with Passenger on Ubuntu

Introduction

Ruby on Rails is an application stack that provides web developers with a framework to quickly create a variety of web applications, and nginx is a light, high performance web server software. The two programs can be easily configured to work very well together on a virtual private server when installed through Phusion Passenger.

You can run this tutorial on your VPS as a user with sudo privileges. You can check out how to set that up here: Ubuntu Server Setup

Step One— Install Ruby with RVM

Before we do anything else, we should run a quick update to make sure that all of the packages we download to our virtual server are up to date:

sudo apt-get update

Once that's done, we can start installing RVM, Ruby Version Manage, on our VPSr. This is a great program that lets you use several versions of Ruby on one system; however, in this case, we will just use it to install the latest version of Ruby on the droplet.

To install RVM, open terminal and type in this command:

curl -L get.rvm.io | bash -s stable

After it is done installing, load RVM.

source ~/.rvm/scripts/rvm

In order to work, RVM has some of its own dependancies that need to be installed. You can see what these are:

rvm requirements

In the text that RVM shows you, look for this paragraph.

Additional Dependencies:
# For Ruby / Ruby HEAD (MRI, Rubinius, & REE), install the following:
  ruby: /usr/bin/apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion

Just follow the instructions to get your system up to date with all of the required dependancies.

rvmsudo /usr/bin/apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion

Step Two—Install Ruby

Once you are using RVM, installing Ruby is easy.

rvm install 1.9.3

Ruby is now installed. However, since we accessed it through a program that has a variety of Ruby versions, we need to tell the system to use 1.9.3 by default.

rvm use 1.9.3 --default

Step Three—Install RubyGems

The next step makes sure that we have all the required components of Ruby on Rails. We can continue to use RVM to install gems; type this line into terminal.

 rvm rubygems current

Step Four—Install Rails

Once everything is set up, it is time to install Rails.

To start, open terminal and type in:

gem install rails

This process may take a while, be patient with it. Once it finishes you will have Ruby on Rails installed on your virtual server.

Once that’s done you are all set with Ruby on Rails, and it is time to connect it to nginx

Step Five—Install Passenger

Passenger is an effective and easy way to deploy Rails on nginx or apache. In this instance, we are going to run the nginx installation.

Once Ruby on Rails is installed, go ahead and install passenger.

gem install passenger 

Step Six—Install nginx

Here is where Passenger really shines. As we are looking to install Rails on an nginx server, we only need to enter one more line into terminal:

rvmsudo passenger-install-nginx-module

And now Passenger takes over.

Passenger first checks that all of the dependancies it needs to work are installed. If you are missing any, Passenger will let you know how to install them, either with the apt-get installer on Ubuntu.

After you download any missing dependancies, restart the installation. Type: passenger-install-nginx-module once more into the command line.

Passenger offers users the choice between an automated setup or a customized one. Press 1 and enter to choose the recommended, easy, installation.

Step Seven—Start nginx

Passenger will take about five to ten minutes to install, configure, and optimize nginx with Ruby on Rails.

After it finishes, it will let you know about changes made to the nginx configuration file and how to deploy a Ruby on Rails application on your virtual server.

The last step is to turn start nginx, as it does not do so automatically.

 sudo service nginx start 

nginx is now on. You can see the exciting “Welcome to nginx” screen in your browser if you point it toward http://youripaddress/

Step Eight—Connect Nginx to Your Rails Project

Once you have rails installed, open up the nginx config file

sudo nano /opt/nginx/conf/nginx.conf

Set the root to the public directory of your new rails project.

Your config should then look something like this:

server { 
listen 80; 
server_name example.com; 
passenger_enabled on; 
root /var/www/my_awesome_rails_app/public; 
}

(*NB—to create your new rails project, follow these steps:
  • Install NodeJs if you do not yet have it:
    sudo apt-get install nodejs
  • Create your new rails app in your preferred directory:
    rails new my_awesome_rails_app

By Etel Sverdlov

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Just ran through this setup a couple of things were different for me as of July 20, 2012 or maybe I did something wrong… but any who.

To load RVM I had to use this command: source /usr/local/rvm/scripts/rvm

To start nginx I used all the defaults when letting passenger set things up and found nginx here: /opt/nginx/sbin/nginx

The passenger install tells you to edit the nginx.conf which is located here: /opt/nginx/conf/nginx.conf

Thanks for the update! It looks like you might have been going for a system wide RVM install rather than a local one, which is what is described in the steps above.

If you are installing on a single user, the steps above should work.

Also-- thank you for pointing out the nginx configuration typo—the tutorial now says nginx, which is accessible, as you pointed out at /opt/nginx/conf/nginx.conf.

Also can use this script for init.d service wget -O init-deb.sh http://library.linode.com/assets/660-init-deb.sh sudo mv init-deb.sh /etc/init.d/nginx sudo chmod +x /etc/init.d/nginx sudo /usr/sbin/update-rc.d -f nginx defaults

after that you can control nginx with sudo /etc/init.d/nginx stop sudo /etc/init.d/nginx start sudo /etc/init.d/nginx restart

Etel Sverdlov
DigitalOcean Employee
DigitalOcean Employee badge
September 19, 2012

When installing Nginx from the Ubuntu repositories, the package already comes with its own init script. You should not need to install any additional scripts or software from any other providers to make it work.

As a note, the smallest droplet with 256mb RAM will cause g++ to fail during nginx compilation.

To fix this, resize to the 512mb instance.

Also, as a note, that when following the instructions above - as a second user on the droplet - I ran into the same issue spavbg did. Follow his instructions if you have already created an additional user and are logged in as that in the system.

Is there a way to compile nginx on a 256mb droplet?

Etel Sverdlov
DigitalOcean Employee
DigitalOcean Employee badge
October 4, 2012

I was able to run these steps on a 256MB droplet.

However, an alternative is to resize a droplet to 512MB, go through the steps, and then resize back down to a 256MB droplet—this will let you perform the steps without issue and incur almost no additional cost (at most about $.02)

If you have problems with memory size, you can set up a swap space.

I had to load RVM using this:

source /etc/profile.d/rvm.sh

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.