如何解决错误“Missing` secret_key_base` for'production'environment” (Rails 4.1) [英] How to solve error "Missing `secret_key_base` for 'production' environment" (Rails 4.1)

查看:1858
本文介绍了如何解决错误“Missing` secret_key_base` for'production'environment” (Rails 4.1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从头开始创建一个rails应用程序(rails 4.1),并且遇到了一个我无法解决的奇怪问题。

每次尝试在Heroku上部署我的应用程序我得到一个错误500:


缺少 secret_key_base for '生产'环境中,将此值设置为 config / secrets.yml


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

  secret_key_base:<%= ENV [SECRET_KEY_BASE]%> 

在Heroku中,我配置了一个环境变量SECRET_KEY_BASE,其结果为rake secret命令。如果我启动heroku config,我可以看到具有正确名称和值的变量。



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

非常感谢

解决方案

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

https://gist.github.com/pablosalgadom/4d75f30517edc6230a67



我正在使用Rails 4.1与Unicorn v4.8.2,当我试图部署我的应用程序它没有正常启动,并在unicorn.log文件中,我发现此错误消息:



app error:在'production'环境中缺少'secret_key_base`,请在config / secrets.yml中设置此值(RuntimeError)



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

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

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


  1. 在生产服务器的终端中执行下一个命令:

      $ RAILS_ENV =生产rake secret 

    这会返回一个包含字母和数字的大字符串,将该代码作为GENERATED_CODE引用)。

  2. 登录您的服务器




        $ vi / etc / 

      如果您以root用户身份登录,请找到该文件并进行编辑:档案

      转至档案底部(VI中的大写字母GIFT + G)



      使用GENERATED_CODE编写环境变量(按i键写入V I),一定要在文件末尾添加一行:

        $ export SECRET_KEY_BASE = GENERATED_CODE 

      保存更改并关闭文件(我们按ESC键,然后写入:x和ENTER 在VI中保存并退出的关键)。 然而,如果您以普通用户身份登录,为此要求将其称为example_user,您需要找到其他文件之一:

        $ vi〜/ .bash_profile 
      $ vi〜/ .bash_login
      $ vi〜/ .profile

      这些文件按重要性排列,这意味着如果您有第一个文件,那么你不需要写在其他文件中。因此,如果您在您的目录〜/ .bash_profile 〜/ .profile 中找到这2个文件,您只需要写入第一个〜/ .bash_profile ,因为Linux只会读取这一个,另一个会被忽略。



      <然后我们到文件底部(在VI中为GIFT + G,大写字母G)。

      然后我们用我们的GENERATED_CODE(按下i键写入VI),请确保在文件末尾有新行:

        $ export SECRET_KEY_BASE = GENERATED_CODE 

      编写代码后,保存更改并关闭文件(我们将 ESC键,然后写入:x和ENTER键保存并在VI中退出)。


      $ b

        $ printenv | grep SECRET_KEY_BASE 

      或与:

      <$ p $当你执行这个命令时,如果一切都完成了,你就可以执行这个命令了。好吧,它会显示你之前的GENERATED_CODE。最后,在完成所有配置后,您应该可以毫无问题地使用Unicorn或其他方式部署您的Rails应用程序。 你关闭你的shell终端并重新登录到生产服务器,你将拥有这个环境变量并准备好使用它。



      这就是它!我希望这个迷你指南帮助你解决这个错误。



      免责声明:我不是Linux或Rails专家,所以如果你发现错误或任何错误,我会很高兴解决它!


      I've created a rails app (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 app 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 have configured an environment variable "SECRET_KEY_BASE" with the result of "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?

      Thanks a lot

      解决方案

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

      https://gist.github.com/pablosalgadom/4d75f30517edc6230a67

      I was using Rails 4.1 with Unicorn v4.8.2, when I tried to deploy my app 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 this 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 the next command:

        $ RAILS_ENV=production rake secret
        

        This returns a large string with letters and numbers, copy that (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 ("SHIFT + G" for capital G in VI)

          Write your environment variable with the GENERATED_CODE (Press "i" key to write 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 (we push "ESC" key and then write ":x" and "ENTER" key for save and exit in VI).

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

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

          These files are in order of importance, that means that if you have the first file, then you wouldn't need to write in the others. So if you found this 2 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 ("SHIFT + G" for capital G in VI).

          And we will write our environment variable with our GENERATED_CODE (Press "i" key to write in VI), be sure to be in 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 (we push "ESC" key and then write ":x" and "ENTER" key for save and exit in VI).

      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 app with Unicorn or other.

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

      And thats it!! I hope this mini guide help you to 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!

      这篇关于如何解决错误“Missing` secret_key_base` for'production'environment” (Rails 4.1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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