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


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!

is it possible to deploy by using git to push the rails app to the server and then use the same nginx conf with passenger

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

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

Good article

This doesn’t really work… This is titled as deploying a rails app, but really this tutorial explains how to set up a server. How do I create a database and run migrations? I followed this tutorial exactly as instructed and I got to a point where I get a big error screen that says “We’re sorry, but something went wrong.” It’s a Phusion Passenger error.

Are there any updated digital ocean articles that explain how to deploy a rails app? Every article I’ve found is outdated. They use things like Ubuntu 14.04 instead of 18.04 LTS. And they all deal with Rails 4 instead of Rails 5.

Digital Ocean is advertised as being the simplest platform to use, but with Rails 5 applications, it seems very complex. Maybe I’m doing something wrong? I don’t really see the benefit of using something like Digital Ocean over AWS EC2 for deployment at this point.

If you’re having problems installing these using Ubuntu 16, I highly suggest taking a look at passenger’s own guide on this here: https://www.phusionpassenger.com/library/install/nginx/install/oss/xenial/

It was the only way it worked for me, as I kept running into issues on installing nginx-extras

I hope this helps anyone who was stuck like me!

404 Error

hello, I installed this solution, but I have one problem, it is reverse proxy and my rails app is hidden behind passenger, how can I PASS IP TO RAILS, my access log looks like this:

127.0.0.1 - - [30/Aug/2017:19:24:16 +0200] "GET / HTTP/1.1" 200 93084 "-" "Zabbix"
127.0.0.1 - - [30/Aug/2017:19:25:05 +0200] "GET /buy HTTP/1.1" 200 29430 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"
127.0.0.1 - - [30/Aug/2017:19:25:31 +0200] "GET / HTTP/1.1" 200 93104 "-" "Zabbix"

and sites-enabled/default

server {
        listen 127.0.0.1:8080 default_server;
        #listen [::]:8080 default_server ipv6only=on;

        server_name www.name.com;
        passenger_enabled on;
        rails_env    production;
        root         /home/andy/silentel/current/public;

        set_real_ip_from 127.0.0.1;
        real_ip_header X-Real-IP;
        real_ip_recursive on;

        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

how can I set set_real_ip and ip forwarding correctly?

I’ve followed all the steps, using my existing rails app pulled from Github, and all I get when I load my site is a blank page with “Welcome to nginx!” as the tab title

Regards Community,

But only one thing, where I can find the ngnix source file to build rtmp_module? such as describe [https://www.atlantic.net/community/howto/install-rtmp-ubuntu-14-04/ ]

cd nginx-1.8.0
./configure --add-module=../nginx-rtmp-module-master

Thank you a lot

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!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

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

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more