Deploying Rails on Dreamhost with Phusion Passenger

Deploying Ruby on Rails has always been a pain, even with tools like Mongrel and Capistrano. On shared hosting, it’s nearly impossible. A little while ago, I noticed a project called Phusion Passenger (aka mod_rails), which sought to make deploying as easy as uploading your application to your webhost. A few days ago, Dreamhost (my host) announced on their blog that Phusion Passenger was available on their servers. Naturally, I took some time to play around with it, and so far I’ve been extremely impressed. I threw together a quick app using Viddler.rb and deployed it to Dreamhost (you can see it here). Here’s a quick guide on how to deploy to Dreamhost yourself using mod_rails.

Getting set up

The first thing to do is to set up your domain or subdomain to use Passenger. Just go to the Manage Domains page on the DH web panel, and either create a new domain or edit an existing one. Check the “Ruby on Rails Passenger (mod_rails)?” checkbox and set the web directory to where the public directory of your app will be. Below is a screenshot of my settings for viddler.fidgeting.net.

mod_rails settings

Next, you need to create your database, so just go into the Manage MySQL section of the panel. Create your database, which should be named as a production database. In my case, it was viddler_production. Then, make sure to change your database.yml to reflect the database you just created.

Uploading and running your application

Before uploading, make sure the first line of your public/dispatch.fcgi has the correct path to ruby on Dreamhost (which is /usr/bin/ruby). Then all you need to do is upload the files to Dreamhost and go to the domain. That’s it—your app is now completely functional. Amazing, huh?

If you need to first run migrations, you can do that from SSH with this command:

RAILS_ENV=production rake db:migrate

If you need to restart your application (anytime you change any files), just run the following command via SSH:

touch tmp/restart.txt

Getting Gems to work on Dreamhost

Since Dreamhost is a shared host, you can’t directly install gems on the server. Dreamhost’s wiki has a lot of information on different ways to get gems working, but they are all fairly involved, and I couldn’t get any of them to work. The way I did it was by unpacking the gems into vendor/gems by using the “gem unpack” command:

gem unpack <GEM NAME>

I’m sure there are better ways to do it, but this method worked perfectly for me.

Overall Impressions

Like I said at the beginning, I’m very impressed with Passenger, and so far I haven’t noticed any issues. The total time for me to deploy was about 20 minutes: way faster than with Mongrel or FastCGI. It’s definitely awesome to see a host as large as Dreamhost using this, and hopefully soon more will follow suit.

Posted on May 17, 2008
Tagged with: , , , , , ,

30 Comments

Avatar

Colin Devroe 21 May 2008 at 8:45AM

Superb write up.

Avatar

Laurent Kretz 27 May 2008 at 1:16PM

Hello Kyle, Thanks for that very easy tutorial !

I am having an issue, and it might be linked to the dispatch.fgci ruby address …

The home page of my app is fine, it’s the “Welcome aboard” default page, but when i navigate to a specific action (ex: mySite.com/myController/myMethod/ ) i get a “Ruby on Rails version ‘1.1.2’ not found” error …

In the dispatch file, how exactly should the address be writter (maybe i have a path or synthax prob) ?

Avatar

Kyle 27 May 2008 at 1:23PM

Hm, I don’t think that’s a dispatch.fcgi error. It seems like your app is requiring a earlier version of Rails (1.1.2). Check your config/environment.rb and see what RAILS_GEM_VERSION is set to. For me it’s “2.0.2” (the newest version). You might need to freeze rails–check out this tutorial on how to do it.

1.1.2 is a pretty old version, so you probably want to upgrade to the newest…

Avatar

Laurent Kretz 27 May 2008 at 2:04PM

Hi again, Thanks for answering, but obviously there’s something else, my baby app seems to not “boot” on the server (as it works perfectly on my local machine)… Will reply if I understand why and how I fix it, for future readers !

Avatar

Ryan 10 Jul 2008 at 10:15AM

Great write-up, I’m trying it out now. However, I am still pretty new to setting up production environments. When using this method of deploying an app to Dreamhost, can I still use Subversion to update the app, rollback, etc. ?

Avatar

Kyle 10 Jul 2008 at 10:18AM

