Ubuntu 14.04 VPS Rails 上未加载环境变量

Environment variables not loaded on Ubuntu 14.04 VPS Rails

这几天让我发疯了,我不知道如何解决这个问题。我是新手,所以它可能只是一些非常简单的东西。

我使用 Digital Ocean 在 Ubuntu 14.04 上设置了我的 VPS 托管,以将我的 Rails 4 应用程序部署到生产环境。我按照本教程 https://www.digitalocean.com/community/tutorials/deploying-a-rails-app-on-ubuntu-14-04-with-capistrano-nginx-and-puma

使用 Capist运行o 和 Nginx 部署了我的应用程序

我目前唯一的问题是我的环境变量没有被我的应用程序加载。或者至少,当我尝试订阅时事通讯时,它告诉我需要一个有效的 api 密钥。

到目前为止我做了什么:

这是我的 bash_profile 文件: [[ -s "$HOME/.profile" ]] && source "$HOME/.profile" # 载入默认的.profile

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM$
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"
export MAILCHIMP_API_KEY="somenumber"
export MAILCHIMP_LIST_ID="somenumber"

如果需要,很乐意添加任何额外的代码。

非常感谢!

有同样的问题,你上面提到的一切。这是我所做的:

在 ubuntu 框的 ~/.profile 中确保加载 both .bashrc && .bash_profile

# if running bash
if [ -n "$BASH_VERSION" ]; then
  # include .bashrc if it exists
  if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
  fi
  if [-f "$HOME/.bash_profile" ]; then
    . "$HOME/.bash_profile"
  fi
fi

然后,确保从 .bash_profile.

导出环境变量
# .bash_profile
export KEY=value
etc.

重新启动你的服务器(在我的例子中,我是 运行 nginx,所以 sudo service nginx restart),然后重新启动你的应用程序(touch app/current/tmp/restart.txt)。

您的应用现在应该可以访问环境变量了。