Rails 4:如何使用RVM在Ubuntu 14.04上添加环境变量?

时间:2015-10-06 04:51:04

标签: ruby-on-rails-4 nginx capistrano passenger ubuntu-14.04

现在我正在尝试将我的Ruby on Rails应用程序部署到运行Ubuntu的Windows Azure上的虚拟机,但我可以修复设置环境变量错误,如下所述。

我已经为.bashrc添加了变量,现在添加了/etc/environment,但错误是相同的:

App 1227 stderr: [ 2015-10-06 04:10:57.3814 1352/0x9020d38(Worker 1) utils.rb:86 ]: *** Exception RuntimeError in Rack application object (Missing `secret_to$
App 1227 stderr: from /home/deploy/apps/matching_people/shared/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/application.rb:534:in `validate_secret_$
App 1227 stderr:        from /home/deploy/apps/matching_people/shared/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/application.rb:246:in `env_config'
App 1227 stderr:        from /home/deploy/apps/matching_people/shared/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/engine.rb:514:in `call'
App 1227 stderr:        from /home/deploy/apps/matching_people/shared/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/application.rb:165:in `call'
App 1227 stderr:        from /usr/lib/ruby/vendor_ruby/phusion_passenger/rack/thread_handler_extension.rb:94:in `process_request'
App 1227 stderr:        from /usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler/thread_handler.rb:151:in `accept_and_process_next_request'
App 1227 stderr:        from /usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler/thread_handler.rb:112:in `main_loop'
App 1227 stderr:        from /usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler.rb:415:in `block (3 levels) in start_threads'
App 1227 stderr:        from /usr/lib/ruby/vendor_ruby/phusion_passenger/utils.rb:112:in `block in create_thread_and_abort_on_exception'
[ 2015-10-06 04:10:57.3819 988/b5efeb40 age/Cor/Req/Utils.cpp:95 ]: [Client 1-1] Sending 502 response: application did not send a complete response

这是我的sudo nano /etc/environment文件:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"

export SECRET_KEY_BASE=2da0d3f0bfd7b9b05110bfad512f42df2d2bb2ef715c4e831caba96a9c0b2141fbfa718dff2f5daf916cd70a70afd1f24df49884c561fbbaf364b36652b2c7d1
ruby -e 'p ENV["SECRET_KEY_BASE"]'

export MATCH_PEOPLE_DATABASE_PASSWORD=2015deployer
ruby -e 'p ENV["MATCH_PEOPLE_DATABASE_PASSWORD"]'

当我运行echo $SECRET_KEY_BASEecho $MATCH_PEOPLE_DATABASE_PASSWORD时,我得到了确切的数据。

deploy@vmw-ubuserver:~$ echo $SECRET_KEY_BASE
2da0d3f0bfd7b9b05110bfad512f42df2d2bb2ef715c4e831caba96a9c0b2141fbfa718dff2f5daf916cd70a70afd1f24df49884c561fbbaf364b36652b2c7d1

deploy@vmw-ubuserver:~$ echo $MATCH_PEOPLE_DATABASE_PASSWORD
2015deployer

但我在制作中仍然遇到同样的错误,我在Ubuntu 14.04上使用RVM,Capistrano,Passenger和NGINX。

我的database.yml:

production:
  adapter: mysql2
  encoding: utf8
  pool: 5
  host: localhost
  database: matchpeople_production
  username: deployer
  password: <%= ENV['MATCH_PEOPLE_DATABASE_PASSWORD'] %>

My secrets.yml:

# Do not keep production secrets in the repository,
# instead read values from the environment.
production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
  secret_token: <%= ENV["SECRET_KEY_BASE"] %>

任何形式的帮助?

2 个答案:

答案 0 :(得分:5)

寻找并寻找正确的答案我一直在尝试太多的选择,但我没有让它工作正常。

所以,当我测试谷歌上发现的一些配置时,突然我发现了我的解决方案。

很简单,只是:

  • 添加下一行:

    export SECRET_KEY_BASE=3aa7e349bh345h245hi23452345h234ih52i3u45h$
    export MATCH_PEOPLE_DATABASE_PASSWORD=your_pass_here
    
    # Uncomment these lines and restart the server to validate if the variables are read correctly
    #ruby -e 'p ENV["SECRET_KEY_BASE"]'
    #ruby -e 'p ENV["MATCH_PEOPLE_DATABASE_PASSWORD"]'
    

在以下文件的末尾:

  1. sudo nano ~/.bash_profile
  2. sudo nano /etc/environment
  3. 如果您需要,可以取消注释 ~/.bash_profile 中的最后两行并重新启动服务器,当您再次登录时,您会看到值您的承诺之前的环境变量如下图所示。

    enter image description here

    我建议你保留这些注释的行只是为了验证然后再次评论它们。

    如果您有任何疑问,请发表评论!

    希望它有用。

答案 1 :(得分:0)

根据我的经验,Ruby RVM的secrets.yml中不需要secret_token。

production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

我认为要解决问题,请从production:部分删除第二行

如果您需要将环境变量添加到RVM ,请修改

# /etc/profile.d/appspecific.sh

添加类似

的内容
export GEM_PATH=/usr/local/rvm/gems/ruby-1.9.3-p551:/usr/local/rvm/gems/ruby-1.9.3-p551@global
export GEM_HOME=/usr/local/rvm/gems/ruby-1.9.3-p551
祝你好运!