Tutorial

How To Deploy a Rails App with Passenger and Nginx on Ubuntu 14.04

How To Deploy a Rails App with Passenger and Nginx on Ubuntu 14.04

Introduction

If you are a Ruby on Rails developer, you probably need a web server to host your web apps. This tutorial shows you how to use Phusion Passenger as your Rails-friendly web server. Passenger is easy to install, configure, and maintain and it can be used with Nginx or Apache. In this tutorial, we will install Passenger with Nginx on Ubuntu 14.04.

An alternate method to deploy your Rails app is with this 1-Click Rails Installation that uses Nginx with Unicorn, a HTTP server that can handle multiple requests concurrently.

By the end of this tutorial, you will have a test Rails application deployed on your Passenger/Nginx web server and accessible via a domain or IP address.

Step One — Create Your Droplet

Create a new Ubuntu 14.04 Droplet. For smaller sites, it is enough to take the 512 MB plan.

Droplet size

You may want to choose the 32-bit Ubuntu image because of smaller memory consumption (64-bit programs use about 50% more memory then their 32-bit counterparts). However, if you need a bigger machine or there is a chance that you will upgrade to more than 4 GB of RAM, you should choose the 64-bit version.

Droplet image

Step Two — Add a Sudo User

After the Droplet is created, additional system administration work is needed. You should create a system user and secure the server.

Follow the Initial Server Setup article.

In this tutorial, you should create a basic user with sudo privileges. We will use the rails user in this example. If your user has another name, make sure that you use correct paths in the next steps.

Step Three (Optional) — Set Up Your Domain

In order to ensure that your site will be up and visible, you need to set up your DNS records to point your domain name towards your new server. You can find more information on setting up a hostname by following the link.

However, this step is optional, since you can access your site via an IP address.

Step Four — Install Ruby

We will install Ruby manually from source.

Before we do anything else, we should run an update to make sure that all of the packages we want to install are up to date:

sudo apt-get update

Next, install some dependencies. This should make the installation as smooth as possible:

sudo apt-get install build-essential libssl-dev libyaml-dev libreadline-dev openssl curl git-core zlib1g-dev bison libxml2-dev libxslt1-dev libcurl4-openssl-dev nodejs libsqlite3-dev sqlite3

Create a temporary folder for the Ruby source files:

mkdir ~/ruby

Move to the new folder:

cd ~/ruby

Download the latest stable Ruby source code. At the time of this writing, this is version 2.1.3. You can get the current latest version from the Download Ruby website. If a newer version is available, you will need to replace the link in the following command:

wget http://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.3.tar.gz

Decompress the downloaded file:

tar -xzf ruby-2.1.3.tar.gz

Select the extracted directory:

cd ruby-2.1.3

Run the configure script. This will take some time as it checks for dependencies and creates a new Makefile, which will contain steps that need to be taken to compile the code:

./configure

Run the make utility which will use the Makefile to build the executable program. This step can take a bit longer:

make

Now, run the same command with the install parameter. It will try to copy the compiled binaries to the /usr/local/bin folder. This step requires root access to write to this directory. It will also take a bit of time:

sudo make install

Ruby should now be installed on the system. We can check it with the following command, which should print the Ruby version:

ruby -v

Finally, we can delete the temporary folder:

rm -rf ~/ruby

Step Five — Install Passenger and Nginx

The preferred method to install Passenger in the past was using a generic installation via RubyGems (passenger-install-nginx-module).

However, you can now install Passenger on Ubuntu with the Advanced Packaging Tool (APT), which is what we’ll be using. In this manner, the installation and — even more importantly — the update process for Passenger with Nginx, is really simple.

First, install a PGP key:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 561F9B9CAC40B2F7

Create an APT source file (you will need sudo privileges):

sudo nano /etc/apt/sources.list.d/passenger.list

And insert the following line in the file:

deb https://oss-binaries.phusionpassenger.com/apt/passenger trusty main

Press CTRL+x to exit, type y to save the file, and then press ENTER to confirm the file location.

Change the owner and permissions for this file:

sudo chown root: /etc/apt/sources.list.d/passenger.list
sudo chmod 600 /etc/apt/sources.list.d/passenger.list

Update the APT cache:

sudo apt-get update

Finally, install Passenger with Nginx:

sudo apt-get install nginx-extras passenger

This step will overwrite our Ruby version to an older one. To resolve this, simply remove the incorrect Ruby location and create a new symlink to the correct Ruby binary file:

sudo rm /usr/bin/ruby
sudo ln -s /usr/local/bin/ruby /usr/bin/ruby

