Support / KnowledgeBase

 
Search the KnowledgeBase Search

Getting started with Ruby On Rails

  • Applies to: (gs)

  • Difficulty: Medium

  • Time needed: 5 minutes

  • Tools needed: ssh, vi

 

TIP:

This article also appears in our wiki. Help us keep our "Getting started with Ruby On Rails" article updated by joining the wiki. Click on the link to get started.

WARNING:

(mt) Media Temple only supports the basic operation and up-time of our (gc) Grid-Container.  Please consult our Scope of Support for further details.


NOTE:

  • Throughout the course of this document, we will use mt-example.com as a placeholder name. Please make note to replace mt-example.com in the examples with your actual domain name.

History of RoR:

Our (gs) Grid-Service platform includes the added feature of support for Ruby On Rails. This framework is relatively new and differs greatly from PHP and CGI. Over the past year or so, RoR has gained a lot of popularity among web developers. It is definitely a great system to work with as far as development is concerned; however it is very resource hungry.  For this reason we have developed the concept of a 'container' to keep RoR in its own little sandbox environment. This ensures a high performing environment for Ruby on Rails applications to operate inside of, and also ensures that it does not affect the availability or performance of other core services.

This walkthrough will guide you through the process of enabling your Container, setting up the underlying system for Ruby on Rails applications to run, and setting up a basic test application to demonstrate a proper configuration.

Overview of commands used in this tutorial:

  • rails: This command is the standard command-line utility for generating a new Ruby on Rails application.
  • gem: This command is the standard RubyGems command-line utility for managing a RubyGems repository.
  • mtr: This command is specific to the (gs) Grid-Service environment. This tool is your main interface with the (gs) Container add-on. With this tool you can:
    • add and remove applications
    • start, stop and restart applications
    • list and query the status of applications
    • setup your private rubygems install
    • generate the .htaccess configuration to allow the (gs) Grid-Service to proxy your application

NOTE:

Because our (gs) Container environment is slightly different from the (gs) Grid-Service nodes, installing a gem that requires compiling a C extension may not work correctly. We've pre-compiled certain gems to ensure they will work properly in the (gc) Container environment. This list includes:

  • mysql
  • postgres
  • mongrel

In the future, we will be compiling more gems as needed. For the latest list please run the following command:

gem list --remote --source=http://gems.mediatemple.net/

SSH Access:

Log in to your AccountCenter and navigate to the (gs) Grid-Controls for your site. You may do this by clicking on mt-example.com from the 'Domains/Servers' list. Under the section titled 'Server', click on Server Administrator. Toggle the radio button under SSH option to Enabled instead of Disabled. Press the button to save the changes.

Enable the Ruby on Rails Container:

Navigate to: GridControls -> GridContainers -> Ruby on Rails manage button -> Container Status  click on the link titled Your GridContainer is currently active. In this screen, you will see a button titled enable container. Go ahead and click on this button. An animated graphic will start playing to show you that the container is being loaded. This process usually 5-10 minutes, but it may take up to an hour, so please be patient while it loads. Once the page indicates that the container is enabled, you may proceed to the next step.

Logging In:

Now we need to log in through SSH to perform the initial setup of our container:

  • For Windows users on PuTTY: Open the putty program. Set the server name as mt-example.com. When prompted for your username, type serveradmin@mt-example.com. Enter your password.
  • For OS X users, open your Terminal.app and type the following at the shell prompt:
ssh mt-example.com@mt-example.com

Enter your password and you will see a prompt that looks like this:

mt-example.com@cl18:~$

Setup:

NOTE:

To prevent having to type your user/pass for every step, you can use the mtr generate_config command. This can be done at any time (does not have to be done at the beginning). It's a good time saver and is recommended, but completely optional. If you use mtr generate_config you do not need to include your username/password in every line of this guide. There is no harm in specifying the username/password, but the generate_config command is designed to keep you from having to type that information every time. This document is written assuming one has not used mtr generate_config.

The first thing we need to do is use mtr to set up the default RubyGems installation. This is sort-of a package management system. It is used to install 'gems', which are pre-made packages for specific uses. In this walk through, we will install several of these 'gems' to get a functioning RoR environment.

This is done by typing:

mtr -u serveradmin@mt-example.com -p <password> setup_rubygems 

