Beginners guide to setting up a Rails Hello World

From A2Wiki

Jump to: navigation, search

Follow these steps to get your first rails application working on the A2Hosting servers.

Setup your SSH account, follow all the procedures here [1]

This will make your life much easier. No passwords to enter. Ok lets get going;

1. First thing to do is to create a Rails application. Log into your server via ssh and enter the following commands

  cd ~/public_html
  rails -d mysql hello

2. Next create a controller to service our requests

 cd hello
 script/generate controller say index

This creates all the necessary files in the ~/public_html/hello/app dir.

3. Now we need to edit the view to display our famous programmers #1 program.

 cd app/views/say
 echo "Hello World" > index.html.erb

4. Next go into the cPanel, logon with your userid password, then click on the "Ruby on Rails" icon in the "Software/Services" section.

5. In the "App Name" field type "hello" and hit tab. Notice how it automatically put the directory name in the Application Path field. Now hit create. This will take a while. I hit a bug here where the cPanel never returns after displaying "Added Ruby on Rails Application" so I just kill the browser and log back into the cPanel.

6. [Optional step] So now I created a sub-domain to point to my hello world application. Click on the "sub domains" icon in the "domains" section. Then enter hello as your subdomain name. Lets say its hello.mydomain.com

7. Now to keep Rails happy we need to setup a database. Since earlier I told rails that we'll be using mysql let go ahead and create one. Go back to the Home of cPanel and click on the "mySql databases" icon.

8. In the database field enter "helloproduction" then hit the "Create Database" button. Once thats done hit the go back url.

9. Now we need to make a MySql user. So enter hello in the username field. Do the password thing then hit "Create User". Once thats done hit go back.

10. Now we have to give our user access to our database. Just to make life interesting the database name and the users name are prefixed with your userid. So in the "Add User To Database" section select the two corresponding helloproduction database and hello user. Then hit "Add". The next screen allows you to set the security for the user on that database. Just go ahead and grant them all. Hit "Make changes" .

11. Now we need to tell our Rails application of this database and user. Go back to your ssh terminal session, its probably timed out by now so log back in. Go into ~/public_html/hello/config and edit the database.yml file. You can use vi to do this so enter the command;

 vi database.yml

Now if you haven't used vi before there are only a few things you need to know.

 Press i to go into edit mode
 Press Esc to get out of edit mode
 Press d to delete the character the cursor is on
 Press u to undo last change
 Press cw to change the current word the cursor is on
 Press A (capital) to edit at the end of the line.
 Press ZZ to save the file when in non-edit mode

Move your cursor down to the database line in the production section and change "hello_production" to the database you created earlier. Also change the username and password. (Remember the userid prefix.) Once your done hit Esc ZZ (thats capital Z!)

12. So now we need to migrate our database. Enter this command;

cd ~/pbulic_html/hello
rake db:migrate RAILS_ENV=production

We need to only migrate production database's on this system hence the RAILS_ENV override. Otherwise we would have to setup a development database.

You should see a successful migration, if you don't and you get Access denied go and check all the database names and username. Remember the userid prefix.

13. We're on the home straight now. Go back to your cPanel session in your browser and go into the Ruby on Rails section. It should be in your "Frequently Accessed Areas" now. We need to create what they call a "Rewrite". Basically it redirects all traffic to our Rails application. Otherwise we'd have to mess about with ports, eeek. So go ahead and hit the "Create Rewrite" on your Rails application. The button is towards the bottom of the page. Now select the domain or subdomain you want your Rails app to respond to. In my case its hello.mydomain.com

14. Now we only have to getting it running. Go back into the Ruby on Rails section and hit the "Run" button on your hello application.

15. Ok! It should be running now. Goto your webaddress and lets see (hello.mydomain.com for this example). If you see the "Welcome aboard" page start smiling. If you don't then your application is not running. You can check in ~/public_html/hello/log/production.log. If its empty then the application isn't running. Go and check everything.

16. Now for the famous Hello World. Add /say to your webaddress (hello.mydomain.com/say). You should see "Hello World". Pat your self on the back we're done.

You can use this as a great start for deploying your applications. Remember when you upload something that the database.yml can't be updated. I made mine read only just in case.

 chmod -w database.yml

Deploying Rails applications still seems a bit of an art form, there is a tool called Capistrano that can help. But I haven't seen a good solution yet.

Good luck.

Personal tools