2

When I navigate to my Rails production site, I get:

*** Exception RuntimeError in Rack application object (Missing `secret_key_base` for 'production' environment, set this value in `config/secrets.yml`) (process 29032, thread 0x9b81a60(Worker 1)):

I see many solutions to this error is to export a environment variable with export SECRET_KEY_BASE='a long string generated by running rake secret

However that didn't work for me (I get the same error). I had to include in config/environments/production.rb: config.secret_key_base = "a long string generated by running rake secret" to solve this error.

My question is, is this the right way, since the practice is to not commit the secret key? Is there any way to make Rails read from the environment variable?

I'm using Ubuntu 14.04.1, Apache2, Phusion Passenger stack.

resting
  • 16,287
  • 16
  • 59
  • 90
  • Follow the Link below
    [http://stackoverflow.com/questions/23180650/how-to-solve-error-missing-secret-key-base-for-production-environment-on-h](http://stackoverflow.com/questions/23180650/how-to-solve-error-missing-secret-key-base-for-production-environment-on-h) cheers
    – Riliwan Balogun Nov 15 '15 at 21:44

2 Answers2

7

You are using environment variables incorrectly. Setting an environment variable in bash has no effect on Apache and on Phusion Passenger + Apache (the same is true with Nginx), because environment variables are inherited on a per-process basis and are not system-wide. The Phusion Passenger documentation has a long section explanation why this, and how you can fix it.

Hongli
  • 18,682
  • 15
  • 79
  • 107
0

You either do not commit the complete secrets.yml into repository and deploy it somehow else (via chef or puppet ) or your let it read the secrets from ENV vars in production

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

Having them as ENV vars is one the best practices of so called 12 factor apps. But this is only a better solution if you can achieve not store them within the filesystem on your server, but rather be only available to the rails process. This can be achieved with docker.

dre-hh
  • 7,840
  • 2
  • 33
  • 44
  • Ya, the `secrets.yml` has the code that you show. But somehow rails is not picking up the ENV vars. I had to set it manually in `config/environments/production.rb`. Maybe its a phusion passenger thing, since I see most people deploy to heroku or nginx. – resting Sep 15 '14 at 13:20
  • Yeah, it seems that the ENV vars are not being set properly. What do you use passenger with, if not with nginx? – dre-hh Sep 15 '14 at 13:26
  • It has got nothing to do with Apache vs Nginx, and everything to do with the nature of environment variables. See my above answer. – Hongli Sep 15 '14 at 18:31
  • So how do you set the ENV vars? – pbreitenbach May 22 '15 at 05:22
  • http://blog.carbonfive.com/2015/03/17/docker-rails-docker-compose-together-in-your-development-workflow/ checkout the part about env_file directive – dre-hh May 22 '15 at 11:46