设置环境变量(ENV)以便在Rails中使用

时间:2022-03-05 12:05:12

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

在Rails服务器上使用MongoID进行实验,并对如何/在哪里设置环境变量感到困惑。

config/mongoid.yml default template provides:

配置/ mongoid。yml默认模板提供了:

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?

我的问题是,这些东西是不是在轨道上?还是在系统层面?如果是,在哪里/如何设置,使用户帐户不需要登录才能有效?

2 个解决方案

#1


14  

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

ENV散列将具有从启动rails进程时开始的系统环境中的值。

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

在启动服务器或程序之前,可以在命令行上设置这些参数。例如在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.

除非将它们添加到配置文件中,否则这些配置文件只对shell的生命有效,但是您的web服务器很可能不会使用该配置文件,因此它只对本地开发有用。

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

它们也可以设置,例如,在使用SetEnv的Apache中。例如:

<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.

在您的apache配置中,SetEnv在任何地方都是合法的,这与您的应用程序所在的上下文是相同的。

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.

关于您对最佳实践的评论,有些人在源代码控制中放置了一个示例yml配置文件,而忽略了config/*。来自源码控制的yml文件。在克隆存储库时,将示例复制并更正为正确的值是设置的一部分,如运行rake tmp:create以使tmp文件夹结构。

#2


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:

我想在这里添加另一个选项。在引导时,Rails将尝试读取DATABASE_URL作为url,并从env变量连接到数据库(忽略database.yml)。您应该将数据库指定为:

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.

这只是另一个选项,而不是将erb设置放入database.yml。

#1


14  

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

ENV散列将具有从启动rails进程时开始的系统环境中的值。

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

在启动服务器或程序之前,可以在命令行上设置这些参数。例如在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.

除非将它们添加到配置文件中,否则这些配置文件只对shell的生命有效,但是您的web服务器很可能不会使用该配置文件,因此它只对本地开发有用。

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

它们也可以设置,例如,在使用SetEnv的Apache中。例如:

<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.

在您的apache配置中,SetEnv在任何地方都是合法的,这与您的应用程序所在的上下文是相同的。

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.

关于您对最佳实践的评论,有些人在源代码控制中放置了一个示例yml配置文件,而忽略了config/*。来自源码控制的yml文件。在克隆存储库时,将示例复制并更正为正确的值是设置的一部分,如运行rake tmp:create以使tmp文件夹结构。

#2


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:

我想在这里添加另一个选项。在引导时,Rails将尝试读取DATABASE_URL作为url,并从env变量连接到数据库(忽略database.yml)。您应该将数据库指定为:

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.

这只是另一个选项,而不是将erb设置放入database.yml。