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 inconfig/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
Demi Magus answer worked for me until Rails 5.
On Apache2/Passenger/Ruby (2.4)/Rails (5.1.6), I had to put
from Demi Magus answer in /etc/apache2/envvars, cause /etc/profile seems to be ignored.
Source: https://www.phusionpassenger.com/library/indepth/environment_variables.html#apache
Add
config/secrets.yml
to version control and deploy again. You might need to remove a line from.gitignore
so that you can commit the file.I had this exact same issue and it just turned out that the boilerplate
.gitignore
Github created for my Rails application includedconfig/secrets.yml
.this is works good https://gist.github.com/pablosalgadom/4d75f30517edc6230a67 for root user should edit
but if you non root should put the generate code in the following
While you can use initializers like the other answers, the conventional Rails 4.1+ way is to use the
config/secrets.yml
. The reason for the Rails team to introduce this is beyond the scope of this answer but the TL;DR is thatsecret_token.rb
conflates configuration and code as well as being a security risk since the token is checked into source control history and the only system that needs to know the production secret token is the production infrastructure.You should add this file to
.gitignore
much like you wouldn't addconfig/database.yml
to source control either.Referencing Heroku's own code for setting up
config/database.yml
fromDATABASE_URL
in their Buildpack for Ruby, I ended up forking their repo and modified it to createconfig/secrets.yml
fromSECRETS_KEY_BASE
environment variable.Since this feature was introduced in Rails 4.1, I felt it was appropriate to edit
./lib/language_pack/rails41.rb
and add this functionality.The following is the snippet from the modified buildpack I created at my company:
You can of course extend this code to add other secrets (e.g. third party API keys, etc.) to be read off of your environment variable:
This way, you can access this secret in a very standard way:
Before redeploying your app, be sure to set your environment variable first:
Then add your modified buildpack (or you're more than welcome to link to mine) to your Heroku app (see Heroku's documentation) and redeploy your app.
The buildpack will automatically create your
config/secrets.yml
from your environment variable as part of the dyno build process every time yougit push
to Heroku.EDIT: Heroku's own documentation suggests creating
config/secrets.yml
to read from the environment variable but this implies you should check this file into source control. In my case, this doesn't work well since I have hardcoded secrets for development and testing environments that I'd rather not check in.This worked for me.
SSH into your production server and
cd
into your current directory, runbundle exec rake secret
orrake secret
, you will get a long string as an output, copy that string.Now run
sudo nano /etc/environment
.Paste at the bottom of the file
Where
rake secret
is the string you just copied, paste that copied string in place ofrake secret
.Restart the server and test by running
echo $SECRET_KEY_BASE
.On Nginx/Passenger/Ruby (2.4)/Rails (5.1.1) nothing else worked except:
passenger_env_var
in/etc/nginx/sites-available/default
in the server block.Source: https://www.phusionpassenger.com/library/config/nginx/reference/#passenger_env_var