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!

It’s not working for me.I have few queries: 1.Does nginx automatically start passenger?

2.Some tutorials like https://medium.com/@samx18/deploy-a-rails-app-locally-on-os-x-using-passenger-with-nginx-in-under-5-minutes-be0381e90f5f have mentioned to start passenger not nginx.

I have checked all the ways.Nothing is working for me.

Using your way ,server is starting on port 80(default),webpage is displaying “We’re sorry, but something went wrong.”.And in nginx error log it’s showing “2015–08–01 23:57:17.5349 60403/0x10fce0000 App/Implementation.cpp:303 ]: Could not spawn process for application /private/var/www/app_name: An error occured while starting up the preloader.”

Using the second way,server is starting on port 3000(default) and webpage is displaying ‘404 Not Found:nginx/1.8.0’.

I have checked all the permissions stuff.Nothing worked.Please help.

I can’t get Rails to read my environment variables (or Passenger for that matter when I start the app manually).

I have an environment variable like this in ~/.bashrc: export EMAIL_USERNAME=bob@gmail.com

And then in nginx I have: env EMAIL_USERNAME;

Unfortunately this does not get passed on to the app when it’s running (but it’s fine when I do it from the rails console). Anyone else having this issue?

I found following the instructions provided by Phusion Passenger to be less problematic.

My Droplet is 516MB and I get a memory error from rvmsudo passenger-install-nginx-module. I tried adding Swap… I am thinking maybe passenger’s nginx install is too heavy for smallest Droplet?

sudo swapon -s Filename Type Size Used Priority /swapfile file 262140 0 -1

rvmsudo passenger-install-nginx-module

++: internal compiler error: Killed (program cc1plus) Please submit a full bug report, with preprocessed source if appropriate. See <file:///usr/share/doc/gcc-4.8/README.Bugs> for instructions. rake aborted!

Your compiler failed with the exit status 4. This probably means that it ran out of memory. To solve this problem, try increasing your swap space: https://www.digitalocean.com/community/articles/how-to-add-swap-on-ubuntu-12-04

Using “rvmsudo passenger-install-nginx-module” does not come with an init script.

Answer found on http://askubuntu.com/questions/257108/trying-to-start-nginx-on-vps-i-get-nginx-unrecognized-service

Between step 6 and 7 I had to:

The nginx: unrecognized service error means the startup scripts need to be created. Fortunately the startup scripts have already been written.

We can fetch them with wget and set them up following these steps:

Download nginx startup script

wget -O init-deb.sh http://library.linode.com/assets/660-init-deb.sh

Move the script to the init.d directory & make executable

sudo mv init-deb.sh /etc/init.d/nginx sudo chmod +x /etc/init.d/nginx

Add nginx to the system startup

sudo /usr/sbin/update-rc.d -f nginx defaults Now we can control nginx using:

sudo service nginx stop sudo service nginx start sudo service nginx restart sudo service nginx reload

@David, why don’t you write a new article? Since this is the only Ubuntu article I could find on the subject (and painfully outdated).

As it turns out now this I got everything works I was able to try stuff and figure out what my issue really was. it was that the user needed to be set to the user that created the rails app via RVM

The server_name could just needs to be the IP address

and the location needed to be removed or commented out.

So for mer it looks like the thing that real got me was the user needed to be the ubuntu user and not the name of the rails app!

user ruby mine; # this is the user that the rails_app was created with using RVM

server { listen 80; server_name MY-SITE-DNS-NAME.com localhost 100.160.00.000; root /var/www/rails_app/public; passenger_enabled on; …

all I had to do was add the localhost MY_DROPLET_IP; So And it worked!!! please update this in the Tutorial

also how do I delete posts? I want to get rid of the above three!

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
March 8, 2014

@David: Can you pastebin it? It’s too long for a comment and line breaks currently do not work properly. Let me know once you’ve done that so I can delete your older comments.

#user nobody; to user my_rails_app; and add a server entry for the webapp:

server {     listen 80;     server_name www.example.com;     root /var/www/my_rails_app/public; # <— be sure to point to ‘public’!     passenger_enabled on; }

Finally, restart Nginx with the new configuration: 1 /etc/init.d/nginx start

1.	bundle install CD to your rails app and install all dependencies

rake aborted! FATAL: Peer authentication failed for user “drk”

Try to modify your /etc/postgresql/9.1/main/pg_hba.conf file like this, changing the method for local users from peer to trust. t

TYPE DATABASE USER CIDR-ADDRESS METHOD

“local” is for Unix domain socket connections only

local all all md5

IPv4 local connections:

host all all 127.0.0.1/32 md5bundle

IPv6 local connections:

host all all ::1/128 md5

sudo service postgresql restart

  1. rake db:create:all

    1. rake db:migrate RAILS_ENV=production run all migrations in production mode
    2. RAILS_ENV=production bundle exec rake assets:precompile precompile all assets
    3. if you see permission error, run sudo chown -R alex:www-data public to gain ownership over the directory

rubymineprojects@DavidKozikowski:~$ sudo nano /opt/nginx/conf/nginx.conf

5.	sudocd /etc/init.d/nginx start restart nginx

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.