Set environment variable (ENV) for use in Rails
Asked Answered
J

2

15

Experimenting with MongoID on a Rails server and confused about how/where to set the environment variables.

config/mongoid.yml default template provides:

defaults: &defaults
  host: localhost

...

# set these environment variables on your prod server
production:
  host: <%= ENV['MONGOID_HOST'] %>
  port: <%= ENV['MONGOID_PORT'] %>
  username: <%= ENV['MONGOID_USERNAME'] %>
  password: <%= ENV['MONGOID_PASSWORD'] %>
  database: <%= ENV['MONGOID_DATABASE'] %>

My question is are these set in Rails somewhere? or are they at the system level? and if so where/how to set so that no user account needs to be logged in for them to be valid?

Journeywork answered 11/2, 2011 at 20:34 Comment(2)
You don't have to necessarily use these MONGOID_ environment variables -- you could instead just set the values directly inside the yaml file.Bleb
yeah, but I'm trying to learn best practices. I also don't want passwords embedded in code and thus in git.Journeywork
M
16

The ENV hash will have values from the system environment from when the rails process was started.

These can be set on the command line prior to starting the server or program. For example in bash:

export MONGOID_USERNAME='username'

These are only good for the life of your shell, unless you add them to your profile, but it is likely that your web server won't use that profile, so it is only useful for local development.

They can also be set, for example, in Apache with SetEnv. For example:

<Location /app >
    SetEnv MONGOID_HOST 'localhost'
    SetEnv MONGOID_PORT '8883'
    SetEnv MONGOID_USERNAME 'username'
</Location>

This could be anywhere SetEnv is legal in your apache config, and that is the same context that your application lives under.

Regarding you comment about best practices, some people put an example yml config file in source control, and ignore the config/*.yml files from source control. When cloning a repository, copying and correcting the examples to the correct values is part of the setup, like running rake tmp:create to make the tmp folder structure.

Mizell answered 11/2, 2011 at 20:44 Comment(3)
Apache, Nginx, or Standalone?Mizell
We're using Apache with PassengerJourneywork
We do the default.yml & then in a Cap recipe copy the actual database.yml in for our SQL database but I saw the use of environment variables in MongoID's defaults so thought I'd investigate. Thanks for the Apache SetEnv info!Journeywork
H
0

I wanted to add another option here. On boot, Rails will attempt to read DATABASE_URL as a url and connect to a database from that env variable (ignoring database.yml). You should specify the database as:

DATABASE_URL="mysql2://user:pass@localhost/app_development" rails server

and you can verify this via:

DATABASE_URL="..." rails runner "p ActiveRecord::Base.connection_config"

This is just another option instead of putting erb settings into database.yml.

Hortatory answered 28/8, 2013 at 21:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.