【问题标题】:Deployed Rails missing secret key部署的 Rails 缺少密钥
【发布时间】:2014-11-02 16:12:20
【问题描述】:

我正在尝试将我的 Rails 应用程序部署到我的 Digital Ocean VPS。我已经完成了GoRails guide 中的所有步骤,但是当我部署我的项目并访问我的 IP 地址时,我收到一条消息:

An error occurred.

Sorry, the page you are looking for is currently unavailable.
Please try again later.

If you are the system administrator of this resource then you should check the error log for details.

Faithfully yours, nginx.

我检查了显示我的 nginx 错误日志:

Exception RuntimeError in Rack application object (Missing `secret_key_base` for 'production' environment, set this value in `config/secrets.yml`) (process 2490, thread 0x007fbd33665ce0(Worker 1)):

在 VPS 上,我通过运行 rake secret 然后运行 ​​export SECRET_KEY_BASE= + key 创建了一个秘密。现在当我echo $SECRET_KEY_BASE 时,它会显示我的钥匙。但我仍然收到错误消息。

在我的deploy.rb 我有这个:

set :linked_files, %w{config/database.yml config/secrets.yml}

我在 VPS 上的secrets.yml 是这样的:

development:
  secret_key_base: 89dacb16fd905ff4c6352ac93f4676a5dd&^%3f93edce9a5be796712d54b57b91e1335598fd73e3998fddbbdeaf3ee7f65157f2fb01ce1bea5658aa7bf745d1f

test:
  secret_key_base: cf351585b2cb43459f5a073cbfd885b3dd2af44124f13a855522f678c1cf06625c121cd3b7857&&6e7fe2ba11180066753142143231c79c513e71e20372a0462

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

刚刚重启了nginx,还是没有结果。

更新

deploy@movieseat:~$ ps aux | grep nginx
root      4545  0.0  0.6  98936  3360 ?        Ss   11:32   0:00 nginx: master process         /usr/sbin/nginx
www-data  4548  0.0  0.9  99304  4608 ?        S    11:32   0:00 nginx: worker process
www-data  4549  0.0  0.9  99304  4608 ?        S    11:32   0:00 nginx: worker process
www-data  4550  0.0  0.9  99304  4608 ?        S    11:32   0:00 nginx: worker process
www-data  4551  0.0  0.9  99304  4608 ?        S    11:32   0:00 nginx: worker process
deploy    4655  0.0  0.3   9436  1660 pts/1    S+   11:33   0:00 grep --color=auto nginx

【问题讨论】:

  • 确保 SECRET_KEY_BASE 设置为与 nginx 和乘客运行相同的用户。
  • 我该如何检查?我使用ssh deploy@***.**.***.** 登录,然后运行所有命令。我还以该用户身份重新启动了 nginx。所以我认为 nginx 和乘客从部署用户运行。
  • 它没有显示任何输出。 deploy@movieseat:~$ ls -la | grep nginx deploy@movieseat:~$
  • 对不起,我在睡觉,我的意思是:ps aux | grep nginx
  • @PeterBoomsma :试试这个:1:运行:RAILS_ENV=production rake secret,2:打开你的 bashrc:vi ~/.bashrc,把它放进去:export SECRET_KEY_BASE = value_you_get_from_rake_secret_command_without_quote 例如:export SECRET_KEY_BASE=abjdbjd28hewu83tg,3:运行:source ~/.bashrc或者重新登录服务器,4:重启rails服务器。

标签: ruby-on-rails ruby nginx


【解决方案1】:

我假设您没有签入存储库中的 secrets.yml 文件,因为您已将其定义为 deploy.rb 中的链接文件,即

set :linked_files, %w{config/database.yml config/secrets.yml}

您可以像 database.yml 一样对 secrets.yml 执行相同的操作。我的建议是将 secret_key_base 本身的值放在 secret.yml 文件中,而不是从 ENV 中读取它,因为有权访问部署服务器中的链接文件的人无论如何都可以访问 ENV 变量.

但是,如果您在存储库中添加了 secrets.yml 文件,则尝试从系统环境中读取值是有意义的。

【讨论】:

  • 好的,我已经在我的 VPS 上创建了一个秘密,并将其添加到我的 secrets.yml,也在 VPS 上。我收到We're sorry, but something went wrong. If you are the application owner check the logs for more information. 控制台现在显示Failed to load resource: the server responded with a status of 500 (Internal Server Error) 错误。当我访问该页面时,我来自 nginx 的错误日志没有记录任何内容。这可能是件好事。
  • I, [2014-11-02T12:54:45.760056 #10530] INFO -- : Completed 500 Internal Server Error in 2ms F, [2014-11-02T12:54:45.762918 #10530] FATAL -- : ActiveRecord::StatementInvalid (Could not find table 'users'): app/controllers/application_controller.rb:18:in current_user' app/helpers/sessions_helper.rb:26:in logged_in?' app/controllers/movies_controller.rb:5:in index'` 是我现在得到的。所以要检查一下数据库出了什么问题。
  • @PeterBoomsma:最近的错误有所不同。它抱怨没有用户表。您是否在部署期间或之后运行迁移?
  • 是的,我看到了:)。我感觉差不多完成了。约 3 天后。我已经完成了rake db:create 和 rake db:migrate`,但这并没有做任何事情。然后我查看了 db 文件夹,发现 production.sqlite3 文件是空的。所以我想这就是现在的问题。好吧,我猜我是完整的圈子,因为我之前有过这个,现在又一次。 bundle exec RAILS_ENV=production rake db:create bundler: command not found: RAILS_ENV=production
  • 你能在执行 bundle 命令或执行 'which ruby​​' 之前检查是否加载了 ruby​​ 版本管理器吗?
猜你喜欢
  • 2019-03-20
  • 2016-04-25
  • 2018-01-26
  • 1970-01-01
  • 2017-03-09
  • 1970-01-01
  • 2019-02-11
  • 1970-01-01
  • 2016-08-29
相关资源
最近更新 更多