Getting "Missing `secret_key_base` for 'production' environment" when trying to get Unicorn to work with Rails

April 10, 2017 137 views
Ruby on Rails Ubuntu

Hi,

I'm tryihng to follow your tutorial for installing unicorn and getting it to work with nginx ...

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-unicorn-and-nginx-on-ubuntu-14-04

I have a file in ~/myproject/config/secrets.yml which contains

[code]
development:
secretkeybase: 547268978521e278fd572db969ce2d25cea9da7c6db4f7164942d02322177128704a4c76ed1f536dd7ac791b10ef0355b1928fb128077657bdef156472ad81

test:
secretkeybase: c1aae665f8ed25da55a457870526dcac878d5a3734ab2fd29b4ec32b4b31bbe2f16d3b72bf0ca19286c1c88e4862d35f45c2c10140d990388e5e2184459cd23e

Do not keep production secrets in the repository,

instead read values from the environment.

production:
secretkeybase: <%= ENV["SECRETKEYBASE"] %>

[/code]

Yet when I restart nginx and unicorn and attempt to visit my web page, I get this error in my unicorn logs ...

[code]
E, [2017-04-09T22:37:34.736220 #11291] ERROR -- : app error: Missing secret_key_base for 'production' environment, set this value in config/secrets.yml (RuntimeError)
E, [2017-04-09T22:37:34.736414 #11291] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/railties-5.0.2/lib/rails/application.rb:513:in validate_secret_key_config!'
E, [2017-04-09T22:37:34.736462 #11291] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/railties-5.0.2/lib/rails/application.rb:246:in
envconfig'
E, [2017-04-09T22:37:34.736491 #11291] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/railties-5.0.2/lib/rails/engine.rb:693:in `build
request'
E, [2017-04-09T22:37:34.736515 #11291] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/railties-5.0.2/lib/rails/application.rb:521:in build_request'
E, [2017-04-09T22:37:34.736541 #11291] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/railties-5.0.2/lib/rails/engine.rb:521:in
call'
E, [2017-04-09T22:37:34.736646 #11291] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/unicorn-5.2.0/lib/unicorn/httpserver.rb:562:in `processclient'
E, [2017-04-09T22:37:34.736680 #11291] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/unicorn-5.2.0/lib/unicorn/httpserver.rb:658:in `workerloop'
E, [2017-04-09T22:37:34.736709 #11291] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/unicorn-5.2.0/lib/unicorn/httpserver.rb:508:in `spawnmissingworkers'
E, [2017-04-09T22:37:34.736738 #11291] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/unicorn-5.2.0/lib/unicorn/http
server.rb:132:in start'
E, [2017-04-09T22:37:34.736767 #11291] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/unicorn-5.2.0/bin/unicorn:126:in
<top (required)>'
E, [2017-04-09T22:37:34.736793 #11291] ERROR -- : /home/rails/.gem/ruby/2.4.0/bin/unicorn:22:in load'
E, [2017-04-09T22:37:34.736818 #11291] ERROR -- : /home/rails/.gem/ruby/2.4.0/bin/unicorn:22:in
<top (required)>'
E, [2017-04-09T22:37:34.736842 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/cli/exec.rb:74:in load'
E, [2017-04-09T22:37:34.736867 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/cli/exec.rb:74:in
kernelload'
E, [2017-04-09T22:37:34.736894 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/cli/exec.rb:27:in run'
E, [2017-04-09T22:37:34.736939 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/cli.rb:332:in
exec'
E, [2017-04-09T22:37:34.736968 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor/command.rb:27:in run'
E, [2017-04-09T22:37:34.736996 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in
invoke
command'
E, [2017-04-09T22:37:34.737021 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor.rb:359:in dispatch'
E, [2017-04-09T22:37:34.737050 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/cli.rb:20:in
dispatch'
E, [2017-04-09T22:37:34.737078 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor/base.rb:440:in start'
E, [2017-04-09T22:37:34.737106 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/cli.rb:11:in
start'
E, [2017-04-09T22:37:34.737136 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/exe/bundle:34:in block in <top (required)>'
E, [2017-04-09T22:37:34.737170 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/friendly_errors.rb:100:in
withfriendlyerrors'
E, [2017-04-09T22:37:34.737200 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/exe/bundle:26:in <top (required)>'
E, [2017-04-09T22:37:34.737230 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/bin/bundle:22:in
load'
E, [2017-04-09T22:37:34.737253 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/bin/bundle:22:in <main>'
E, [2017-04-09T22:37:34.737266 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/bin/ruby_executable_hooks:15:in
eval'
E, [2017-04-09T22:37:34.737280 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/bin/rubyexecutablehooks:15:in `<main>'
[/code]

What am I missing? What else do I need to do here?

1 Answer

Hi @laredotornado6b

Did you following the Set Environment Variables?
https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-unicorn-and-nginx-on-ubuntu-14-04#set-environment-variables

And please use the code-button in the toolbar to enclose code in three ticks - that makes it much easier to read.

When you are ready to deploy your Ruby on Rails application, there are many valid setups to consider. This tutorial will help you deploy the production environment of your Ruby on Rails application, with PostgreSQL as the database, using Unicorn and Nginx on Ubuntu...
  • Hi, Yes, following that tutorial I created the file

    rails@myproject:~$ cat /home/rails/myproject/.rbenv-vars
    SECRET_KEY_BASE=secretkeybase
    APP_DATABASE_PASSWORD=apppassword
    

    Is there another step involved? I thought it should be prtty straightforward.

    • And when you run rbenv vars in /home/rails/myproject it shows what you've just set?

      I'm not sure how to use Rails, but have you configured Gunicorn to run with the user rails?

      What does the log from Gunicorn tell you?

      • Hi,

        Yes I see variables when I run

        rails@myproject:~/myproject$ rbenv vars
        # /home/rails/myproject/.rbenv-vars
        export SECRET_KEY_BASE='secretkeybase'
        export APP_DATABASE_PASSWORD='mypassword'
        
        

        When you say "Gunicorn", is taht the same as unicorn? If so, the error I get is as before, but I'll format it this time (from /home/rails/myproject/shared/log/unicorn.stderr.log) ...

        E, [2017-04-10T10:34:20.473529 #1249] ERROR -- : app error: Missing `secret_key_base` for 'production' environment, set this value in `config/secrets.yml` (RuntimeError)
        E, [2017-04-10T10:34:20.473650 #1249] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/railties-5.0.2/lib/rails/application.rb:513:in `validate_secret_key_config!'
        E, [2017-04-10T10:34:20.473676 #1249] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/railties-5.0.2/lib/rails/application.rb:246:in `env_config'
        E, [2017-04-10T10:34:20.473693 #1249] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/railties-5.0.2/lib/rails/engine.rb:693:in `build_request'
        E, [2017-04-10T10:34:20.473709 #1249] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/railties-5.0.2/lib/rails/application.rb:521:in `build_request'
        E, [2017-04-10T10:34:20.473724 #1249] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/railties-5.0.2/lib/rails/engine.rb:521:in `call'
        E, [2017-04-10T10:34:20.473739 #1249] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/unicorn-5.2.0/lib/unicorn/http_server.rb:562:in `process_client'
        E, [2017-04-10T10:34:20.473753 #1249] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/unicorn-5.2.0/lib/unicorn/http_server.rb:658:in `worker_loop'
        E, [2017-04-10T10:34:20.473767 #1249] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/unicorn-5.2.0/lib/unicorn/http_server.rb:508:in `spawn_missing_workers'
        E, [2017-04-10T10:34:20.473781 #1249] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/unicorn-5.2.0/lib/unicorn/http_server.rb:132:in `start'
        E, [2017-04-10T10:34:20.473795 #1249] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/unicorn-5.2.0/bin/unicorn:126:in `<top (required)>'
        E, [2017-04-10T10:34:20.473809 #1249] ERROR -- : /home/rails/.gem/ruby/2.4.0/bin/unicorn:22:in `load'
        E, [2017-04-10T10:34:20.473823 #1249] ERROR -- : /home/rails/.gem/ruby/2.4.0/bin/unicorn:22:in `<top (required)>'
        E, [2017-04-10T10:34:20.473837 #1249] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/cli/exec.rb:74:in `load'
        E, [2017-04-10T10:34:20.473850 #1249] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/cli/exec.rb:74:in `kernel_load'
        E, [2017-04-10T10:34:20.473865 #1249] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/cli/exec.rb:27:in `run'
        E, [2017-04-10T10:34:20.473879 #1249] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/cli.rb:332:in `exec'
        E, [2017-04-10T10:34:20.473893 #1249] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
        E, [2017-04-10T10:34:20.473907 #1249] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
        E, [2017-04-10T10:34:20.473933 #1249] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor.rb:359:in `dispatch'
        E, [2017-04-10T10:34:20.473950 #1249] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/cli.rb:20:in `dispatch'
        E, [2017-04-10T10:34:20.473970 #1249] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor/base.rb:440:in `start'
        E, [2017-04-10T10:34:20.473985 #1249] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/cli.rb:11:in `start'
        E, [2017-04-10T10:34:20.474000 #1249] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/exe/bundle:34:in `block in <top (required)>'
        E, [2017-04-10T10:34:20.474014 #1249] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/friendly_errors.rb:100:in `with_friendly_errors'
        E, [2017-04-10T10:34:20.474028 #1249] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/exe/bundle:26:in `<top (required)>'
        E, [2017-04-10T10:34:20.474042 #1249] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/bin/bundle:22:in `load'
        E, [2017-04-10T10:34:20.474055 #1249] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/bin/bundle:22:in `<main>'
        E, [2017-04-10T10:34:20.474068 #1249] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/bin/ruby_executable_hooks:15:in `eval'
        E, [2017-04-10T10:34:20.474087 #1249] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/bin/ruby_executable_hooks:15:in `<main>'
        
        

        Looks like unicorn is running under tails based on "ps" ...

        rails@myproject:~/myproject$ ps -elf | grep unicorn
        1 S rails     1238     1  0  80   0 - 66087 poll_s 10:24 ?        00:00:02 unicorn master (old) -c config/unicorn.rb -E production -D
        1 S rails     1246  1238  0  80   0 - 66087 poll_s 10:24 ?        00:00:00 unicorn worker[0] -c config/unicorn.rb -E production -D
        1 S rails     1249  1238  0  80   0 - 66087 poll_s 10:24 ?        00:00:00 unicorn worker[1] -c config/unicorn.rb -E production -D
        0 S rails     1991  1238  0  80   0 - 65471 poll_s 10:28 ?        00:00:03 unicorn master -c config/unicorn.rb -E production -D
        1 S rails     1997  1991  0  80   0 - 67898 poll_s 10:28 ?        00:00:00 unicorn worker[0] -c config/unicorn.rb -E production -D
        1 S rails     2000  1991  0  80   0 - 70655 poll_s 10:28 ?        00:00:00 unicorn worker[1] -c config/unicorn.rb -E production -D
        0 S rails     2406  1755  0  80   0 -  1808 inotif 10:38 pts/1    00:00:00 tail -f /home/rails/myproject/shared/log/unicorn.stderr.log
        0 S rails     2421  2043  0  80   0 -  2935 pipe_w 10:39 pts/0    00:00:00 grep unicorn
        
        

        Is there anywhere in the unicorn config where I can put the SECRETKEYBASE definition?

        • In the comment section of the tutorial you've follow, someone has the same problem. Here the author recommends deleting everything and starting over.

          I simply don't know. You're probably better off by asking in a Rails forum.
          http://stackoverflow.com/questions/tagged/ruby-on-rails

          • Delete everything and start over? Are you kidding me -- I'm not doing taht.

            Anyway, what about my question -- "Is there anywhere in the unicorn config where I can put the SECRETKEYBASE definition?"

Have another answer? Share your knowledge.