Articles tagged with Deployment Tutorials


Deploying a Rails App with DreamHost

Posted 05/04/2011 in DreamHost and Deployment Tutorials.
UPDATED TUTORIAL COMING SOON. Greetings everyone. Our experience has taught us that getting an app up and running online with FastCGI enabled on a shared host is not easy. Many developers have run through this tutorial from scratch and we have received a lot of positive feedback from those who have successfully used it to deploy their applications online with DreamHost. If you decide to sign up for a plan with this hosting company (which we'd recommend), please consider using their promo code 50OFFRAILS - which instantly gives you $50.00 off any of their plans (i.e. starting at ~$5/month hosting for 20GB+ space and 1TB+ monthly bandwidth) and helps us out with hosting costs. Thanks.

Ok - this tutorial assumes that you have a working Rails app on your local machine and wish to have it up and running on your DreamHost server with FastCGI in production mode.

Here we go:

As soon as your username and password are set youíll log into their control panel which will look something like this.

Rails Hosting - DreamHost Web Panel

Click on the "Domains" tab and look for the following

Rails Hosting - DreamHost Add Domain

Click on the "Add New Domain / Sub-Domain" and then you'll see something like this (a slightly older pic, but you get the idea):

Rails Hosting - DreamHost Fully Hosted Domains

It is important that FastCGI is selected. You may also use a subdomain and load your app there if you don't want it hogging your site. We'll tell you how to set the web directory root to your public folder later.

OK, go to Goodies > mySQL and you'll see the following (again, they've spruced up their mySQL so it will look a bit different):

Rails Hosting - DreamHost mySQL

Enter in all your info and make note of it.

Login to your FTP/SSH domain (ours was slauson.dreamhost.com) via your favorite SSH program (we use SSH Secure Shell, but openSSH is free if you're looking for one) and get to the command line to generate your rails app (note: if your user does not have SSH enabled you can do so by going to the users -> manage users -> edit window).

 
> cd www.domain.com
> rails your_app_name

You don't need to generate the scaffolds, controllers, or models here if you have them on your local box.

Ok, at this point we would recommend going to the config folder on your local machine and setting up some *.online versions of the database.yml, routes.rb, and the environment.rb. Even though once you upload them to the server you'll remove the .online part to replace the existing files, we found that using this method helped keep my configurations for local development and online production organized.

Rails Hosting - Make online files

The 'production' part of your database.yml.online should look like this:

 
production:
  adapter: mysql
  database: your_database_name
  username: your_database_username
  password: *******
  host: your_mysql_host_name.com
  port: 3306

Note that we got rid of the socket line used for connecting to a local mySQL database (DreamHost uses remote mySQL servers).

Weíll also give you a tip about your environment.rb file ñ add the following lines to the end of the file to allow yourself to send emails later from your DreamHost server.

 
# Include your application configuration below 
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.server_settings = {
  :address => "domain-of-smtp-host.com",
  :domain => "domain-of-sender.com",
  :port	=> 25,
  :authentication => :login,
  :user_name => "your_user_name",
  :password => "*****" }
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.default_charset = "utf-8"

In your environment.rb file it is also important to uncomment the following line:
 
ENV[ëRAILS_ENVí] ||= ëproductioní
Please Note: NEVER run your Rails app with FastCGI in development mode on a shared host (or anywhere) - this will always lead to memory leaks. DreamHost does have a watchdog for this - but don't test it!

And in your routes.rb set the default route to the name of your main controller (you will see that all you need to do it uncomment this line and add in your controller name).
 
map.connect '', :controller => "your_main_controller_name"

OK, at this point you can dump all your mySQL data online to your database domain that you set above (it should be ready by now).

Then FTP your entire app directory to replace the one that was built on the server.

Now you can open the config folder and upload in your online versions of database.yml, environment.rb, and routes.rb files (remember to remove the .online part).

Next, upload your images, styles, and javascripts to the appropriate folders in the public directory. Make sure you public folder files stay CHMOD 755 ish and don't get too high or low (this is, believe it or not, a common error point). Then make sure your .htaccess file has the little ëfí to make your app FastCGI and upload it:
 
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
You must also DELETE the index.html file in your public folder. Your route.rb will now send requests to your main controller.

OK, at this point you can point your browser to www.domain.com/your_rails_app/public and you should see your rails app running fast and nice. If not, double check everything above. If it still doesn't work, feel free to email us at info _at_ railshosting.org and we'd be happy to help you out.

It is very important to note that in production mode with FastCGI much of your application data is cached and held in memory. This means that Rails is 'always on' and ready to feed a browser with code. Thus, if you make any changes to your app (database, controllers, views, models, config, etc...) you'll need to run via the command line:
 
killall -USR1 dispatch.fcgi
You won't be able to kill other user's processes (as you'll see) - but yours should be shut down after this. You can also try:
 
killall -9 ruby
...but we believe as of a couple of months ago DreamHost disabled users from doing this (our own observations, please let us know if you see otherwise).

Ok

Last thing is make your public folder the root web directory. If your a shell symlink master (see below) you know how to do this, but for everyone else you can do this by going back to the DreamHost control panel and making your way to Domains -> Manage Domains and click "edit" next to your Rails app domain (again, sorry for the slightly old pic).

Rails Hosting - DreamHost Web Directory

change the web directory path to your public folder and that's it. Wait about 10 mins, point your browser to your www.domain.com, and youíre all set.

We actually recommend that you become a shell/symlink master, with Dreamhost we often create our own Rails app folders in my root and then can easily symlink to a test/preview domain ([~]$ ln -s ~/myapp/public ~/www.thetestpreviewdoamin.com ), make sure things are working, then symlink to our live domain ([~]$ rm ~/www.thetestpreviewdoamin.com    [~]$ ln -s ~/myapp/public ~/www.thelivedoamin.com ). It just makes things a little easier and this helps you if you ever go with other hosts that give you SSH/Shell access.