CMPT 470, Fall 2012

Deploying Rails

This information is provided to help you get (possibly several) Rails applications running on your group's web server. The setup I have suggested is Apache 2 with Passenger. That seems to be the best way to do things in the modern world.

Installation and Setup

First of course, you need the relevant software installed. Since Rails 3 isn't yet packaged in Ubuntu, it's easiest to install it as a Gem:

sudo apt-get install rake rubygems apache2-mpm-prefork libmysqlclient-dev ruby-dev
sudo apt-get install build-essential libssl-dev zlib1g-dev libcurl4-openssl-dev zlib1g-dev apache2-prefork-dev libapr1-dev libaprutil1-dev
sudo gem install rails mysql2 passenger
sudo /var/lib/gems/*/bin/passenger-install-apache2-module

If you haven't already, you might need to install the MySQL server as well: sudo apt-get install mysql-server

I'm going to assume a Rails application in the directory /home/userid/railsapp/ that will be accessed at http://server/mysite/. Start by creating a symbolic link so the Passenger module can find your code (since it doesn't understand Apache aliases):

sudo ln -s /home/userid/railsapp/ /var/www/mysite

To set up the Passenger module and turn on Rails for this URL, edit /etc/apache2/sites-enabled/000-default and at the bottom of the <Virtualhost> section (right before the “</Virtualhost>”), add this:

LoadModule passenger_module /var/lib/gems/1.8/gems/passenger-3.0.0/ext/apache2/mod_passenger.so
PassengerRoot /var/lib/gems/1.8/gems/passenger-3.0.0
PassengerRuby /usr/bin/ruby1.8
RailsEnv production
RailsBaseURI /mysite

(Substituting version numbers as necessary: the passenger-install-apache2-module command gives the proper version of the first three lines.) For additional Rails apps, only the last line needs to be repeated.

App Configuration

In your app's Gemfile, comment out the line that requires the SQLite Gem, and add a requirement for the MySQL Gem:

# gem 'sqlite3'
gem 'mysql2'

You probably need to modify your config/database.yml file so that your app uses a MySQL database in production mode, rather than the SQLite database you are using for development. See the MySQL database instructions for info on creating a database.

Once you have created a database, put the connection info in config/database.yml in the production section like this:

production:
  adapter: mysql2
  encoding: utf8
  database: myapp
  username: appuser
  password: secretpassword

When you are working with the database at the command line, you have to be sure to specify production mode:

rake db:create RAILS_ENV="production"
rake db:migrate RAILS_ENV="production"

Unless you want to manually compile your assets each time you deploy (bundle exec rake assets:precompile), you'll need to enable automatic compilation by editing config/environments/production.rb to set this variable appropriately:

config.assets.compile = true

Working

You should be able to work on the Rails application more-or-less without worrying about the setup.

If you make changes to the database, or code, or otherwise want to restart the app's process, the easiest and most surefire way is to restart Apache entirely:

sudo /etc/init.d/apache2 restart

Rails deployment instructions and the Passenger + Apache user's guide


Copyright © , based on content created by Greg Baker.