How Do I Start Using Ruby on Rails?

Step 1: Set Up Your Rails Environment

First, we're going to need to create the environment in which our rails application will live. Head to your cPanel and access the Ruby on Rails icon from the Software/Services section.

From the Ruby on Rails control panel, users will be able to create an application environment.

  1. Be sure to give it a name. In this instance, we'll call the application Redmine since it will be hosting a Redmine application.
  2. cPanel will fill in the application path, so just check the box for Load on Boot?
  3. Set the environment to Production.
  4. Click Create.

This will create a directory at /home/[your username]/rails_apps/[app name]. Since the username is cubecity, this directory is called /home/cubecity/rails_apps/redmine.

Note: It's best to install this on a domain or subdomain as Redmine will often not work correctly with the default rewrite in a subfolder.

Step 2: Download & Extract the Application

First, head to your application's webpage and download the application. In this instance, we'll go http://rubyforge.org/projects/redmine/ to download Redmine.

Upload your application zip file to your rails_apps directory in your home directory as shown in the following figure.

Unzipping the File

This next part goes as follows:

  1. Delete the redmine directory in its entirety.
  2. Rename the directory you unzipped to redmine. Essentially, we're replacing the default cPanel redmine directory with our downloaded one.
  3. Delete your zip file for basic housekeeping. See the following figure for clarification.

Now we're done with step two. Let's set up our database.


Step 3: Set Up the Database & Database User

Head back to cPanel. Go to MySQL Databases under the Databases section.

First, let's create a new MySQL database.

  1. Under the Create New Database section, make a new database.
  2. Choose a name for your MySQL database
  3. Click on Create Database as in the below figure:



    In the example, we'll call it redmine. Also, create a MySQL user.
  4. Scroll down to the MySQL Users section, and begin filling out your desired database user credentials. We will also call the user redmine. Check out the figure below for more information:

  5. Next, in the Add User To Database section, let's add our user to our redmine database.
  6. Select your user from the drop-down menu.
  7. Select your database from the drop-down menu.
  8. Click Add.

  9. In the next screen that appears, select the box for All Privileges.

Now, with our database done, let's create the required Gemfile.


Step 4: Setting up the Gem Environment

To ensure gem installation can occur properly, we would need to ensure that a valid Gemfile exists, as well as that the PATH and GEM_HOME variables are set properly. To do this:

  1. First open the .bashrc file, located in your /home/cPanel_username folder, and add this to the end of the file:
     
    export PATH=$PATH:~/ruby/gems/bin
    export GEM_HOME=~/ruby/gems

  2. Next, make a new file called Gemfile in the /home/cPanel_user/rails_apps/redmine folder, and paste the following into it:
     
    source "http://rubygems.org"
    gem "rake", "0.8.3"
    gem "rack", "1.1.0"
    gem "i18n", "0.4.2"
    gem "rubytree", "0.5.2", :require => "tree"
    gem "RedCloth", "~>4.2.3", :require => "redcloth" # for CodeRay
    gem "mysql"
    gem "coderay", "~>0.9.7"
    gem "rdoc", "~>2.4.2"

Now that the the environment is configured, let's hook Rails up to this.


Step 5: Connect Rails to The Database

This next section requires knowledge of SSH and how to connect to the server with a client such as PuTTY.

Instructions on configuring your SSH access can be found in this article:

We'll be executing some commands from SSH, and these are required for the application to function correctly.

  1. Connect to your account via SSH. You should be looking at a screen similar to this:



  2. Change directories to your rails_apps/redmine directory (or whatever you named your rails app in step one. You can do this by executing the following command:
    cd rails_apps/redmine
  3. Copy config/database.yml.example to config/database.yml and edit this file in order to configure your database settings for "production" environment. Do this with the following command:
    cp config/database.yml.example config/database.yml
  4. Edit the file to add our MySQL credentials we created in step three. Execute the following command:
    nano config/database.yml

    You should see a screen that looks similar to this:

  5. Go ahead and edit the settings under the production section. Here's an example of what the settings look like:
    production:
    adapter: mysql
    database: cubecity_redmine
    host: localhost
    username: cubecity_redmine
    password: q1w2e3r4

Instead of 'cubecity' you'll use your own username. The rest of this step for Redmine is reposted from Redmine's guide for installing:

  1. Execute the following code in SSH:
    rake generate_session_store
  2. Next, execute the following. It will create tables and an administrator account:
    RAILS_ENV=production rake db:migrate
  3. Insert default configuration data in database, by running the following command:
    RAILS_ENV=production rake redmine:load_default_data

Now let's set up redirects.


Step 6: Create Redirects

Start by heading back to cPanel.

  1. Go to your Ruby on Rails control panel and look under the Create A Rewrite section.
  2. Click the Create Rewrite button to create a rewrite for your application. This will create a .htaccess file in the directory of the domain you choose.
    HostGator doesn't recommend installing rails applications to subdirectories unless you are experienced with Ruby on Rails. Note which domain you rewrite the site to, so that you can to make a brief change to its .htaccess file.
  3. Go back to cPanel and head to your File Manager.
  4. In the Directory Selection popup, select Document Root for:, choose the domain you chose for your rewrite, make sure that Show Hidden Files is checked, and click Go.
  5. Select the .htaccess file, and select Edit. You should see the following (or something similar, depending on what your domain is.):
    RewriteEngine on

    RewriteCond %{HTTP_HOST} ^rails.hgfix.net$ [OR]
    RewriteCond %{HTTP_HOST} ^www.rails.hgfix.net$
    RewriteRule ^/?$ "http://127.0.0.1:12001%{REQUEST_URI}" [P,QSA,L]
  6. Change the following line:
    RewriteRule ^/?$ "http://127.0.0.1:12001%{REQUEST_URI}" [P,QSA,L]

    To match the line listed below:
    RewriteRule ^(.*)$ "http://127.0.0.1:12001%{REQUEST_URI}" [P,QSA,L]

    Note the change in the second set of characters ==> ^/?$ to ^(.*)$
  7. Click Save Changes.

Step 7: Run Application

At this point, everything should theoretically run by browsing to the domain at which you installed your Rails application.