Ryan: Absolutely! If you’re using Capistrano, you just have to change the restart task to create the restart.txt file:

http://tomcopeland.blogs.com/juniordeveloper/2008/05/mod_rails-and-c.html

Avatar

Ryan 10 Jul 2008 at 10:41AM

Hmmm… I thought by using mod_rails, then I didn’t have to use Cap? I’m obviously confused.

Also, I enabled mod_rails, setup mysql db, uploaded the project to the public folder on my ftp, so now http://trinity.tefcwindsor.org/ just shows the index (I have to migrate the db). So I ran: rake db:migrate RAILS_ENV=”production”

But I still get an access denied error. Even though I swear my yml file is setup with the correct production permissions.

Avatar

Kyle 10 Jul 2008 at 10:53AM

Ryan: Capistrano is just a tool to make deploying easier–it checks out the source from SVN, keeps copies so you can roll back, etc. It doesn’t have to do as much work when deploying with mod_rails, but I still find it useful.

Also, make sure your domain is pointed at your /public directory, otherwise it won’t work. Right now it doesn’t seem to be set up that way.

I’m not sure what the issue is with migrating, maybe sending Dreamhost a support message might help? I know they helped me with a couple things when I started using Passenger.

Avatar

Ryan 10 Jul 2008 at 11:18AM

“Before uploading, make sure the first line of your public/dispatch.fcgi has the correct path to ruby on Dreamhost (which is /usr/bin/ruby).”

This may sound silly, but I’m looking at my dispatch.fcgi file, and I don’t see where I should input that path for Ruby….

When I try to run Cap deploy, everything seems to be working file, then at the end I get an error saying it can’t find the Rake File and it seems to be looking in usr/lib/ruby

Avatar

Kyle 10 Jul 2008 at 11:35AM

The very first line of dispatch.fcgi needs to be this:

#!/usr/bin/ruby
Avatar

Ryan 10 Jul 2008 at 1:45PM

Ok, I’ve added that…. But when using “deploy” in capistrano, I still get the following:

