I have recently deployed an app and got internal server error because of missing production secret_key_base. After hours of testing, I managed to solve this problem with two methods:
Method 1:
I generated a new secret_key with rake secret
and replaced it with <%= ENV["SECRET_KEY_BASE"] %>
in secrets.yml
. Deployed the app again and this time it worked. But I think that this method is wrong.
Method 2:
I generated a new secret_key with rake secret
and added it to environments/production.rb
like config.secret_key_base = 'd1f4810e662acf46a33960e3aa5bd0************************
, without changing secrets.yml
(default is production: <%= ENV["SECRET_KEY_BASE"] %>
). Deployed the app again and it works fine.
My questions:
The application's key_generator , and thus secret_key_base , are used by three core features within the Rails framework: Deriving keys for encrypted cookies which are accessible via cookies. encrypted . Deriving the key for HMAC signed cookies which are accessible via cookies.
The master key When you create a new rails app a file called credentials. yml. enc is added to the config directory. This file will be decrypted in a production environment using a key stored either on a RAILS_MASTER_KEY environment variable or a master.
Your text editor will open an unencrypted version of your credentials. If you don't have EDITOR set, you can run EDITOR=vi bin/rails credentials:edit or use your favorite text editor. After saving the file, the encrypted version will be saved to config/credentials. yml.
2.2 config/secrets. yml file in the config folder. By default, this file contains the application's secret_key_base , but it could also be used to store other secrets such as access keys for external APIs.
I have finally found the corrent method. None of the methods mentioned in question are the correct one.
Correct method:
We ourselves should generate a secret key (by rake secret
) then create an environment variables for SECRET_KEY_BASE by running following command from command prompt:
rhc set-env SECRET_KEY_BASE=3dc8b0885b3043c0e38aa2e1dc64******************** -a myapp
after running this command, connect to your server via SSH and run env
so you should see your SECRET_KEY_BASE in the list.
Now restart you app rhc app-stop myapp
and rhc app-start myapp
, then you are good to go.
If you're on a normal Ubuntu machine just put export SECRET_KEY_BASE=" <<< output from rake secret here >>> "
in your ~/.bashrc
.
Run source ~/.bashrc
and restart the app.
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