首页 > 解决方案 > 为什么我的 Rails 应用程序打不开?即使我使用命令($ heroku config:set ~ 等)设置了环境变量 'SECRET_KEY_BASE'

问题描述

我的ruby版本是2.6.6,我的mysql版本是8.0.23,我的mysql2版本是0.5.3,我的bundler版本是2.2.21,我的rails版本是5.0.7.2,我的Xcode版本是12.5。

我使用 macOS Big Sur(11.4 版)和文本编辑器“Atom”。

我计划使用 HEROKU 的 URL 发布我的 Rails 应用程序(例如 https://[My APP Name].herokuapp.com)。

我运行了以下命令。

$ heroku open

发生以下错误。

An unhandled lowlevel error occurred. The application logs may have details.

我运行了以下命令。

$ heroku logs

#<RuntimeError: Missing
 `secret_key_base` for 'production' environment, set this value in `
config/secrets.yml`>

我使用以下命令设置了两个环境变量:$ heroku config:set [ENVIRONMENT VARIABLE NAME]。

$ heroku config:set DATABASE_USERNAME=****
$ heroku config:set DATABASE_PASSWORD=****

我的 config/secrets.yml 文件如下所示。

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

我运行了以下命令。

$ bundle exec rake secret

GENERATED_CODE

我运行了以下命令。

$ heroku config:set SECRET_KEY_BASE=GENERATED_CODE

Setting SECRET_KEY_BASE and restarting ⬢ [My APP Name]... done, v24
SECRET_KEY_BASE: GENERATED_CODE

我如下设置环境变量SECRET_KEY_BASE

$ vi /etc/profile

SECRET_KEY_BASE我在 Vim 编辑器中设置了如下环境变量。

-- INSERT --
# System-wide .profile for sh(1)

if [ -x /usr/libexec/path_helper ]; then
        eval `/usr/libexec/path_helper -s`
fi
if [ "${BASH-no}" != "no" ]; then
        [ -r /etc/bashrc ] && . /etc/bashrc
fi
if [ -d /etc/profile.d ]; then
  # commands go here
fi
if [ -f /etc/profile ]; then
  # commands go here
fi
if [ -d /etc/profile.d -a -f /etc/profile ]; then
  # both are true

fi
if [ -d /etc/profile.d -o -f /etc/profile ]; then
  # at least one is true
fi

export SECRET_KEY_BASE=GENERATED_CODE

我使用以下两个命令保存了上述SECRET_KEY_BASE内容并结束了 Vim 编辑器。

:w !sudo tee %
:q!

我使用以下命令再次打开 vim 编辑器$ vi /etc/profile

# System-wide .profile for sh(1)

if [ -x /usr/libexec/path_helper ]; then
        eval `/usr/libexec/path_helper -s`
fi
if [ "${BASH-no}" != "no" ]; then
        [ -r /etc/bashrc ] && . /etc/bashrc
fi
if [ -d /etc/profile.d ]; then
  # commands go here
fi
if [ -f /etc/profile ]; then
  # commands go here
fi
if [ -d /etc/profile.d -a -f /etc/profile ]; then
  # both are true

fi
if [ -d /etc/profile.d -o -f /etc/profile ]; then
  # at least one is true
fi

export SECRET_KEY_BASE=GENERATED_CODE

我运行了以下命令。

$ vi ~/.bash_profile

我在~/.bash_profile.

export SECRET_KEY_BASE=GENERATED_CODE
~
~
~
~
~
~
~
~
~
~
~
~
"~/.bash_profile" 1L, 152B

我保存了上面的内容SECRET_KEY_BASE,使用Esc然后“ :x”和Enter

我验证环境变量SECRET_KEY_BASE在 Linux 中设置正确。因为我运行了以下三个命令。

$ printenv | grep SECRET_KEY_BASE

SECRET_KEY_BASE=GENERATED_CODE
$ echo $SECRET_KEY_BASE

GENERATED_CODE
$ heroku config

=== [My APP Name] Config Vars
DATABASE_PASSWORD: ****
DATABASE_URL: postgres://****
DATABASE_USERNAME: ****
HEROKU_POSTGRESQL_AQUA_URL: postgres://****
HEROKU_POSTGRESQL_BLACK_URL: postgres://****
LANG: en_US.UTF-8
RACK_ENV: production
RAILS_ENV: production
RAILS_LOG_TO_STDOUT: enabled
RAILS_SERVE_STATIC_FILES: enabled
SECRET_KEY_BASE: GENERATED_CODE

我检查了是否有/etc/profile.d目录和/etc/profile文件。因为我运行了以下命令。

$ sudo ls -lh /etc/profile

-r--r--r--  1 DATABASE_USERNAME  wheel   599B Aug  8 15:08 /etc/profile

我运行了以下命令。

$ heroku open

Web 浏览器中出现以下错误。

An unhandled lowlevel error occurred. The application logs may have details.

我运行了以下命令。

$ heroku logs

#<RuntimeError: Missing
 `secret_key_base` for 'production' environment, set this value in `config/secrets.yml`>

为什么我的 Rails 应用程序打不开?即使我使用以下命令设置了环境变量“SECRET_KEY_BASE”。

$ heroku config:set SECRET_KEY_BASE=GENERATED_CODE
$ vi /etc/profile
$ vi ~/.bash_profile

标签: ruby-on-railsbashheroku

解决方案


推荐阅读