Tutorial

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

Published on November 21, 2014
How To Deploy a Rails App with Passenger and Apache on Ubuntu 14.04

Introduction

In this tutorial, we will demonstrate how to install Phusion Passenger as your Rails-friendly web server, which is easy to install, configure, and maintain. We will integrate it into Apache on Ubuntu 14.04. By the end of this tutorial, we will have a test Rails application deployed on our Droplet.

If you prefer Nginx over Apache, take a look at how to deploy a Rails app with Passenger and Nginx on Ubuntu 14.04 by following the link.

Prerequisites

The first step is to create a new Droplet. For smaller sites it is enough to use the 512 MB plan.

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 consider the 64-bit version.

Be sure to use Ubuntu 14.04. At the time of this writing, Ubuntu 14.10 does not have a Passanger APT repository yet. Moreover, Ubuntu 14.04 has an additional benefit: it’s a LTS version, which stands for “long term support.” LTS releases are designed to be stable platforms that we can stick with for a long time. Ubuntu guarantees LTS releases will receive security updates and other bug fixes for five years.

  • Ubuntu 14.04 32-bit Droplet

Step 1 — Add a Sudo User

After the Droplet is created, you should create a system user and secure the server. You can do so by following the Initial Server Setup article.

If you want to follow this tutorial, you need 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 2 (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 3 — 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 libraries and other dependencies. This will 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 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.4. You can get the current latest version from the 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.4.tar.gz

Decompress the downloaded file:

tar -xzf ruby-2.1.4.tar.gz

Select the extracted directory:

cd ruby-2.1.4

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:

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

If your Ruby installation was successful, you should see output like the following:

ruby 2.1.4p265 (2014-10-27 revision 48166) [x86_64-linux]

Finally, we can delete the temporary folder:

rm -rf ~/ruby

Step 4 — Install Apache

To install Apache, type this command:

sudo apt-get install apache2

Yes, that’s all!

Step 5 — Install Passenger

First, install the PGP key for the repository server:

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

Create an APT source file:

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

Insert the following line to add the Passenger repository to 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 to restrict access to root:

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:

sudo apt-get install libapache2-mod-passenger

Make sure the Passenger Apache module; it maybe enabled already:

sudo a2enmod passenger

Restart Apache:

sudo service apache2 restart

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 6 — 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/apache2/sites-available/default step.

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

Move to your user’s home directory:

cd ~

Install the rails gem without extra documentation, which makes the installation 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 update the paths in the other commands and files in this section.

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, first open the Gemfile:

nano Gemfile

Find the following line:

# gem 'therubyracer',  platforms: :ruby

Uncomment it:

gem 'therubyracer',  platforms: :ruby

Save the file, and run Bundler:

bundle install

Now, we need to create a virtual host file for our project. We’ll do this by copying the default Apache virtual host:

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/testapp.conf

Open the config file:

sudo nano /etc/apache2/sites-available/testapp.conf

Edit it or replace the existing contents so your final result matches the file shown below. Changes you need to make are highlighted in red. Remember to use your own domain name, and the correct path to your Rails app:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin webmaster@localhost
    DocumentRoot /home/rails/testapp/public
    RailsEnv development
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    <Directory "/home/rails/testapp/public">
        Options FollowSymLinks
        Require all granted
    </Directory>
</VirtualHost>

Basically, this file enables listening to our domain name on port 80, sets an alias for the www subdomain, sets the mail address of our server administrator, sets the root directory for the public directory of our new project, and allows access to our site. You can learn more about Apache virtual hosts by following the link.

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 RailsEnv option. If your app is ready for production you’ll want to leave this setting out.

If you don’t want to assign your domain to this app, you can skip the ServerName and ServerAlias lines, or use your IP address.

Save the file (CTRL+X, Y, ENTER).

Disable the default site, enable your new site, and restart Apache:

sudo a2dissite 000-default
sudo a2ensite testapp
sudo service apache2 restart

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

http://droplet_ip_address

Verify that your app is deployed. You should see either your custom application, or the Welcome aboard default Rails page:

Test page

The Rails app is now live on your server.

Step 7 — Update Regularly

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

To update Passenger and Apache, 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 symlink to the Ruby binary file and creating a new (correct) one. They are listed at the end of Step 6 in this tutorial.

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

sudo service apache2 restart

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

Learn more about us


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!

If your Apache & Phusion Passenger setup was broken by the Phusion Passenger version 5 release, note that a new apt repository has been published that provides access to Phusion Passenger 4 libraries. See: Passenger 4 APT repository now available

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

There was a passenger update today and after updating my servers, it broke the setup. I can confirm they were a couple of updates from passenger.

I have setup a new server to test this same setup with a rails app (Project Fedena) and after doing exactly as shown in this guide, I come up with an error: AH01630: client denied by server configuration: /srv/fedena/public/

I believe this is related to the absence of an index file in the /app/public directory. Any ideas?

The Phusion Passenger application server tried to start the web application. But the application itself (and not Passenger) encountered an internal error.

Error details:

cannot load such file – bundler/setup (LoadError) /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in require' /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in require’ /usr/lib/ruby/vendor_ruby/phusion_passenger/loader_shared_helpers.rb:363:in activate_gem' /usr/lib/ruby/vendor_ruby/phusion_passenger/loader_shared_helpers.rb:221:in block in run_load_path_setup_code’ /usr/lib/ruby/vendor_ruby/phusion_passenger/loader_shared_helpers.rb:527:in running_bundler' /usr/lib/ruby/vendor_ruby/phusion_passenger/loader_shared_helpers.rb:220:in run_load_path_setup_code’ /usr/share/passenger/helper-scripts/rack-preloader.rb:91:in preload_app' /usr/share/passenger/helper-scripts/rack-preloader.rb:189:in block in module:App’ /usr/lib/ruby/vendor_ruby/phusion_passenger/loader_shared_helpers.rb:380:in run_block_and_record_step_progress' /usr/share/passenger/helper-scripts/rack-preloader.rb:188:in module:App’ /usr/share/passenger/helper-scripts/rack-preloader.rb:30:in <module:PhusionPassenger>' /usr/share/passenger/helper-scripts/rack-preloader.rb:29:in <main>’ The stdout/stderr output of the subprocess so far is:

Error: The application encountered the following error: cannot load such file – bundler/setup (LoadError) /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in require' /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in require’ /usr/lib/ruby/vendor_ruby/phusion_passenger/loader_shared_helpers.rb:363:in activate_gem' /usr/lib/ruby/vendor_ruby/phusion_passenger/loader_shared_helpers.rb:221:in block in run_load_path_setup_code’ /usr/lib/ruby/vendor_ruby/phusion_passenger/loader_shared_helpers.rb:527:in running_bundler' /usr/lib/ruby/vendor_ruby/phusion_passenger/loader_shared_helpers.rb:220:in run_load_path_setup_code’ /usr/share/passenger/helper-scripts/rack-preloader.rb:91:in preload_app' /usr/share/passenger/helper-scripts/rack-preloader.rb:189:in block in module:App’ /usr/lib/ruby/vendor_ruby/phusion_passenger/loader_shared_helpers.rb:380:in run_block_and_record_step_progress' /usr/share/passenger/helper-scripts/rack-preloader.rb:188:in module:App’ /usr/share/passenger/helper-scripts/rack-preloader.rb:30:in <module:PhusionPassenger>' /usr/share/passenger/helper-scripts/rack-preloader.rb:29:in <main>’

after doing all the step it gives me Forbidden You don’t have permission to access / on this server. 403

Thank for sharing.

When I am trying to hit ‘sudo service apache2 restart’, getting error as “AH00112: Warning: DocumentRoot [/home/rails/testapp/public] does not exist”

Please suggest proper way.

I have tried RAILS_ENV=production rake assets:precompile and restarted the server, but the CSS seems to be loading from localhost instead of the server. How can we configure the asset pipeline to work in production?

I already have rails app and when I was upload this “Incomplete response received from application” message appear, why?, and how to fix it ?

Same issue as here. 403 Forbidden.

StackOverFlow changed my virtual host- Apache 2.4

<VirtualHost *:80> ServerName www.mydomain.com PassengerMaxPoolSize 4 ServerAdmin webmaster@localhost DocumentRoot /home/rails/testapp/public PassengerRuby /usr/local/bin/ruby <Directory /home/rails/testapp/public> Require all granted Options FollowSymLinks </Directory> </VirtualHost>

============================================= apache error log

AH01630: client denied by server configuration: /home/rails ===================UPDATE=================== OK, app location was /home/david/testapp. changed that in apache2 conf file and now I am getting.

Incomplete response received from application,

but it is serving something. Rails 5.0.0.1, ruby 2.3.2p217, Ubuntu 16.10

Everything works, including production now. But how do I get my actual domain name to point to this instead of only having access via the direct IP address?

I am trying to install the PGP key for the repository server using this command: sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 561F9B9CAC40B2F7

I am getting an error that it has failed, no such file or directory. Can someone please tell me how I can resolve this. Regards, Jennifer Flynn

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!

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
DigitalOcean Cloud Control Panel