Rails 4.1.4 Production environment, Ubuntu Apache/Passenger

August 4, 2014 12.5k views

I'm getting the error

App 2280 stderr: [ 2014-08-04 18:45:00.6935 2301/0x9a59598(Worker 1) utils.rb:72 ]: *** Exception RuntimeError in Rack application object (Missing secret_key_base for 'production' environment, set this value in config/secrets.yml) (process 2301, thread 0x9a59598(Worker 1)):

But I'm sure I'd run export SECRET_KEY_BASE=xxx and ruby -e 'p ENV["SECRET_KEY_BASE"]'. Have went through a page of Google search results. Not sure what else I'm missing.
Setup is as per title.

Side question. Is there anyway we can find back questions we'd asked? I found a solution to my pass problem but I have no idea how to get the question back.

3 Answers

Try adding it to config/secrets.yml:

production:
  secret_key_base: XXX

Is there anyway we can find back questions we'd asked? I found a solution to my pass problem but I have no idea how to get the question back.

That's not supported at the moment, however, it should be soon :) You can search for it until then (using the search bar at the very top of this page).

  • I added the secret_key_base directly to config/secrets.yml, Now I get undefined symbol method symbolize keys.

    Here's the log:

    App 17202 stderr: [ 2014-08-05 04:18:21.2312 17219/0x9c3d738(Worker 1) utils.rb:72 ]: *** Exception NoMethodError in Rack application object (undefined method `symbolize_keys' for #<String:0x9dabe44>) (process 17219, thread 0x9c3d738(Worker 1)):
    App 17202 stderr:       from /var/www/html/rails/resume/vendor/bundle/ruby/2.1.0/gems/railties-4.1.4/lib/rails/application.rb:327:in `secrets'
    App 17202 stderr:       from /var/www/html/rails/resume/vendor/bundle/ruby/2.1.0/gems/railties-4.1.4/lib/rails/application.rb:451:in `validate_secret_key_config!'
    App 17202 stderr:       from /var/www/html/rails/resume/vendor/bundle/ruby/2.1.0/gems/railties-4.1.4/lib/rails/application.rb:195:in `env_config'
    App 17202 stderr:       from /var/www/html/rails/resume/vendor/bundle/ruby/2.1.0/gems/railties-4.1.4/lib/rails/engine.rb:510:in `call'
    App 17202 stderr:       from /var/www/html/rails/resume/vendor/bundle/ruby/2.1.0/gems/railties-4.1.4/lib/rails/application.rb:144:in `call'
    App 17202 stderr:       from /usr/local/rvm/gems/ruby-2.1.2/gems/passenger-4.0.48/lib/phusion_passenger/rack/thread_handler_extension.rb:74:in `process_request'
    App 17202 stderr:       from /usr/local/rvm/gems/ruby-2.1.2/gems/passenger-4.0.48/lib/phusion_passenger/request_handler/thread_handler.rb:141:in `accept_and_process_next_request'
    App 17202 stderr:       from /usr/local/rvm/gems/ruby-2.1.2/gems/passenger-4.0.48/lib/phusion_passenger/request_handler/thread_handler.rb:109:in `main_loop'
    App 17202 stderr:       from /usr/local/rvm/gems/ruby-2.1.2/gems/passenger-4.0.48/lib/phusion_passenger/request_handler.rb:448:in `block (3 levels) in start_threads'
    [Tue Aug 05 04:18:21.235496 2014] [core:error] [pid 17186] [client 220.255.2.220:40332] End of script output before headers:
    

    I found that I can add it in config/environments/production.rb with config.secret_key_base = xxx instead and it works. Not sure why it works though.

Make sure you are in development /testing.

Put this in virtual host configuration:

RailsEnv development

Worry about Secrets once you go production mode

Rails default /config/secrets.yml looks like this:

development:
      secret_key_base: # random string
test:
      secret_key_base: # random string
production:
      secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

It is OK to keep test and development secret key base in your (possibly public) codebase, however, the secret key on production should be set up within server environment.
We are running Ubuntu 14.04, here is how we set up our SECRETKEYBASE environmental variable:
ssh into your production server and run sudo nano /etc/environment. This will open the environment file in nano text editor. You need to add two lines at the bottom of this file:

export SECRET_KEY_BASE=<rake secret>
ruby -e 'p ENV["SECRET_KEY_BASE"]'

Where <rake secret> is the output you get when running rake secret in console (it will be a string of random characters just like # random string for development and test secretkeybase).
Source /etc/environment or restart the server and test by running echo $SECRET_KEY_BASE.

  • I have done it exactly as you but I still getting the same error:

    App 1227 stderr: [ 2015-10-06 04:10:57.3814 1352/0x9020d38(Worker 1) utils.rb:86 ]: *** Exception RuntimeError in Rack application object (Missing `secret_to$
    App 1227 stderr:        from /home/deploy/apps/matching_people/shared/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/application.rb:534:in `validate_secret_$
    App 1227 stderr:        from /home/deploy/apps/matching_people/shared/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/application.rb:246:in `env_config'
    App 1227 stderr:        from /home/deploy/apps/matching_people/shared/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/engine.rb:514:in `call'
    App 1227 stderr:        from /home/deploy/apps/matching_people/shared/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/application.rb:165:in `call'
    App 1227 stderr:        from /usr/lib/ruby/vendor_ruby/phusion_passenger/rack/thread_handler_extension.rb:94:in `process_request'
    App 1227 stderr:        from /usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler/thread_handler.rb:151:in `accept_and_process_next_request'
    App 1227 stderr:        from /usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler/thread_handler.rb:112:in `main_loop'
    App 1227 stderr:        from /usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler.rb:415:in `block (3 levels) in start_threads'
    App 1227 stderr:        from /usr/lib/ruby/vendor_ruby/phusion_passenger/utils.rb:112:in `block in create_thread_and_abort_on_exception'
    [ 2015-10-06 04:10:57.3819 988/b5efeb40 age/Cor/Req/Utils.cpp:95 ]: [Client 1-1] Sending 502 response: application did not send a complete response
    

    This is my sudo nano /etc/environment file:

    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
    
    export SECRET_KEY_BASE=2da0d3f0bfd7b9b05110bfad512f42df2d2bb2ef715c4e831caba96a9c0b2141fbfa718dff2f5daf916cd70a70afd1f24df49884c561fbbaf364b36652b2c7d1
    ruby -e 'p ENV["SECRET_KEY_BASE"]'
    
    export MATCH_PEOPLE_DATABASE_PASSWORD=2015deployer
    ruby -e 'p ENV["MATCH_PEOPLE_DATABASE_PASSWORD"]'
    

    And when I run echo $SECRET_KEY_BASE or echo $MATCH_PEOPLE_DATABASE_PASSWORD I got the exact data.

    But I still getting the same error in production, I'm using RVM, Capistrano, Passenger and NGINX on Ubuntu 14.04.

    database.yml :

    production:
      adapter: mysql2
      encoding: utf8
      pool: 5
      host: localhost
      database: matchpeople_production
      username: deployer
      password: <%= ENV['MATCH_PEOPLE_DATABASE_PASSWORD'] %>
    

    secrets.yml :

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

    Any kind of help?

    • Are you sure Passenger is attempting to run in production mode? Can you do a bundle exec rails c in the project folder on the server and see if Rails.env outputs "production"?
      If not, you may try adding these lines to /etc/environment
      export RAILSENV=production
      export RACK
      ENV=production

      • Alright, so all I need to do is to add those VARIABLES to the /etc/environment in order to set the Rails environment to PRODUCTION?

Have another answer? Share your knowledge.