This command sets up a private RubyGems repository in your data directory. The repository is created inside of the data directory in your home directory. For instance, if your site ID is 30, your RubyGems repository will be created in /home/30/data/rubygems. This default repository contains many of the gems that you will need. If at any point in time your repository becomes corrupt or you want to start over, issuing the setup_rubygems command will reset your repository to a default state.

After doing this, we need to refresh the bash profile to adopt some new environment variables:

source ~/.bash_profile

This will load in some environment variables needed to access your gem repository. This only needs to be done the first time as this file is run when you log into your (gs) site. The RubyGems specific area is marked off between special comment tags (#MTSTART and #MTEND). Anything between those tags will be overwritten when the setup_rubygems command is issued. Anything outside the tags will be left alone.

Next, we need to register the (mt) Media Temple gem repository with your local repository:

gem sources --add http://gems.mediatemple.net

With our repository added, whenever a custom compiled version of a gem exists, RubyGems will use it instead of the default one from gems.rubyforge.org.  To see a listing of the gems installed by default run the following  command:

gem list

At this point the initial setup is complete. If you have an application you'd like to install at this point, you do not need to go through with the remaining text in this document. It is recommended to do so, however, as it will give you a better idea of how this system works. The remaining text explains how to set up a testapp for testing RoR functionality.

Create the Rails test application to demonstrate a functioning container

The first thing that has to be done is navigating in to the right directory:

cd $HOME/../../containers

Next, we make a directory for rails and navigate in to it:

mkdir rails && cd rails

Now, we need to tell rails to install a test application and call it 'testapp': This command will generate the directory structure that we need in order to proceed:

rails testapp

Change directories to 'testapp'

cd testapp

Next, we use mtr to add a new application called testapp. It also needs to know the directory in which our testapp resides, so the following command will do both of those things.

mtr -u serveradmin@mt-example.com -p <password> add testapp $PWD testapp.domain.com 

This will make your container know that the application "testapp" is served out of the directory structure that we just created with the rails command. This registers your application in the (gs) Container. For more information about the parameters, run "mtr help".

Now, we can start the testapp by typing:

mtr -u serveradmin@mt-example.com -p <password> start testapp

NOTE:

If you get this error...

domain.com@clXX:/home/#####/containers/rails/testapp$ mtr -u serveradmin@mt-example.com -p (password) start testapp
Error executing command:
application started, but pid file was not found, check 'mtr status'

Please run 'mtr status' as described in the error, and check the state of your application. If your application shows that it is 'running' in the output of 'mtr status', then your application should be working, and you can disregard this message on startup. If your application is 'stopped', doing the following steps again has been reported to fix this issue.

gem install rails
gem install mongrel
mtr -u serveradmin@mt-example.com  -p <password>  add testapp $PWD
testapp.mt-example.com 

Check to see if your application is running:

mtr -u serveradmin@mt-example.com -p <password> status

NOTE:

  • Environment is always set to "Production" mode by default. One can set an application to be in "Development" mode, however it is more resource hungry and is not recommended for anyone but power users or people who know what they're doing. See this KB on how to enable development mode: http://kb.mediatemple.net/article.php?id=250

  • When doing an 'mtr list', the IP address listed for each application is for INTERNAL OPERATION ONLY. One cannot access these resources from outside of the (gs) Grid-Service SSH session.

Now, we will have mtr generate the .htaccess directives needed in order for the application to function:

mtr -u serveradmin@mt-example.com -p <password> generate_htaccess testapp

Since applications that are running on your (gs) Container must be proxied through the (gs) Grid-Service nodes, a few mod_rewrite rules must be added to the .htaccess file in the public directory. This generated area is between two special comment tags (#MTSTART and #MTEND). Anything between those tags will be overwritten when the generate_htaccess command is issued. Anything outside the tags will be left alone.

The .htaccess file is located in /home/#####/containers/rails/testapp/public

Next, we will create the symbolic link for your test app to bridge the gap between the ruby container and your public web server, accessible from the web:

mtr -u serveradmin@mt-example.com -p <password> create_link testapp

This will basically set up a symbolic link from your application's container directory to a web-accessible path.

You may now go to http://testapp.mt-example.com/ to view your newly created testapp.

Revisions:

02-27-2009: Updated info on how to get to the ROR controls in the AC. Thanks to aezs for point that out.

User Comments

No visitor comments posted. Post a comment

Fields marked with an asterisk(*) are required. Comment on this article

Fill out the form below if you would like to comment on this article.
 
 
 

(code is not case-sensitive)
 
Submit
 
 

Continue