500 internal server error after deploying rails app Rail 4.1 - ruby-on-rails

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

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 ;)

Bundler won't find puma when deploying Rails 5 app

Just FYI, this is the first time I'm deploying something on a production server. I went through this guide as my server is also hosted on DO.
The environment is pretty much the same - I'm using Ubuntu 14.04, Ruby 2.3.3 (rbenv) and rails 5.
I followed everything step by step and I have gotten no error, however, now I'm getting a 502 bad gateway when accessing my public IP.
I have noticed that for some reason after starting puma manager (sudo start puma-manager), the directory "shared/sockets/puma.sock" is not getting created.
When I start puma manually by using -> RACK_ENV=production bundle exec puma -C config/puma.rb from my app directory, I get the following error:
bundler: failed to load command: puma (/root/.rbenv/versions/2.3.3/bin/puma)
Errno::ENOENT: No such file or directory - connect(2) for /root/belooga/shared/sockets/puma.sock
Any idea of what I might be doing wrong? Should you need to see any file, please let me know and I'll provide anything that might be needed.
Cheers!
EDIT:
I have double-checked all the paths in:
myapp/config/puma.rb
/etc/puma.conf
/etc/nginx/sites-available/default
... the directory "shared/sockets/puma.sock" is not getting created.
Here is your problem, create the directory before you start the puma server.

Capistrano 3 Start Server Error (Rails 4.2)

This is my spec for deployment to my staging:
Rails 4.2.5
Capistrano 3.4.1
Thin 1.7.0
Nginx 1.4.6
Everything works fine, capistrano also able to deploy and run the server
BUT
When I try to access the staging, it's always internal server error and written on log/thin.log on the rails app:
Unexpected error while processing request: Missing 'secret_token' and
'secret_key_base' for 'production' environment, set these values in
'config/secrets.yml'
I also have set the secret_key_base for production environment generated from rake secret RAILS_ENV=production
if I kill the running server process run by capistrano and manually run the server using bundle exec thin -p [MY_PORT] -e production -d start, the error disappears and everything is normal
So,
it passes nginx so the error must be in thin or capistrano
it bugs me everytime I deploy to production then I must kill the
server process and start it manually
my questions are:
why thin server started by capistrano always have error with missing
secret_key_base and secret_token although I already have it on
my secrets.yml?
how to fix it? I'm out of options

Rails + Faye + Apache: Moving Faye from Thin to Passenger

Deploying with our main app on Passenger and Faye on Thin is currently working. But I'm having some issues transitioning from that set-up to Faye using passenger as well.
It suggests here (https://github.com/faye/faye-websocket-ruby) that I can run Faye on Passenger Standalone and start said server with this command
passenger start -p 9292
However, this doesn't even work locally. First it returns this error stating it cannot find faye.js at
http://localhost:9292/faye?message=%5B%7B%22channel%22%3A%22%2Fmeta%2Fhandshake%22%2C%22version%22%3A%221.0%22%2C%22supportedConnectionTypes%22%3A%5B%22callback-polling%22%5D%2C%22id%22%3A%221%22%7D%5D&jsonp=__jsonp1__
The error is this
Failed to load resource: the server responded with a status of 404 (Not Found)
When you change the browser to the indicated location it says
no route matches [GET] "/faye"
Looking at the passenger logs it seems to first run into this error
Started GET "/faye" for 127.0.0.1 at 2014-05-09 10:04:23 -0700
ActionController::RoutingError (No route matches [GET] "/faye"):
and then run into this one
Started OPTIONS "/faye" for 127.0.0.1 at 2014-05-09 10:04:58 -0700
ActionController::RoutingError (No route matches [OPTIONS] "/faye"):
While running using Thin I used the following command to start the server
bundle exec rackup faye.ru -s thin --daemonize -E production
There are a few differences between the passenger start I'm trying and the thin, but the largest one is that faye.ru never gets run. My faye.ru is basic
require 'faye'
faye_server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 45)
Faye::WebSocket.load_adapter('thin')
run faye_server
To switch that over to Passenger I know I need to remove the third line...but then what? When I just added this straight into my config.ru file the faye server ended up taking over the entire app, which definitely was not my aim. I thought perhaps putting it in an initializer would work, but I run into the following error:
undefined method `run' for main:Object (NoMethodError)
Which I assume is because this isn't a .ru file but a .rb file.
Regardless, I find myself rather confused, and I'd definitely appreciate any tips/directions to go.
Edit:
This site (http://rubydoc.org/github/jamesotron/faye-rails/frames) states
If you want to run faye-rails on passenger, make sure you are using passenger 4.0 standalone or passenger 4.0 on nginx 1.4+ for nginx with websocket support. Passenger on apache is not supported. Because passenger uses a multi-process model, you must use the faye redis backend. Add gem 'faye-redis' to your Gemfile and configure your routes like this:
config.middleware.use FayeRails::Middleware, mount: '/faye', :timeout => 25, server: 'passenger', engine: {type: Faye::Redis, host: 'localhost'}
However I've tried adding the relevant code to my application.rb, where I have several config commands going on within
module App
class Application < Rails::Application
but simply adding below that (where all my other config.whatever are) the code suggested above leads to this error
uninitialized constant App::Application::FayeRails (NameError)
Edit:
Added the faye-rails gem because I'm an idiot (see comments). This also required adding
config.middleware.delete Rack::Lock
because (console output)
faye-rails can't work when Rack::Lock is enabled, as it will cause
a deadlock on every request.
However, now I'm running into this error
/Users/WEF6/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-4.0.1/lib/active_support/dependencies.rb:229:in `require': cannot load such file -- passenger (LoadError)
This must be related to some of the changes I made as outlined in this post as I outlined above how I was receiving errors after a successful passenger start-up locally. Running passenger now the server throws the following error
Could not spawn process for group location#default: An error occured while starting up the preloader.
in 'void Passenger::ApplicationPool2::SmartSpawner::handleErrorResponse(Passenger::ApplicationPool2::SmartSpawner::StartupDetails &)' (SmartSpawner.h:455)
in 'string Passenger::ApplicationPool2::SmartSpawner::negotiatePreloaderStartup(Passenger::ApplicationPool2::SmartSpawner::StartupDetails &)' (SmartSpawner.h:566)
in 'void Passenger::ApplicationPool2::SmartSpawner::startPreloader()' (SmartSpawner.h:206)
in 'virtual ProcessPtr Passenger::ApplicationPool2::SmartSpawner::spawn(const Passenger::ApplicationPool2::Options &)' (SmartSpawner.h:752)
in 'void Passenger::ApplicationPool2::Group::spawnThreadRealMain(const SpawnerPtr &, const Passenger::ApplicationPool2::Options &, unsigned int)' (Implementation.cpp:804)
[ 2014-05-09 12:15:15.1055 71107/0x10c9ce000 agents/HelperAgent/RequestHandler.h:2222 ]: [Client 21] Cannot checkout session.
Error page:
cannot load such file -- passenger (LoadError)
You must declare the faye-rails gem in your gemfile including this line:
'faye-rails', '~> 2.0.0'
Update:
I think you can solve the rack lock issue adding the line below in your application.rb
config.middleware.delete Rack::Lock
I hope this helps you.
I noticed you're using faye.ru when running Thin. Phusion Passenger only supports config.ru as the filename for the Rack startup file. Maybe the solution is as easy as renaming faye.ru to config.ru?

SemiFixed: Missing `secret_key_base` for 'production' environment

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.

Resources