Monday, November 10th, 2008...5:07 pm
Set RAILS_ENV for Phusion Passenger on Dreamhost
Recently I began looking into using Phusion Passenger for running all my Rails apps instead of the normal nginx+mongrel stack that I normally use. Since its release, Passenger has been getting quite a bit of publicity and hype and I figured it was time I started looking into it on my own as well.
Dreamhost was the first shared hosting provider to offer Passenger on all their shared hosting plans so that was a natural place for me to start. I planned on using my Dreamhost box as a staging server for a few small apps as I started to get a feel for Passenger.
The major problem I ran into was trying to set RAILS_ENV to “staging” so that my apps would run from the staging environment. The way Passenger gets configured, the RAILS_ENV setting is configured in the Apache virtual host file, which I don’t have access to on Dreamhost (or most other shared hosting providers).
You can set the RAILS_ENV constant in your config/environment.rb file to ’staging’ and Passenger will pick the setting up; but, of course, this won’t work because then I would be required to constantly manage and change my environment.rb file depending on whether or not I was working locally in ‘development’ or on ‘production.’
What I decided to do was just add a custom task to my capistrano deployment recipe file (config/deploy.rb) and manually insert the RAILS_ENV variable into the top of my config/environment.rb file after deploy.
namespace :deploy do
desc "set ENV['RAILS_ENV'] for mod_rails (phusion passenger)"
task :set_rails_env do
tmp = "#{current_release}/tmp/environment.rb"
final = "#{current_release}/config/environment.rb"
run <<-CMD
echo 'RAILS_ENV = "#{rails_env}"' > #{tmp};
cat #{final} >> #{tmp} && mv #{tmp} #{final};
CMD
end
end
after "deploy:finalize_update", "deploy:set_rails_env"
Now based on my cap deployment command, the RAILS_ENV will be set to ’staging’ or ‘production’ automatically. Really, staging is all that I needed since production is the natural environment on my production server already.
Hope this helps and saves someone else an hour of searching.

7 Comments
November 18th, 2008 at 8:37 pm
Thanks, that was really helpful!
I had to make to minor changes:
Line 9: CMD
Make sure to remove the spaces after “CMD”
Line 7 I had to change from:
echo ‘RAILS_ENV = “#{rails_env}”‘ > #{tmp};
to:
echo ‘ENV['RAILS_ENV'] = “#{rails_env}”‘ > #{tmp};
November 18th, 2008 at 9:19 pm
Yeah, I wouldnt cut and paste directly from the listing because of CR-LF issues between unix and windows (unless you view the plaintext version).
Also, strange you had to set ENV['RAILS_ENV'] instead of just RAILS_ENV. I had the exact opposite case on my dreamhost server.
January 6th, 2009 at 12:01 am
Awesome. It’s nice to see little nuggets of useful info about running Rails on specific hosting platforms now. I love DreamHost — been an avid customer for 4 or more years now — and after many years of PHP and Java I am really beginning to love the Ruby/Rails combo. And Capistrano makes things so easy to manage from a deployment perspective it’s spooky.
Thanks for sharing!
April 14th, 2009 at 4:48 am
Change in config/environment.rb
ENV['RAILS_ENV'] ||= ‘production’
To
ENV['RAILS_ENV'] = ’staging’
If you need perticuler staging ENV.
Thanks-
Shrii
————————–
lokhande.shrikant@gmail.com
April 28th, 2009 at 10:20 pm
I’m going lobby Dreamhost to allow for us to set the RailsEnv string from their interface.
This really is a problem with their api…and if fixed, should make it easy to define things like:
stage.example.com => RailsEnv “staging”
test.example.com => RailsEnv “test”
http://www.example.com => RailsEnv “production”
April 29th, 2009 at 8:14 am
@chovy,
If you do get it up for a vote on Dreamhost, I’ll gladly spend some of my voting credits to vote for it.
June 22nd, 2009 at 11:12 pm
I’m a dreamhost user as well. Struggling to set rails environment of staging app to staging. One app with multiple environments sounds so complicated with shared hosting.
Leave a Reply