Ruby-on-rails SemiFixed:“生产”环境缺少“secret_key_base”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23501329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:36:50  来源:igfitidea点击:

SemiFixed: Missing `secret_key_base` for 'production' environment

ruby-on-railsrubyherokuruby-on-rails-4

提问by ReynierPM

I've created a rails app (Rails 4.1.0) from scratch and I am getting a problem that I am not able to solve. I have installed and configured Apache Passenger Module, it works fine. Any time I try to access the virtual host I get an error 500:

我从头开始创建了一个 rails 应用程序(Rails 4.1.0),但遇到了一个我无法解决的问题。我已经安装并配置了 Apache 乘客模块,它工作正常。每当我尝试访问虚拟主机时,都会收到错误 500:

Missing secret_key_base for 'production' environment, set this value in config/secrets.yml

The secret.yml file contains the following configuration:

secret.yml 文件包含以下配置:

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

Even when it's not recommended I did this:

即使不推荐我这样做:

    production:
  secret_key_base: XXXXXXXXXXXXXXXXXXXXXXXXXXXX (random key)

But Apache logs show this:

但 Apache 日志显示:

# tailf /etc/httpd/logs/error_log
[Tue May 06 12:56:18 2014] [error] [client 192.168.3.1] Premature end of script headers:
App 4405 stderr: [ 2014-05-06 12:56:18.5763 4421/0x9d48bdc(Worker 1) utils.rb:68 ]: *** Exception RuntimeError in Rack application object (Missing `secret_key_base` for 'production' environment, set this value in `config/secrets.yml`) (process 4421, thread 0x9d48bdc(Worker 1)):
App 4405 stderr:        from /usr/local/rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/application.rb:440:in `validate_secret_key_config!'
App 4405 stderr:        from /usr/local/rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/application.rb:195:in `env_config'
App 4405 stderr:        from /usr/local/rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/engine.rb:510:in `call'
App 4405 stderr:        from /usr/local/rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/application.rb:144:in `call'
App 4405 stderr:        from /usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.41/lib/phusion_passenger/rack/thread_handler_extension.rb:74:in `process_request'
App 4405 stderr:        from /usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.41/lib/phusion_passenger/request_handler/thread_handler.rb:141:in `accept_and_process_next_request'
App 4405 stderr:        from /usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.41/lib/phusion_passenger/request_handler/thread_handler.rb:109:in `main_loop'
App 4405 stderr:        from /usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.41/lib/phusion_passenger/request_handler.rb:448:in `block (3 levels) in start_threads'

How I can fix this error? How do I access the development enviroment? I mean how I can write code, test in my browser and then deploy to Heroku or just test in production enviroment?

我该如何解决这个错误?如何进入开发环境?我的意思是如何编写代码,在浏览器中进行测试,然后部署到 Heroku 或仅在生产环境中进行测试?

Update

更新

I manage to fix the error above by setting RailsEnv developmenton VirtualHost but now I get this other error:

我设法通过RailsEnv development在 VirtualHost上进行设置来修复上述错误,但现在出现了另一个错误:

SQLite3::CantOpenException unable to open database file Rails.root: /var/www/html/moviedb

SQLite3::CantOpenException 无法打开数据库文件 Rails.root: /var/www/html/moviedb

I'm trying to use this GEMfor make queries to TheMovieDB website and get results. Any advice on this new error?

我正在尝试使用此 GEM查询 TheMovieDB 网站并获得结果。关于这个新错误有什么建议吗?

采纳答案by Geraud Puechaldou

What I did : On my production server, I create a config file (confthin.yml) for Thin (I'm using it) and add the following information :

我所做的:在我的生产服务器上,我为 Thin(我正在使用它)创建了一个配置文件 (confthin.yml) 并添加以下信息:

environment: production user: www-data group: www-data SECRET_KEY_BASE: mysecretkeyproduction

环境:生产用户:www-data 组:www-data SECRET_KEY_BASE:mysecretkeyproduction

I then launch the app with

然后我启动应用程序

thin start -C /whereeveristhefieonprod/configthin.yml

瘦启动 -C /whereeveristhefieonprod/configthin.yml

Work like a charm and then no need to have the secret key on version control

像魅力一样工作,然后不需要在版本控制上拥有密钥

Hope it could help, but I'm sure the same thing could be done with Unicorn and others.

希望它能有所帮助,但我相信 Unicorn 和其他人也可以做同样的事情。

回答by mahatmanich

Rails app secret key via bash

通过 bash 的 Rails 应用密钥

If you have access to the bashconsole of the userthat is running the rails app, and if you are not using thin, then you could provide the environment variable via the users ~/.bashrc

如果您有权访问运行 rails 应用程序的用户bash控制台,并且您没有使用,那么您可以通过用户提供环境变量thin~/.bashrc

just add the following:

只需添加以下内容:

export SECRET_KEY_BASE=`MY_SECRET_KEY`

Note: MY_SECRET_KEY could be anything but it would be secure to generate a key with rake secreton the bash console substitute that keywith "MY_SECRET_KEY" from the example.

注意:MY_SECRET_KEY 可以是任何东西,但rake secret在 bash 控制台上用示例中的“MY_SECRET_KEY”替换该密钥是安全的。

Once you have done that you should see a SECRET_KEY_BASEvariable in the hashwhich is returned when you call ENVin your rails console.

完成后,您应该会在 中看到一个SECRET_KEY_BASE变量,hash当您ENV在 Rails 控制台中调用该变量时会返回该变量。

Last but not least, reboot your rails app and the error should disappear.

最后但并非最不重要的一点是,重新启动 Rails 应用程序,错误应该会消失。

Rails app secret key per rbenv-vars

每个 rbenv-vars 的 Rails 应用密钥

Further, if you are using rbenv, you can use the rbenv-varsplugin to achive a per rails app secret key environment variable.

此外,如果您正在使用rbenv,您可以使用该rbenv-vars插件来实现每个 rails 应用程序的密钥环境变量。

A good read on this would be here!

关于这个的好书就在这里!

Using god (godrb.com):thanks hlcs

使用上帝(godrb.com):谢谢hlcs

You need to god terminateand then start again.

你需要god terminate然后重新开始。

回答by cpliu338

I solved a similar situation with this (perhaps only useful in Ubuntu-like environment). See: Missing secret key base for production environment. Hope it helps.

我用这个解决了类似的情况(也许只在类似 Ubuntu 的环境中有用)。请参阅:缺少生产环境的密钥库。希望能帮助到你。