Question

How to set up environment variables on Ubuntu 14, Nginx, Capistrano and Rails?

this is driving me crazy for a few days now and I can’t figure out how to solve this. I am new to this so it might just be something very simple.

I set up my VPS hosting on Ubuntu 14.04 using Digital Ocean to deploy my Rails 4 app to a production environment. I deployed my app using Capistrano and Nginx by following this tutorial https://www.digitalocean.com/community/tutorials/deploying-a-rails-app-on-ubuntu-14-04-with-capistrano-nginx-and-puma

The only problem that I currently have is that my environment variables are not loaded by my app. Or at least, when I try to subscribe to my newsletter, it tells me that a valid api key is required.

What I did so far:

  • Installed rbenv-var as instructed in the documentation.
  • Created a .rbenv-var file in my /appname/current folder
  • When I run rbenv vars, everything looks OK
  • ran source ~/.bash_profile (found this somewhere that could solve the problem, but it didnt)
  • Tried “spring stop” as indicated somewhere but didn’t work either. Spring was not installed
  • I also tried adding my variables to my ~/.bashrc file, that didn’t work either
  • Tried adding them to my ~/.bash_profile file, ran source ~/.bash_profile but that didn’t work either.

This is my bash_profile file:

[[ -s "$HOME/.profile" ]] && source "$HOME/.profile" # Load the default .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"

Happy to add any extra code if required.

Thanks a lot!

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

I’m also new to this kind of thing so forgive me if this is the n00b way to do it:

  • I put all my environment variables in etc/environment like so:
    export SOME_ENV_VAR=some_value
  • You have to log out and back into your Ubuntu user account for the vars to become usable.
  • If Capistrano needs the vars before the task is run, in your deploy.rb use:

set :default_env, { ‘SOME_ENV_VAR’ => ‘some_value’ }

took a while to figure out… Put the variable to /etc/default/unicorn.