如何解决错误“‘生产’环境缺少‘secret_key_base’"(导轨 4.1) [英] How to solve error "Missing `secret_key_base` for 'production' environment" (Rails 4.1)

查看:27
本文介绍了如何解决错误“‘生产’环境缺少‘secret_key_base’"(导轨 4.1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Rails 4.1 从头开始​​创建了一个 Rails 应用程序,但遇到了一个我无法解决的奇怪问题.

每次我尝试在 Heroku 上部署我的应用程序时,我都会收到错误 500:

生产"环境缺少secret_key_base",请在config/secrets.yml"中设置此值

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

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

在 Heroku 上,我使用 rake secret 命令的结果配置了SECRET_KEY_BASE"环境变量.如果我启动 heroku config,我可以看到具有正确名称和值的变量.

为什么我仍然收到此错误?

解决方案

我也遇到了同样的问题,通过创建一个环境变量来解决它,每次登录到生产服务器时都会加载,并做了一个迷你指南 配置它的步骤:

我使用 Rails 4.1 和 Unicorn v4.8.2,当我尝试部署我的应用程序时,它没有正确启动,在 unicorn.log 文件中,我发现了以下错误消息:

应用程序错误:生产"环境缺少secret_key_base",请在config/secrets.yml"中设置此值(运行时错误)

经过一些研究,我发现 Rails 4.1 改变了管理 secret_key 的方式,所以如果你阅读位于 exampleRailsProject/的 secrets.yml 文件config/secrets.yml 你会发现这样的东西:

# 不要在存储库中保留生产机密,# 而是从环境中读取值.生产:secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

这意味着 Rails 建议您在生产服务器中为 secret_key_base 使用环境变量.为了解决这个错误,你应该按照以下步骤在你的生产服务器中为 Linux(在我的例子中是 Ubuntu)创建一个环境变量:

  1. 在您的生产服务器的终端中执行:

    $ RAILS_ENV=生产耙秘密

    这将返回一个包含字母和数字的大字符串.复制它,我们将该代码称为 GENERATED_CODE.

  2. 登录您的服务器

    • 如果您以 root 用户身份登录,请找到该文件并进行

      $ vi/etc/profile

      在 vi 中使用 Shift+G(大写G")转到文件底部.

      使用GENERATED_CODE 编写环境变量,按i 插入vi.确保在文件末尾换行:

      $ export SECRET_KEY_BASE=GENERATED_CODE

      保存更改并使用 Esc 关闭文件,然后使用:x"和 Enter 保存并退出 vi.p>

    • 但是,如果您以普通用户身份登录,在本要点中我们将其称为example_user",您将需要找到以下其他文件之一:

      $ vi ~/.bash_profile$ vi ~/.bash_login$ vi ~/.profile

      这些文件按重要性排序,这意味着如果您拥有第一个文件,那么您就不需要编辑其他文件.如果您在目录 ~/.bash_profile~/.profile 中找到这两个文件,则只需写入第一个 ~/.bash_profile,因为 Linux 只会读取这个,另一个会被忽略.

      然后我们再次使用 Shift+G 转到文件底部,并使用 用我们的 GENERATED_CODE 写入环境变量我再次,并确保在文件末尾添加一个新行:

      $ export SECRET_KEY_BASE=GENERATED_CODE

      编写代码后,再次使用 Esc 保存更改并关闭文件,然后使用:x"和 Enter 保存并退出.

  3. 您可以使用以下命令验证我们的环境变量是否在 Linux 中正确设置:

    $ printenv |grep SECRET_KEY_BASE

    或与:

    $ echo $SECRET_KEY_BASE

    当你执行这个命令时,如果一切顺利,它会显示你之前的GENERATED_CODE.最后,完成所有配置后,您应该能够使用 Unicorn 或其他工具部署您的 Rails 应用程序,而不会出现问题.

当您关闭 shell 并再次登录到生产服务器时,您将设置此环境变量并准备好使用它.

就是这样!我希望这个迷你指南可以帮助您解决这个错误.

免责声明:我不是 Linux 或 Rails 专家,因此如果您发现错误或任何错误,我很乐意修复它.

I created a Rails application, using Rails 4.1, from scratch and I am facing a strange problem that I am not able to solve.

Every time I try to deploy my application on Heroku I get an error 500:

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

The secret.yml file contains the following configuration:

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

On Heroku I configured the "SECRET_KEY_BASE" environment variable with the result of the rake secret command. If I launch heroku config, I can see the variable with the correct name and value.

Why am I still getting this error?

解决方案

I had the same problem and solved it by creating an environment variable to be loaded every time I logged in to the production server, and made a mini-guide of the steps to configure it:

I was using Rails 4.1 with Unicorn v4.8.2 and when I tried to deploy my application it didn't start properly and in the unicorn.log file I found this error message:

app error: Missing `secret_key_base` for 'production' environment, set this value in `config/secrets.yml` (RuntimeError)

After some research I found out that Rails 4.1 changed the way to manage the secret_key, so if you read the secrets.yml file located at exampleRailsProject/config/secrets.yml you'll find something like this:

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

This means that Rails recommends you to use an environment variable for the secret_key_base in your production server. In order to solve this error you should follow these steps to create an environment variable for Linux (in my case Ubuntu) in your production server:

  1. In the terminal of your production server execute:

    $ RAILS_ENV=production rake secret
    

    This returns a large string with letters and numbers. Copy that, which we will refer to that code as GENERATED_CODE.

  2. Login to your server

    • If you login as the root user, find this file and edit it:

      $ vi /etc/profile
      

      Go to the bottom of the file using Shift+G (capital "G") in vi.

      Write your environment variable with the GENERATED_CODE, pressing i to insert in vi. Be sure to be in a new line at the end of the file:

      $ export SECRET_KEY_BASE=GENERATED_CODE
      

      Save the changes and close the file using Esc and then ":x" and Enter for save and exit in vi.

    • But if you login as normal user, let's call it "example_user" for this gist, you will need to find one of these other files:

      $ vi ~/.bash_profile
      $ vi ~/.bash_login
      $ vi ~/.profile
      

      These files are in order of importance, which means that if you have the first file, then you wouldn't need to edit the others. If you found these two files in your directory ~/.bash_profile and ~/.profile you only will have to write in the first one ~/.bash_profile, because Linux will read only this one and the other will be ignored.

      Then we go to the bottom of the file using Shift+G again and write the environment variable with our GENERATED_CODE using i again, and be sure add a new line at the end of the file:

      $ export SECRET_KEY_BASE=GENERATED_CODE
      

      Having written the code, save the changes and close the file using Esc again and ":x" and Enter to save and exit.

  3. You can verify that our environment variable is properly set in Linux with this command:

    $ printenv | grep SECRET_KEY_BASE
    

    or with:

    $ echo $SECRET_KEY_BASE
    

    When you execute this command, if everything went ok, it will show you the GENERATED_CODE from before. Finally with all the configuration done you should be able to deploy without problems your Rails application with Unicorn or some other tool.

When you close your shell and login again to the production server you will have this environment variable set and ready to use it.

And that's it! I hope this mini-guide helps you solve this error.

Disclaimer: I'm not a Linux or Rails guru, so if you find something wrong or any error I will be glad to fix it.

这篇关于如何解决错误“‘生产’环境缺少‘secret_key_base’"(导轨 4.1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