Setting up a Rails dev environment on Webfaction

After purchasing the 3rd edition beta PDF of Agile Web Development with Rails, I wanted to jump right in to mucking around with Ruby on Rails on Webfaction’s servers. I ran into some silly problems that took me a good bit of time to work through.

In the hopes of saving you time, here are the steps it takes to create a simple “Hello World” Rails 2.1 development site on Webfaction.

  1. In the Webfaction Control Panel, create your Rails 2.1 application. Remember to check the box next to “Autostart.” The Mongrel server that Rails uses is not known for it’s reliability and autostart should keep you from having to restart the server manually if it goes down.
  2. Use the Webfaction Control Panel to associate your new Rails application with a domain name/url.
  3. Once that’s setup, go to the URL you associated with your Rails application. This will start your Mongrel server and you should see the “Welcome Aboard” message.
  4. Rails needs to connect to a database to function correctly (unlike Django). Go ahead and create one in the Webfaction Control Panel. (You might want to create three databases depending on your needs. Rails can operate in three different modes, test, development and production. Each can and probably should have its own database.)

Our job isn’t done yet. Right now the site is in production mode, and we need to move it to development. That way we get all the lovely and informative errors while we’re building our application.

  1. SSH into your Webfaction server. Find your way the Rails application directory /webapps/<yourappname>
  2. Update your /config/database.yml file so that under “development” it points to the database you created earlier. Your username will be the same as your database name.
  3. Update your /config/environment.rb file so that in the Rails::Initializer.run do |config| block you set a secret hash key. The line should look like this: config.action_controller.session = { :session_key => “_myapp_session”, :secret => “some secret phrase of at least 30 characters” }
  4. Open the autostart.cgi file. Near the end you’ll find a command that looks like this: os.system(’/usr/local/bin/mongrel_rails start -d -e production -P /home2/<accountname>/webapps/<appname>/log/mongrel.pid -p <processID#>’)
  5. To restart the server in development mode, you’re first going to stop the server by running mongrel_rails stop and then take that command from the autostart.cgi file and run it without the os.system part, the full path, and with production changed to development. Like this, mongrel_rails start -d -e development -P /home2/<accountname>/webapps/<appname>/log/mongrel.pid -p <processID#>

Now it’s time to create our hello world application.

  1. While SSH’ed into the Webfaction server and in your app directory, run ruby script/generate controller Hello This will create the various files necessary for your Hello controller.
  2. Open the newly created “controllers/hello_controll.rb” file and define the world function:
    class HelloController < ApplicationController
    def world
    end
    end
  3. Now we need to associate a template with our exciting new controller and function. Under “views/hello/” create a file named “world.html.erb” and put your hello world message in it as regular HTML: <html><title>hello world</title><body><h1>Hello world!</h1></body></html>

Rails automatically maps your controllers and functions/actions to URLs. So point your browser to <yourappurl>/hello/world and you should see the excessively simple HTML file you just created. Your Webfaction development environment is ready to play with. Enjoy.

2 Comments

  1. Posted June 14, 2008 at 10:26 pm | Permalink

    Nice walk through, glad you chose webfaction.

    Ross
    - http://www.hostdisciple.com

  2. luke
    Posted July 4, 2008 at 9:04 pm | Permalink

    thanks for the walkthrough… my small brain was a bit confused about rails starting in production mode; and the errors that brings with it.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*