executing "cd /mnt/local/home/rdougher/trinity.tefcwindsor.org/releases/20080710160648; rak RAILS_ENV=production  db:migrate"
servers: ["dalitz.dreamhost.com"]
[rdougher@dalitz.dreamhost.com] executing command
*** [err :: rdougher@dalitz.dreamhost.com] rake aborted!
*** [err :: rdougher@dalitz.dreamhost.com] No Rakefile found (looking for: rakefile, Rakefile, rakefile.rb, Rakefile.rb)
*** [err :: rdougher@dalitz.dreamhost.com] /usr/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:2143:in `raw_load_rakefile'
*** [err :: rdougher@dalitz.dreamhost.com] (See full trace by running task with —trace)
command finished
command "cd /mnt/local/home/rdougher/trinity.tefcwindsor.org/releases/20080710160648; rake RAILS_ENV=production  db:migrate" failed on rdougher@dalitz.dreamhost.com

[Note: I edited this so the code displayed a little nicer -Kyle]

Avatar

Kyle 10 Jul 2008 at 1:49PM

Hm, is there a file named Rakefile there? Maybe you forgot to commit it to your repository? Without that, rake doesn’t know about the Rials rake tasks.

Avatar

Ryan 10 Jul 2008 at 1:53PM

Well, if I understand the error correctly, it’s looking for the rake gem in ‘usr/lib/ruby’ when it should be looking for it in ‘usr/bin/ruby’ correct?

Avatar

Kyle 10 Jul 2008 at 2:04PM

Ryan: I don’t think so. There should be a Rakefile in your app directory (releases/20080710160648/ in the error you posted above)

Avatar

Ryan 10 Jul 2008 at 2:07PM

Ok, I just looked in that folder, there is no rakefile. So what did I do wrong? haha.

Do I have to copy over a rakefile manually? Or is it a setting I have messed up? Because I do have a rakefile in my local project folder.

Avatar

Kyle 10 Jul 2008 at 2:09PM

The Rakefile is created automatically when you do rails <myapp>. Not sure how it got lost on deployment, but yeah, just copy it from your local folder.

Avatar

Ryan 10 Jul 2008 at 3:13PM

Man…unfortunately I’m really confused on how to get this stuff working. I copied over the rakefile, so I don’t get that error anymore.

I created a “rails” directory and uploaded the “trinity” project to that directory. And the subdomain trinity.tefcwindsor.org is pointed towards rails/trinity/public

So I go to: http://trinity.tefcwindsor.org and I get a 500 internal server error.

Any thoughts?

Avatar

Kyle 10 Jul 2008 at 3:15PM

Hm, at this point, I’m really not sure. I’d recommend looking through your log files (current/logs/production.log) and see if anythings there. Also, maybe send a support ticket to Dreamhost. They might be able to help more than I can.

Avatar

Ryan 10 Jul 2008 at 4:10PM

Kyle, It looks like maybe I have a problem with my migrations. I ran the RAILS_ENV=production rake db:migrate command, and when I look at PHPmyAdmin to see what it migrated, it only copied the tables, but no records. Now, in my development database (sqlite3) I have many records for users and each page of the application, so I think that’s why my app is really messing up.

Am I missing something here? Shouldn’t running the rake db:migrate command also copy over the records for each table, along with the tables themselves?

Avatar

Kyle 10 Jul 2008 at 4:17PM

rake db:migrate just runs the migrations that you’ve created, it doesn’t insert data (it just creates the structure).

Avatar

Ryan 10 Jul 2008 at 4:22PM

Ok, I understand that now. How do I copy over the records in the database (not just the structure)? I’m reading about “bootstrap” but when I run the rake db:bootstrap command, I get this error:

rake aborted! Don’t know how to build task ‘db:bootstrap’

Avatar

Kyle 10 Jul 2008 at 8:50PM

Hm, I’m really not sure. I’d look through the Rails API doc and google some. You also might get help in the rubyonrails IRC channel.

Avatar

Ryan 10 Jul 2008 at 10:23PM

Kyle, I actually found a good tutorial that showed me how to do that here: http://alistairisrael.wordpress.com/2007/09/07/using-rails-console-to-copy-records-across-databases/

So now the Rails app is actually working! I had to delete the htaccess file. And now the app is running fairly snappy, and my stylesheets and images, etc. are all available.

Now my only problem is getting Subversion to play nice with mod_rails. You can view my app at: http://trinity.tefcwindsor.org/

The Subversion directory I setup is at: http://trinity.tefcwindsor.org/trin089/

The problem is, Rails is trying to execute “trin089” as a controller action. So apart from just creating my repository on a different domain (which I don’t want to do), I can’t think of any solutions. So I can’t get capistrano to work until I get a functioning repository.

Avatar

Baber 09 Sep 2008 at 5:25AM

I hope you can help me out… I have trying to deploy my rails application with no success for a whole week.. Now I keep getting this error

undefined method `include_root_in_json=’

and several others like these Is this some issue with the GEMs or something? Kindly help me out!

Avatar

Kyle 09 Sep 2008 at 9:21AM

Baber: I’m not quite sure what the issue might be, but at first glance, it’s probably a Gem you forgot to include, so the method you’re trying to use doesn’t exist.

Avatar

Jeffrey 01 Oct 2008 at 5:26PM

Hi Kyle, I was wondering if you could offer some advice. I’ve unpacked a gem to my project in /vendor/gems, but when trying to run script/runner it can’t find the gem I copied over. After unpacking the gem, is there anything else I need to do? I tried adding this line to my environment.rb,

ENV[“GEM_PATH”]=’home/user/site/vendor/gems:/usr/lib/ruby/gems/1.8’

Is there anything else I can try? Did you have trouble getting Dreamhost to recognize your gems in the bash environment?

Avatar

Kyle 01 Oct 2008 at 10:35PM

Jeffrey: I haven’t tried script/runner on Dreamhost yet, though in your ENV["GEM_PATH"] line, you don’t have a slash before the “home”, maybe that’s the issue?

Avatar

owen 07 Oct 2008 at 10:02PM

thanks for the great tutorial! just signed up with dreamhost today (and used your code :) )

Avatar

Kyle 07 Oct 2008 at 10:27PM

Owen: Glad it helped you out!

Avatar

Gustavo Scanferla 12 Jun 2009 at 2:58PM

This post helped me too =)

Leave A Comment

Ajax-loader