Question

So I am trying to deploy my rails app in production. When I go to the page I get a 500 error. When I go to my error logs I get the following error:

Exception RuntimeError in Rack application object (Missing `secret_key_base` for 'production' environment, set this value in `config/secrets.yml`) 

I am running Rails 4.1 and my config/secrets.yml looks like this:

    development:
      secret_key_base: <development key>        
    test:
      secret_key_base: <test key>

    # Do not keep production secrets in the repository,
    # instead read values from the environment.
    production:
      secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

I ran rake secret to get the key and put the export in my bash_profile and sourced it. I ran rake assets:precompile successfully. Yet I still keep getting this error. Any ideas?

Update: I tried to update the error message provided to give slightly better information....and the message didn't update. I then tried adding the key directly to the yml file instead of using an environment variable and still no dice. Im running on hostmonster so I can't restart the server.....but something is telling me thats what needs to be done...

Update 2: After sleeping through the night it seems that this issue is no longer an issue. It must have been some sort of caching. Now my issue is that its trying to use an old config that i changed days ago for my database. If I figure out how to nullify the cache I will post it here and mark it as an answer. If someone else knows how to do it please let me know and I will mark it as an answer. I am using HostMonster as my hosting and followed the steps they have on their site for hosting my rails app.

Was it helpful?

Solution 2

  1. You need to restart your server, because after YourAppName::Application.initialize! called in config/environment.rb you can not change your settings.
  2. Checkout your yml markup, probably there some errors
  3. Probably something wrong in your config/initializers/secret_token.rb

The problem is not with ENV pseudo-hash. secret_key_base will be nil if in ENV no such a key.

OTHER TIPS

I had the same problem and I solved creating an environment variable to be loaded every time that I login to the production server and made a mini guide of the steps to configure it by your self:

So I was using Rails 4.1 with Unicorn v4.8.2 and when I tried to deploy my app it doesn't start properly and into the unicorn.log file i found this error message:

app error: Missing secret_key_base for 'production' environment, set this value in config/secrets.yml (RuntimeError)

After a little research I found that Rails 4.1 change the way to manage the secret_key, so if we read the secrets.yml file located at exampleRailsProject/config/secrets.yml (you need to replace "exampleRailsProject" for your project name) you will find something like this:

# Do not keep production secrets in the repository,
# instead read values from the environment.
production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

This means that rails recommends you to use an environment variable for the secret_key_base in our production server, so in order to solve this error you will need to follow this steps to create an environment variable for linux (in my case it is Ubuntu) in our production server:

1.- In the terminal of our production server you will execute the next command:

$ RAILS_ENV=production rake secret

This will give a large string with letters and numbers, this is what you need, so copy that (we will refer to that code as GENERATED_CODE).

2.1- Now if we login as root user to our server we will need to find this file and open it:

$ vi /etc/profile

Then we go to the bottom of the file ("SHIFT + G" for capital G in VI)

And we will write our environment variable with our GENERATED_CODE (Press "i" key to write in VI), be sure to be in a new line at the end of the file:

export SECRET_KEY_BASE=GENERATED_CODE

Having written the code we save the changes and close the file (we push "ESC" key and then write ":x" and "ENTER" key for save and exit in VI)

2.2 But if we login as normal user, lets call it example_user for this gist, we will need to find one of this other files:

$ vi ~/.bash_profile
$ vi ~/.bash_login
$ vi ~/.profile

These files are in order of importance, that means that if you have the first file, then you wouldn't need to write in the others. So if you found this 2 files in your directory "~/.bash_profile" and "~/.profile" you only will have to write in the first one "~/.bash_profile", because linux will read only this one and the other will be ignored.

Then we go to the bottom of the file ("SHIFT + G" for capital G in VI)

And we will write our environment variable with our GENERATED_CODE (Press "i" key to write in VI), be sure to be in a new line at the end of the file:

export SECRET_KEY_BASE=GENERATED_CODE

Having written the code we save the changes and close the file (we push "ESC" key and then write ":x" and "ENTER" key for save and exit in VI)

3.-We can verify that our environment variable is properly set in linux with this command:

$ printenv | grep SECRET_KEY_BASE

or with:

$ echo $SECRET_KEY_BASE

When you execute this command, if everything went ok, it will show you the GENERATED_CODE that we generated before. Finally with all the configuration done you can deploy without problems your Rails app with Unicorn or other.

Now when you close your shell terminal and login again to the production server you will have this environment variable set and ready to use it.

And Thats it!! I hope this mini guide help you to solve this error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top