---
title: "Setting up Rails with Passenger"
date: "2015-01-07"
category: "ruby"
slug: "setting-rails-passenger"
---

## Overview

[Ruby on Rails](http://en.wikipedia.org/wiki/Ruby_on_Rails) is a web application framework built on Ruby. Platform support varies by [[determining-platform-version]]:

- **Pre-v4.5:** Rails 2
- **v4.5 to v5:** Rails 3
- **v6+:** Rails 2-4+ with Ruby 1.8-2.2+ via [rvm](http://rvm.io)

Need a newer platform for Rails 4+? Request a [[migrating-another-server]] by opening a ticket in the [[logging-into-the-control-panel]].

## Rails 2+ on v6+ Platforms

On v6+ [[determining-platform-version]], you can switch between Ruby versions and install multiple Rails versions.

1. Log in to the [[accessing-terminal]].
2. Select a Ruby version with [[changing-ruby-versions]]. This must be run at least once to configure rvm's shim system:
    - `cd /var/www ; rvm use 2.2.2`
3. Install Rails and Passenger:
    - `gem install --no-rdoc --no-ri passenger rails`
    - For Ruby < 2.0: `gem install -y --no-rdoc --no-ri passenger rails`
4. Change to /var/www: `cd /var/www`
5. Initialize a new application: `rails myapp`
6. Configure `PassengerRuby` for your application:
    - Run `passenger-config --ruby-command` to generate the directive
    - Use the _Apache_ directive with _wrappers/_ in the path (not _bin/_), e.g.: `PassengerRuby /.socket/ruby/gems/ruby-2.1.2/wrappers/ruby`
    - **Important:** The wrapper path populates necessary gem environment variables
    - Add the directive to `.htaccess` in `public/` (`/var/www/myapp/public`)
7. Install dependencies:
    - cd /var/www/myapp
        bundle install
        
8. Connect your application to a URL:
    - Go to **Web** > **Subdomains** in the [[logging-into-the-control-panel]]. Create a subdomain called _rails_ with the [[where-is-site-content-served-from]] set to `/var/www/myapp/public`.
        
        ![Adding a Rails subdomain in the CP.](kb-image:Rails-subdomain.png)
*Adding a Rails subdomain in the CP.*
9. Set `secret_key_base` in `config/secrets.yml`. Rails requires a [secret key](http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#config-secrets-yml) for production mode.
10. Access your Rails application.

![Sample install from Rails v3 installed using Ruby 1.8](kb-image:rails-v3-installed.png)
*Sample install from Rails v3 installed using Ruby 1.8*

![Sample install from Rails v4 using Ruby 2.2](kb-image:rails-v4-installed.png)
*Sample install from Rails v4 using Ruby 2.2*

## Rails 2 or 3 on Pre-v6 Platforms

Older platforms support Rails 2 or 3 using mod\_passenger and rubygems.

1. Log in to the [[accessing-terminal]].
2. Install Rails:
    - `gem install -v '< 4.0' --no-rdoc --no-ri -y rails`
    - This installs the latest 3.x release
    - `--no-rdoc` and `--no-ri` omit documentation to reduce storage usage
    - Ruby version is fixed per platform: 1.9.1 (v4.5) or 1.9.3 (v5)
3. Change to /var/www: `cd /var/www`
4. Initialize a new application: `rails myapp`
5. Configure `RailsBaseUri` and `PassengerAppRoot` in `.htaccess` under `public/` (`/var/www/myapp/public`):
    - `RailsBaseUri /`
    - For `PassengerAppRoot`, find your **HTTP Base Path** in the control panel under **Account** > **Summary** > **Web** > **HTTP Base Path**, then append your app path. Example:
        - `PassengerAppRoot /home/virtual/site12/fst/var/www/myapp`
6. Edit `environment.rb` in `myapp/environments/` to load sqlite3:
    
    Rails::Initializer.run do |config|
       # Settings in config/environments/\* take ...
       # Comments ...
     
       config.gem "sqlite3"
     
       # more comments ...
    end
    
7. Connect your application:
    - Go to **Web** > **Subdomains** in the [[logging-into-the-control-panel]]. Create a subdomain called _rails_ with the [[where-is-site-content-served-from]] set to `/var/www/myapp/public`.
        
        ![Adding a Rails subdomain in the CP.](kb-image:Rails-subdomain.png)
*Adding a Rails subdomain in the CP.*
8. Access your Rails application.

## Switching Environments

Rails applications start in production mode by default. To switch to development mode for debugging, add the following to your [[htaccess-guide]] file in the `public/` directory:

`SetEnv RAILS_ENV development`

## Restarting Your App

Passenger manages application restarts:

- **On every request:** `touch tmp/always_restart.txt`
- **One-time restart:** `touch tmp/restart.txt`

Passenger compares the file timestamp with its internal record to determine when to restart. Reissue the `touch` command to trigger subsequent restarts.

## Viewing Launcher Errors

On v6+ platforms, launcher errors are logged to `/var/log/passenger.log`.

## See Also

- [Ruby on Rails Guides](http://guides.rubyonrails.org/index.html)
- [Ruby on Rails Tutorial](https://www.railstutorial.org/book): Learn Web Development with Rails e-book
- [A better way to manage the Rails secret token](http://daniel.fone.net.nz/blog/2013/05/20/a-better-way-to-manage-the-rails-secret-token/)
- Concurrent implementations of Rails [v2](http://rails2.futz.net), [v3](http://rails3.futz.net), and [v4](http://rails4.futz.net) on [Sol](http://apnscp.com/server-lookup?domain=futz.net) (v6 platform)
