A collection of computer systems and programming tips that you may find useful.
 
Brought to you by Craic Computing LLC, a bioinformatics consulting company.

Wednesday, July 29, 2009

Environment Variables, Rails 2.2.2 and Passenger 2.2.4

I want to pass an environment variable into my Rails app which represents the root of the server name that it will respond to (e.g. 'craic.com'). Note that this is not the default hostname of the machine. And I want to set this when I run the app on my laptop and when I deploy it to an EC2 node.

I'm running the app under Apache2 and Phusion Passenger.

Setting a UNIX shell environment variable in .bashrc, etc., doesn't work because the files are not sourced from Apache.

The way to do this is to set them within your Apache Virtual Host block using the SetEnv directive. For example, here is a vhost block for my app:
<VirtualHost *:80>
ServerName www.example.com

SetEnv FOOBAR "foobar"

DocumentRoot "/mnt/myapp/current/public"
RailsEnv development
RailsAllowModRewrite off
<directory "/mnt/myapp/current/public">
Order allow,deny
Allow from all
</directory>
</VirtualHost>
Then in your application, typically in your config/environment.rb file, you would access this as:
my_variable = ENV['FOOBAR']

This works great but ONLY with version 2.2.3 or higher of the Passenger Gem. To see what version you have installed:
$ gem list passenger
and to update:
$ sudo gem update passenger
$ sudo passenger-install-apache2-module




 

No comments:

Archive of Tips