Step Six — Set Up The Web Server

Open the Nginx configuration file:

sudo nano /etc/nginx/nginx.conf

Find the following lines, in the http block:

# passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
# passenger_ruby /usr/bin/ruby;

Uncomment both of them. Update the path in the passenger_ruby line. They should look like this:

passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /usr/local/bin/ruby;

Save and exit the file.

Step Seven — Deploy

At this point you can deploy your own Rails application if you have one ready. If you want to deploy an existing app, you can upload your project to the server and skip to the /etc/nginx/sites-available/default step.

For this tutorial, we will create a new Rails app directly on the Droplet. We will need a the rails gem to create the new app.

Move to your user’s home directory (otherwise, you will get the error No such file or directory - getcwd) –

cd ~

Install the rails gem (without extra documentation to install it faster). This will still take a few minutes:

sudo gem install --no-rdoc --no-ri rails

Now we can create a new app. In our example, we will use the name testapp. If you want to use another name, make sure you use correct paths. We will skip the Bundler installation because we want to run it manually later.

rails new testapp --skip-bundle

Enter the directory:

cd testapp

Now we need to install a JavaScript execution environment. It can be installed as the therubyracer gem. To install it, open the Gemfile:

nano Gemfile

Find the following line:

# gem 'therubyracer',  platforms: :ruby

And uncomment it:

gem 'therubyracer',  platforms: :ruby

Save the file, and run Bundler:

bundle install

We need to disable the default Nginx configuration. Open the Nginx config file:

sudo nano /etc/nginx/sites-available/default

Find the lines:

listen 80 default_server;
listen [::]:80 default_server ipv6only=on;

Comment them out, like this:

# listen 80 default_server;
# listen [::]:80 default_server ipv6only=on;

Save the file.

Now, create an Nginx configuration file for our app:

sudo nano /etc/nginx/sites-available/testapp

Add the following server block. The settings are explained below.

server {
  listen 80 default_server;
  server_name www.mydomain.com;
  passenger_enabled on;
  passenger_app_env development;
  root /home/rails/testapp/public;
}

In this file, we enable listening on port 80, set your domain name, enable Passenger, and set the root to the public directory of our new project. The root line is the one you’ll want to edit to match the upload location of your Rails app.

If you don’t want to assign your domain to this app, you can skip the server_name line, or use your IP address.

To test our setup, we want to see the Rails Welcome aboard page. However, this works only if the application is started in the development environment. Passenger starts the application in the production environment by default, so we need to change this with the passenger_app_env option. If your app is ready for production you’ll want to leave this setting out.

Save the file (CTRL+x, y, ENTER).

Create a symlink for it:

sudo ln -s /etc/nginx/sites-available/testapp /etc/nginx/sites-enabled/testapp

Restart Nginx:

sudo nginx -s reload

Now your app’s website should be accessible. Navigate to your Droplet’s domain or IP address:

http://droplet_ip_address

And verify the result:

Test page

You should see the Rails test app live on your server.

Step Eight — Update Regularly

To update Ruby, you will need to compile the latest version as shown in Step Four in this tutorial.

To update Passenger with Nginx, you will need to run a basic system update:

sudo apt-get update && sudo apt-get upgrade

However, if there is a new system Ruby version available, it will probably overwrite our Ruby (installed from source). For this reason, you might need to re-run the commands for removing the existing version of Ruby and creating a new symlink to the Ruby binary file. They are listed at the end of Step Five in this tutorial.

After the update process, you will need to restart the web server:

sudo service nginx restart

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
Leave a comment...

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!

Great tutorial ;)

我是中国人 我喜欢这个网站。非常的好

Moderator Edit— Translation: I am Chinese , I like this site . Very good

Nice work.

But since i prefer Apache over Nginx, i found this tutorial very helpful.

In terms of performances though is Nginx better than Apache for this configuration?

Hi,

the tutorial you have mentioned uses the generic installation via RubyGems (passenger-install-apache2-module). However, there is a better way for Apache, too. Check out the official documentation for more info.

Nginx and Apache are different. Apache is a process-based server, while Nginx is an event-based web server. There are some pros and cons for each of them, use Google for more info. For my purposes, I prefer Nginx.

Good article.

I followed along with tutorial however I am getting a 404 not found when I load my ip address in browser?

Try to look at log files:

sudo less /var/log/nginx/error.log
sudo less /var/log/nginx/access.log    
less ~/testapp/log/development.log

