SemiFixed: Missing `secret_key_base` for 'production' environment

22,476

Solution 1

What I did : On my production server, I create a config file (confthin.yml) for Thin (I'm using it) and add the following information :

environment: production user: www-data group: www-data SECRET_KEY_BASE: mysecretkeyproduction

I then launch the app with

thin start -C /whereeveristhefieonprod/configthin.yml

Work like a charm and then no need to have the secret key on version control

Hope it could help, but I'm sure the same thing could be done with Unicorn and others.

Solution 2

Rails app secret key via bash

If you have access to the bashconsole of the user that is running the rails app, and if you are not using thin, then you could provide the environment variable via the users ~/.bashrc

just add the following:

export SECRET_KEY_BASE=`MY_SECRET_KEY`

Note: MY_SECRET_KEY could be anything but it would be secure to generate a key with rake secret on the bash console substitute that key with "MY_SECRET_KEY" from the example.

Once you have done that you should see a SECRET_KEY_BASE variable in the hash which is returned when you call ENV in your rails console.

Last but not least, reboot your rails app and the error should disappear.

Rails app secret key per rbenv-vars

Further, if you are using rbenv, you can use the rbenv-vars plugin to achive a per rails app secret key environment variable.

A good read on this would be here!

Using god (godrb.com): thanks hlcs

You need to god terminate and then start again.

Solution 3

I solved a similar situation with this (perhaps only useful in Ubuntu-like environment). See: Missing secret key base for production environment. Hope it helps.

Share:
22,476
ReynierPM
Author by

ReynierPM

A passionate programmer and web developer with a background in front-end and back-end development, which is what he's been doing for over eight years. I had experience in web development using PHP (mostly), MySQL and JavaScript. I follows two major principles everyday work: beauty and simplicity. I believes everyone should learn something new every day. While I'm not working, I spends time coding personal projects, learning, watching screen casts, blogging, etc. Some specific areas of interest for me include cloud computing and anything related to web development among other like system and database administration.

Updated on July 09, 2022

Comments

  • ReynierPM
    ReynierPM almost 2 years

    I've created a rails app (Rails 4.1.0) from scratch and I am getting a problem that I am not able to solve. I have installed and configured Apache Passenger Module, it works fine. Any time I try to access the virtual host I get an error 500:

    Missing secret_key_base for 'production' environment, set this value in config/secrets.yml
    

    The secret.yml file contains the following configuration:

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

    Even when it's not recommended I did this:

        production:
      secret_key_base: XXXXXXXXXXXXXXXXXXXXXXXXXXXX (random key)
    

    But Apache logs show this:

    # tailf /etc/httpd/logs/error_log
    [Tue May 06 12:56:18 2014] [error] [client 192.168.3.1] Premature end of script headers:
    App 4405 stderr: [ 2014-05-06 12:56:18.5763 4421/0x9d48bdc(Worker 1) utils.rb:68 ]: *** Exception RuntimeError in Rack application object (Missing `secret_key_base` for 'production' environment, set this value in `config/secrets.yml`) (process 4421, thread 0x9d48bdc(Worker 1)):
    App 4405 stderr:        from /usr/local/rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/application.rb:440:in `validate_secret_key_config!'
    App 4405 stderr:        from /usr/local/rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/application.rb:195:in `env_config'
    App 4405 stderr:        from /usr/local/rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/engine.rb:510:in `call'
    App 4405 stderr:        from /usr/local/rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/application.rb:144:in `call'
    App 4405 stderr:        from /usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.41/lib/phusion_passenger/rack/thread_handler_extension.rb:74:in `process_request'
    App 4405 stderr:        from /usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.41/lib/phusion_passenger/request_handler/thread_handler.rb:141:in `accept_and_process_next_request'
    App 4405 stderr:        from /usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.41/lib/phusion_passenger/request_handler/thread_handler.rb:109:in `main_loop'
    App 4405 stderr:        from /usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.41/lib/phusion_passenger/request_handler.rb:448:in `block (3 levels) in start_threads'
    

    How I can fix this error? How do I access the development enviroment? I mean how I can write code, test in my browser and then deploy to Heroku or just test in production enviroment?

    Update

    I manage to fix the error above by setting RailsEnv development on VirtualHost but now I get this other error:

    SQLite3::CantOpenException unable to open database file Rails.root: /var/www/html/moviedb

    I'm trying to use this GEM for make queries to TheMovieDB website and get results. Any advice on this new error?

  • Artem P
    Artem P over 8 years
    For those who using god you need terminate god (god terminate) and than start again.
  • Daniel
    Daniel over 7 years
    I get the error gems/thin-1.7.0/lib/thin/runner.rb:147:in `parse!': invalid option: --SECRET-KEY-BASE=<key> (OptionParser::InvalidOption)