So I am trying to get my rails app to deploy in production mode, but I get the error: Missing secret_token
and secret_key_base
for 'production' environment, set these values in config/secrets.yml
My secrets.yml file is as expected:
development: secret_key_base: xxxxxxx test: secret_key_base: xxxxxxx production: secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
But even after google and research, I have no idea what to do with the production secret key base. Most of the info out there assumes I have certain background knowledge, but the reality is that I'm a noob.
Can anyone explain to me how to set my secret key and get this to work in production mode?
Have you ever wondered what the secret_key_base value is and how it's used in a Rails application? This configuration value was introduced in Rails 4 and is usually defined on a per-environment basis. It's purpose is simple: to be the secret input for the application's key_generator method.
The key used to encrypt credentials, called the Rails master key, is automatically generated when you create a new Rails app or when you run bin/rails credentials:edit . If you like to create a new key, you can run bin/rails runner 'puts ActiveSupport::EncryptedFile.generate_key'
Secrets YAML Reference. This document is the reference for the YAML grammar used for creating Semaphore secrets. A secret is a bucket that stores environment variables and files.
Rails stores secrets in config/credentials. yml. enc, which is encrypted and cannot be edited directly.
You can generate the key by using following commands
$ irb >> require 'securerandom' => true >> SecureRandom.hex(64) => "3fe397575565365108556c3e5549f139e8078a8ec8fd2675a83de96289b30550a266ac04488d7086322efbe573738e7b3ae005b2e3d9afd718aa337fa5e329cf" >> exit
The errors you are getting just indicate that the environment variable for secret_key_base
are not properly set on the server.
You can use various scripts like capistrano that automate the process of setting these before the application is run.
As for a quick fix try this:
export SECRET_KEY_BASE=YOUR SECRET BASE
Validate the environment variables and check if these have been set.
Command:
env | grep -E "SECRET_TOKEN|SECRET_KEY_BASE"
If your values pop up then these are set on the production server.
Also it is best practice to use ENV.fetch(SECRET_KEY)
as this will raise an exception before the app even tries to start.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With