How would I serve an app with a postgresql database in production with libraries that utilize imagemagick such as rmagick using this method? I’ve:

 sudo apt-get install imagemagick libmagickwand-dev

also

sudo apt-get install postgresql postgresql-contrib

and set up my users and linked to the users, database name and password in my database.yml file however nginx isn’t serving the app, only its nginx index page

I looked at the logs and got this error

2014/10/23 13:50:00 [error] 6220#0: *1 open() "/home/rails/testapp/public/favicon.ico" failed (2: No such file or directory), client: 50.78.26.158, server: , request: "GET /favicon.ico HTTP/1.1", host: "104.131.24.205"
code

Have you disabled the default Nginx configuration? Have you set passenger_enabled on in your config?

based on this tutorial your testapp is located in /home/{yourusername}/testapp

So, instead of /home/rails/testapp/public, it should be /home/{username}/testapp/public

check your rails app path! cheers

A question about Step 6:

If I have installed ruby 2.1.3 using RVM, what parameter should I give to “passenger_ruby”?

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
October 27, 2014

This comment has been deleted

    You can get the correct path to your ruby with:

    which ruby
    

    Hi guys, I have a weird problem. This is my first experience with running my own VPS. I followed this article last week and I got my rails server up and running…mostly(some problems uploading files to S3 with carrierwave+minimagick…but not the topic of this question). This week I created another droplet with more ram/cores because I was running into memory issues with my app. I get to the step where you install Passenger with Nginx and I keep getting “You have held broken packets”.

    I have destroyed and rebuilt three different droplets following these instructions to the letter and I get the same error. On the third droplet I just copy and pasted the commands into the terminal to make SURE they were exact. Still the same error. Any ideas would be extremely helpful and appreciated.

    For the moment I think ill try the one click unicorn/nginx and try to figure that out.

    Thanks, Josh

    Hi! Which version of Ubuntu have you used? Have you selected the 14.04 (NOT the default 14.10) in the dropdown menu? There is not an APT repository for Passenger for 14.10 now.

    Jkostolansky, Thank you sir! I was on “auto pilot” when recreating the droplets today. I was just choosing the second option in the list which is now 14.10x32 and didn’t realize there was a new version.

    Again, thank you!

    Josh

    You are welcome :) and don’t forget to optimize your passenger and nginx settings (not mentiond in this tutorial) - you can lower your memory requirements and increase the performance at the same time.

    At least take a look at these most affecting settings - for nginx:

    worker_processes
    worker_connections
    

    and for passenger:

    passenger_max_pool_size
    passenger_max_instances_per_app
    passenger_pool_idle_time
    

    I will defiantly look at these settings further. I am completely new to VPS hosting for my rails apps. Ive been using Heroku up untill now and I feel like this article gave me a good foundation to launch the rest of my learning.

    Thanks for writing it!

    Thank you so much for this, I was really struggling and this laid out the steps so well.

    Thanks for a very good guide. I manage to get everything working except when running i get the Passenger error screen with below:

    cannot load such file – bundler/setup (LoadError) /usr/local/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in require' /usr/local/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in require’ /usr/lib/ruby/vendor_ruby/phusion_passenger/loader_shared_helpers.rb:263:in block in run_load_path_setup_code' /usr/lib/ruby/vendor_ruby/phusion_passenger/loader_shared_helpers.rb:366:in running_bundler’ /usr/lib/ruby/vendor_ruby/phusion_passenger/loader_shared_helpers.rb:261:in run_load_path_setup_code' /usr/share/passenger/helper-scripts/rack-preloader.rb:100:in preload_app’ /usr/share/passenger/helper-scripts/rack-preloader.rb:158:in <module:App>' /usr/share/passenger/helper-scripts/rack-preloader.rb:29:in module:PhusionPassenger’ /usr/share/passenger/helper-scripts/rack-preloader.rb:28:in `<main>’

    Error ID 07321e1c

    Any ideas?

    Hi. Did you make any changes in the procedure? Are you trying to create a new Rails app directly on the Droplet (as shown) or to deploy your own app?

    I am trying to deploy my own rails app.

    After following your guide i create a local rails app om my Mac and then i copy it directly to the folder on the server.

    I have setup the NGinx conf file as you described with my own settings: server { listen 80 default_server; server_name www.xxxx.com; passenger_enabled on; passenger_app_env development; root /home/rails/xxxx/public; }

    My plan is that when i get this to work i will make an snapshot and then try to automate it with Capistrano 3.x

    I have discovered that after spending hours on getting the setup to work i would rather spending that time to actually producing something in Rails/Ruby;)

    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.