SemiFixed: Missing `secret_key_base` for 'production' environment - ruby-on-rails

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?

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.

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.

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.

Related

no password provided rails 6 with RAILS_ENV=production

i try to put my rails api into digital oceans server with nginx and passenger, so far i got it working but when i try to send a request with postman to my server i have a 500 internal error after checking log/production.log, the 500 internal error is due to ActiveRecord::ConnectionNotEstablished (fe_sendauth: no password supplied
This is weird because RAILS_ENV=production rails db:setup is working,i have my master.key for credentials and secret_key_base is in it, environnement variable in my bashrc for username database and password database. when i do RAILS_ENV=production rails c i can see my ENV["MYAPP_DATABASE_USER"]
still in my rails console in production mode i can do Rails.application.credentials.secret_key_base she is there !
postgres user is set correctly with the same password i supply to rails.. maybe its passenger who is not working properly... but i don't know how to properly check it.
EDIT : PROBLEM SOLVE
i was able to solve my problem passing my env variable to nginx like this
in /etc/nginx/sites-enabled/myapp.conf
passenger_env_var KEY VALUE;
passenger doc's say that he is reading the bashrc file for env variable ... but its ok like that hope its help some dude over here hehe ;)

Rails 5.2 with master.key Digital Ocean deployment: ActiveSupport::MessageEncryptor::InvalidMessage: ActiveSupport::MessageEncryptor::InvalidMessage

I have migrated my Ruby on Rails application from Rails 5.1.2 to Rails 5.2.0 to use the encrypted secrets. Application is successfully deployed to Digital Ocean Ubuntu Server. But when I go in browser to access, it shows the following log.
ActiveSupport::MessageEncryptor::InvalidMessage: ActiveSupport::MessageEncryptor::InvalidMessage
/home/deploy/apps/GeekyCricket/shared/bundle/ruby/2.4.0/gems/activesupport-5.2.0/lib/active_support/message_encryptor.rb:206:in `rescue in _decrypt'
/home/deploy/apps/GeekyCricket/shared/bundle/ruby/2.4.0/gems/activesupport-5.2.0/lib/active_support/message_encryptor.rb:184:in `_decrypt'
/home/deploy/apps/GeekyCricket/shared/bundle/ruby/2.4.0/gems/activesupport-5.2.0/lib/active_support/message_encryptor.rb:157:in `decrypt_and_verify'
/home/deploy/apps/GeekyCricket/shared/bundle/ruby/2.4.0/gems/activesupport-5.2.0/lib/active_support/messages/rotator.rb:21:in `decrypt_and_verify'
/home/deploy/apps/GeekyCricket/shared/bundle/ruby/2.4.0/gems/activesupport-5.2.0/lib/active_support/encrypted_file.rb:79:in `decrypt'
/home/deploy/apps/GeekyCricket/shared/bundle/ruby/2.4.0/gems/activesupport-5.2.0/lib/active_support/encrypted_file.rb:42:in `read'
/home/deploy/apps/GeekyCricket/shared/bundle/ruby/2.4.0/gems/activesupport-5.2.0/lib/active_support/encrypted_configuration.rb:21:in `read'
/home/deploy/apps/GeekyCricket/shared/bundle/ruby/2.4.0/gems/activesupport-5.2.0/lib/active_support/encrypted_configuration.rb:33:in `config'
/home/deploy/apps/GeekyCricket/shared/bundle/ruby/2.4.0/gems/activesupport-5.2.0/lib/active_support/encrypted_configuration.rb:38:in `options'
/home/deploy/apps/GeekyCricket/shared/bundle/ruby/2.4.0/gems/activesupport-5.2.0/lib/active_support/core_ext/module/delegation.rb:271:in `method_missing'
(erb):12:in `<main>'
I have added encrypted secrets using rails credentials:edit, which creates config/credentials.yml.enc and master.key.
I also have added the master.key file on /app_name/shared/config/ on my ubuntu server, also placed an env variable RAILS_MASTER_KEY. But still getting this error, I don't know what I am missing here.
Solution A and B are different solutions. Just choose which is good for you.
a. Deploy from Continous Integration
1. Edit deploy.rb
set :default_env, {
"RAILS_ENV" => "production",
"RAILS_MASTER_KEY" => ENV["RAILS_MASTER_KEY"]
}
2. Add RAILS_MASTER_KEY as a variable
Travis CI
GitLab CI
b. Usage of master.key
1. Edit deploy.rb
append :linked_files, "config/master.key"
2. Upload master.key with :linked_files
Let's assume our application's root path is /home/deploy/awesome-project. All we need to do is upload the key file to /home/deploy/awesome-project/shared/config/master.key.
I faced the same problem, when deploying for the first time on my DigitalOcean Droplet, every time I ran RAILS_ENV=production cap production deploy:initial it failed complaining this error - ActiveSupport::MessageEncryptor::InvalidMessage
I tried below options which all failed -
removing master.key and credential.yml.enc file and then deploying again.
adding both files again and deploying.
Finally one solution worked, i just added master.key, removing credentials.yml.enc file, committed it and redeployed, and it worked without changing my deploy.rb file

Postgresql Schema not found after moving to Amazon AWS

I have a multi-tenant rails 3.2 app that uses postgresql 9.3.5 and apartment gem. Everything works fine in my local and even when I ran "rails s -e production" it still works. Also when I use "rails c production" in AWS, its working. I can intercat with the db via rails console. I'm using VPC. My rails app is in ec2 and the db is in RDS.
When I deploy to AWS I get this error in the browser.
Apartment::SchemaNotFound
One of the following schema(s) is invalid: ec2-54-169-xxx-xxx, "public"
Passenger Logs:
App 9023 stderr: Apartment::SchemaNotFound (One of the following schema(s) is invalid: ec2-54-169-xxx-xxx, "public"):
App 9023 stderr: apartment (0.25.2) lib/apartment/adapters/postgresql_adapter.rb:92:in `rescue in connect_to_new'
App 9023 stderr: apartment (0.25.2) lib/apartment/adapters/postgresql_adapter.rb:85:in `connect_to_new'
App 9023 stderr: apartment (0.25.2) lib/apartment/adapters/abstract_adapter.rb:100:in `switch'
App 9023 stderr: apartment (0.25.2) lib/apartment/elevators/generic.rb:20:in `call'
ec2-54-169-xxx-xxx contains the ip of my ec2 I just replaced some with x.
Finally, I was able to fix it. I was using the Public DNS given by AWS to test. Looks like that was the error because my app was listening to the subdomain. When I tried using the ip of the EC2 it worked. So I worked on the Route53 right away to access it via domain name on the browser, then it already worked.

500 internal server error after deploying rails app Rail 4.1

So this issue is pretty straight forward:
** Exception RuntimeError in Rack application object (Missing `secret_key_base` for 'production' environment, set this value in `config/secrets.yml`) (process 31929, thread 0x95116e4(Worker 1))
So I did that, I went and added the secrets.yml file to the appname/current/config/ directory and added:
production:
secret_key_base: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
And then I restarted the server.
Still I get this 500 error and still the error.log sais that I am missing this file. Am I missing some basic step here?
Note: This is running with passenger and apache2

Weird problem with Rails app and Mongrel

I originally posted a question relating to this problem on serverfault.com: https://serverfault.com/questions/152587/apache-mod-proxy-to-another-server
I have since realized that this is not an issue with my server setup, but my rails application. I have set this application up server-wise the exact same as another functioning rails app I have running on the same server. When I start the server my mongrel.log looks like this:
** Daemonized, any open files are closed. Look at /var/www/osuwebdev/tmp/pids/mongrel.pid and log/mongrel.log for info.
** Starting Mongrel listening at 0.0.0.0:8080
** Starting Rails with production environment...
** Rails loaded.
** Loading any Rails specific GemPlugins
** Signals ready. TERM => stop. USR2 => restart. INT => stop (no restart).
** Rails signals registered. HUP => reload (without restart). It might not work well.
** Mongrel 1.1.5 available at 0.0.0.0:8080
** Writing PID file to /var/www/osuwebdev/tmp/pids/mongrel.pid
When I load a page this pops up in the log:
Wed Jun 30 19:46:10 +0000 2010: Error calling Dispatcher.dispatch #<NoMethodError: undefined method `[]' for nil:NilClass>
/var/lib/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/cgi.rb:108:in `send_cookies'
/var/lib/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/cgi.rb:136:in `out'
/var/lib/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/http_response.rb:65:in `start'
/var/lib/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/cgi.rb:135:in `out'
/var/lib/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/rails.rb:81:in `process'
/var/lib/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb:159:in `process_client'
/var/lib/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb:158:in `each'
/var/lib/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb:158:in `process_client'
/var/lib/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb:285:in `run'
/var/lib/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb:285:in `initialize'
/var/lib/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb:285:in `new'
/var/lib/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb:285:in `run'
/var/lib/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb:268:in `initialize'
/var/lib/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb:268:in `new'
/var/lib/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb:268:in `run'
/var/lib/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/configurator.rb:282:in `run'
/var/lib/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/configurator.rb:281:in `each'
/var/lib/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/configurator.rb:281:in `run'
/var/lib/gems/1.8/gems/mongrel-1.1.5/bin/mongrel_rails:128:in `run'
/var/lib/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/command.rb:212:in `run'
/var/lib/gems/1.8/gems/mongrel-1.1.5/bin/mongrel_rails:281
/usr/local/bin/mongrel_rails:19:in `load'
/usr/local/bin/mongrel_rails:19
I cannot figure out what is different about this app that may cause this issue.
I believe this is a weird combination of bugs in Rack, Mongrel, and Rails.
Save this ruby code in your app to config/mongrel.rb: http://gist.github.com/471663
In your config/environment.rb, at the very end of the file add:
require File.join(File.dirname(File.expand_path(FILE)), 'mongrel')
In that same file add this line in the Rails::initializer.run block:
config.gem "mongrel"
Longer explanation of the problem here: https://rails.lighthouseapp.com/projects/8994/tickets/4690-mongrel-doesnt-work-with-rails-238
This has definitely worked for a Rails 2.3.8 app, though I patched it slightly differently than described above.
Do you still see the problem on subsequent GET requests? I had a similar problem where my Rails app would run fine under WEBrick but not under Mongrel but found somewhere on the internets (here's a link that shows a similar version of the problem) that older versions of Mongrel mishandle the first request to the page but handle subsequent requests ok.
The workaround we put in was that whenever we started the app, we'd have curl perform a GET on the app to 'prime' Mongrel.
This is usually a fail reading a yaml file for config. It has set up a config object to get values from and the entry in the yaml file for your environment isn't there so the object is nil.
Are you running any gems that have a yaml file that maybe isn't set up for development?
I don't know why going back to 2.3.5 would help - it might be that it loads things in a different order and the config information is available.

Resources