Unable to run rails application on puma using ssl - ruby-on-rails

I'm trying to run my rails app on puma in development environment on windows box. Without ssl the application works fine..
But when I try to configure ssl for puma, nothing happens. No error is generated in the log and the web page (https://localhost:3000/) says Secure Connection Failed. Below is the output from command line that shows the server started but nothing happened after that..
E:\ap01\dev\rmmi>foreman start -p 3000
17:19:31 web.1 | started with pid 3016
17:19:34 web.1 | *** SIGUSR2 not implemented, signal based restart unavailable!
17:19:34 web.1 | *** SIGUSR1 not implemented, signal based restart unavailable!
17:19:34 web.1 | *** SIGHUP not implemented, signal based logs reopening unavailable!
17:19:34 web.1 | Puma starting in single mode...
17:19:34 web.1 | * Version 2.11.2 (ruby 2.0.0-p353), codename: Intrepid Squirrel
17:19:34 web.1 | * Min threads: 5, max threads: 5
17:19:34 web.1 | * Environment: development
17:19:36 web.1 | * Listening on tcp://0.0.0.0:3000
17:19:36 web.1 | * Listening on ssl://127.0.0.1:3000?cert=E:/ap01/OpenSSL-Win32/bin/server.crt&key=E:/ap01/OpenSSL-Win32/bin/server.key
17:19:36 web.1 | Use Ctrl-C to stop
My config/puma.rb file is given below -
path_to_key="E:/ap01/OpenSSL-Win32/bin/server.key"
path_to_cert="E:/ap01/OpenSSL-Win32/bin/server.crt"
threads_count = Integer(ENV['MAX_THREADS'] || 5)
threads threads_count, threads_count
preload_app!
rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'
ssl_bind '127.0.0.1', '3000', { key: path_to_key, cert: path_to_cert }
on_worker_boot do
ActiveSupport.on_load(:active_record) do
config = ActiveRecord::Base.configurations[Rails.env] ||
Rails.application.config.database_configuration[Rails.env]
config['pool'] = ENV['MAX_THREADS'] || 5
ActiveRecord::Base.establish_connection(config)
end
end
The profile for foreman is given below -
web: bundle exec puma -p $PORT -C config/puma.rb
I've also set config.force_ssl = true in my apps config/application.rb file.
Could you please advise what I'm missing here or doing incorrectly ?. Any pointers will be appreciated
Thanks in Advance!

You aren't doing anything wrong. The browser doesn't trust your local certificate with your localhost, so it will give you a warning.
But you can bypass it using the "Advanced" option on the page (left side bottom) and click proceed to view the ssl version of your application.

Related

Heroku - Rails app gives 'No such app' when everything seems right

I have a new, Rails 6 app that I'm trying to deploy to Heroku.
Judging from the deploy log and the logs I'm getting with heroku logs --tail everything seems to work, but when I go to the deployed, herokuapp.com domain, I get the 'No such app' screen.
Here is the latest logs:
2021-11-19T13:45:11.000000+00:00 app[api]: Build succeeded
2021-11-19T13:45:11.060793+00:00 heroku[web.1]: Starting process with command `bundle exec puma -C config/puma.rb`
2021-11-19T13:45:12.126085+00:00 app[web.1]: [4] Puma starting in cluster mode...
2021-11-19T13:45:12.126103+00:00 app[web.1]: [4] * Puma version: 5.5.2 (ruby 3.0.2-p107) ("Zawgyi")
2021-11-19T13:45:12.126103+00:00 app[web.1]: [4] * Min threads: 5
2021-11-19T13:45:12.126104+00:00 app[web.1]: [4] * Max threads: 5
2021-11-19T13:45:12.126104+00:00 app[web.1]: [4] * Environment: production
2021-11-19T13:45:12.126104+00:00 app[web.1]: [4] * Master PID: 4
2021-11-19T13:45:12.126104+00:00 app[web.1]: [4] * Workers: 2
2021-11-19T13:45:12.126127+00:00 app[web.1]: [4] * Restarts: (✔) hot (✖) phased
2021-11-19T13:45:12.126133+00:00 app[web.1]: [4] * Preloading application
2021-11-19T13:45:13.203269+00:00 app[web.1]: [4] * Listening on http://0.0.0.0:54150
2021-11-19T13:45:13.203334+00:00 app[web.1]: [4] Use Ctrl-C to stop
2021-11-19T13:45:13.207647+00:00 app[web.1]: [4] - Worker 0 (PID: 7) booted in 0.0s, phase: 0
2021-11-19T13:45:13.209455+00:00 app[web.1]: [4] - Worker 1 (PID: 9) booted in 0.0s, phase: 0
2021-11-19T13:45:13.336382+00:00 heroku[web.1]: State changed from starting to up
And here's the puma config:
workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['RAILS_MAX_THREADS'] || 5)
threads threads_count, threads_count
preload_app!
rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'
on_worker_boot do
# Worker specific setup for Rails 4.1+
# See: https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server#on-worker-boot
ActiveRecord::Base.establish_connection
end
Which is the one recommended by Heroku in their documentation.
The Procfile is a simple one-liner:
web: bundle exec puma -C config/puma.rb
I also tried the heroku configuration by running PORT=3000 heroku local on my machine and it works wonderfully, but not on Heroku's platform :(
I'm using Rails 6.1.4.1 and ruby 3.0.2
Can you point me at where I could try to find the root of the problem?
Thank you,
Balint
This seems to have been a transient issue. When I came back to the problem a few hours later, it just worked without me having to change anything.

No logging/info with Rails and Heroku local

My Rails app is on Heroku and needs access to S3. As such, I have a .env file at the root of my directory with the AWS authentication bits. Simply running rails s will not use the env file, so I need to test my server with heroku local. However, this will not spit out any of the standard rails output. I did try heroku logs and heroku logs -t.
My procfile:
web: bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}
My env file:
RACK_ENV=development
PORT=3000
AWS_ACCESS_KEY_ID=MYSECRETACCESSKEYID
AWS_SECRET_ACCESS_KEY=mySECRETaccessKEY
S3_BUCKET=my-bucket
AWS_REGION=us-east-1
After running heroku local:
[OKAY] Loaded ENV .env File as KEY=VALUE Format
1:33:45 PM web.1 | Ignoring http_parser.rb-0.6.0 because its extensions are not built. Try: gem pristine http_parser.rb --version 0.6.0
1:33:46 PM web.1 | Puma starting in single mode...
1:33:46 PM web.1 | * Version 4.3.5 (ruby 2.6.3-p62), codename: Code Name
1:33:46 PM web.1 | * Min threads: 5, max threads: 5
1:33:46 PM web.1 | * Environment: development
1:33:48 PM web.1 | * Listening on tcp://0.0.0.0:3000
1:33:48 PM web.1 | Use Ctrl-C to stop
Expected results: whenever I make a request to the endpoint, I expect it to log the endpoint, the SQL commands, and the errors, like it usually does.
I think I figured it out. Since we are not calling rails s directly, (my procfile is calling bundle exec puma), I looked on StackOverflow for similar questions involving puma and found this.
I needed to open a second terminal and run tail -f logs/development.log.

rufus + puma + heroku warning

I'm running a rails app on heroku, I have two web dynos.
How can I remove the ! WARNING: Detected 1 Thread(s) started in app boot: warning?
My puma.rb file:
workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['RAILS_MAX_THREADS'] || 5)
threads threads_count, threads_count
preload_app!
rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'
on_worker_boot do |worker_number|
# Worker specific setup for Rails 4.1+
# See: https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server#on-worker-boot
ActiveRecord::Base.establish_connection
end
The logs:
2016-09-03T14:21:24.742960+00:00 app[web.1]: [3] * Listening on tcp://0.0.0.0:53786
2016-09-03T14:21:24.743135+00:00 app[web.1]: [3] ! WARNING: Detected 1 Thread(s) started in app boot:
2016-09-03T14:21:24.743181+00:00 app[web.1]: [3] ! #<Thread:0x007f382b59fca8#/app/vendor/bundle/ruby/2.2.0/gems/rufus-scheduler-3.2.2/lib/rufus/scheduler.rb:563 sleep> - /app/vendor/bundle/ruby/2.2.0/gems/rufus-scheduler-3.2.2/lib/rufus/scheduler.rb:571:in `sleep'
2016-09-03T14:21:24.743257+00:00 app[web.1]: [3] Use Ctrl-C to stop
2016-09-03T14:21:24.754941+00:00 app[web.1]: [3] - Worker 0 (pid: 7) booted, phase: 0
2016-09-03T14:21:24.756269+00:00 app[web.1]: [3] - Worker 1 (pid: 20) booted, phase: 0
2016-09-03T14:21:25.525174+00:00 app[web.2]: ** [NewRelic][2016-09-03 14:21:25 +0000 web.2 (18)] INFO : Doing deferred dependency-detection before Rack startup
It's very easy. Stop using rufus-scheduler. It's using threads, that's what is causing your warning.
Look at:
2016-09-03T14:21:24.743181+00:00 app[web.1]: [3] !
<Thread:0x007f382b59fca8#
/app/vendor/bundle/ruby/2.2.0/gems/rufus-scheduler-3.2.2/lib/rufus/scheduler.rb:563
sleep> -
/app/vendor/bundle/ruby/2.2.0/gems/rufus-scheduler-3.2.2/lib/rufus/scheduler.rb:571
:in `sleep'
Rufus-scheduler clearly is the cause of the warning.
Cf https://github.com/jmettraux/rufus-scheduler/issues/213
Now there is another way to look at the thing: the warning is not emitted by rufus-scheduler, it's emitted by Puma. There's probably a way to disable the warning, but that's a question for people with knowledge of Puma.

Thin Foreman stopped working

I'm using Thin webserver with RoR on my iMac.
I start it with $ foreman start
It was working fine, but now I'm getting this in the console:
09:27:10 web.1 | => Booting Thin
09:27:10 web.1 | => Rails 3.1.3 application starting in development on http://0.0.0.0:5000
09:27:10 web.1 | => Call with -d to detach
09:27:10 web.1 | => Ctrl-C to shutdown server
09:27:10 web.1 | >> Thin web server (v1.3.1 codename Triple Espresso)
09:27:10 web.1 | >> Maximum connections set to 1024
09:27:10 web.1 | >> Listening on 0.0.0.0:5000, CTRL+C to stop
09:27:10 web.1 | Exiting
09:27:11 web.1 | process terminated
09:27:11 system | sending SIGTERM to all processes
And the $ prompt shows up - so the webserver isn't running.
Any ideas?
I just ran $ thin start
and got:
Using rack adapter
/Users/burtondav/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.22/lib/bundler/runtime.rb:31:in `block in setup': You have already activated rack 1.4.1, but your Gemfile requires rack 1.3.6. Using bundle exec may solve this. (Gem::LoadError)
What bundle exec command should I try?
Add the gem 'thin' in your application Gemfile and execute the bundle exec thin start to resolve the version conflict problem.

foreman only shows line with “started with pid #” and nothing else

When I run foreman I get the following:
> foreman start
16:47:56 web.1 | started with pid 27122
Only if I stop it (via ctrl-c) it shows me what is missing:
^CSIGINT received
16:49:26 system | sending SIGTERM to all processes
16:49:26 web.1 | => Booting Thin
16:49:26 web.1 | => Rails 3.0.0 application starting in development on http://0.0.0.0:5000
16:49:26 web.1 | => Call with -d to detach
16:49:26 web.1 | => Ctrl-C to shutdown server
16:49:26 web.1 | >> Thin web server (v1.3.1 codename Triple Espresso)
16:49:26 web.1 | >> Maximum connections set to 1024
16:49:26 web.1 | >> Listening on 0.0.0.0:5000, CTRL+C to stop
16:49:26 web.1 | >> Stopping ...
16:49:26 web.1 | Exiting
16:49:26 web.1 | >> Stopping ...
How do I fix it?
I’ve been able to resolve this issue by 2 different ways:
From https://github.com/ddollar/foreman/wiki/Missing-Output:
If you are not seeing any output from your program, there is a likely
chance that it is buffering stdout. Ruby buffers stdout by default. To
disable this behavior, add this code as early as possible in your
program:
# ruby
$stdout.sync = true
By installing foreman via the heroku toolbelt package
But I still don’t know what’s happening nor why this 2 ways above resolved the issue…
My solution was to put $stdout.sync = true at the top of config/environments/development.rb.
Then everything that loads the development environment (incluing thin) will not buffer stdout.
"Foreman will display to the terminal output anything written to stdout by the processes it launches." - ddollar See foreman-issues#57
BTW, you can use tailf into Procfile to see logs
web: bundle exec rails server thin -p $PORT
log: tail -f log/development.log
Tip: tailf doesn't exist in OSX, using tail -f -n 40 log/development.log works.
I also had the same problem but with a different solution. (ruby 1.9.2p290, rails 3.1.0, ubuntu 10.04.3)
I changed the line in my Procfile from:
web: bundle exec thin start -p $PORT
to:
web: bundle exec rails server thin -p $PORT
and it no longer gave me an issue.
I have the same problem (ruby 1.9.3-p0, rails 3.2rc2, OSX 10.7).
Resolved the issue by using foreman-0.27.0 by adding this line into my Gemfile.
gem 'foreman', '0.27.0'
If you are using Foreman to run a Python project, rather than a Ryby project, and you're having the same issue, here are some solutions for you. If you are using a Procfile to invoke the python CLI directly, then you can use the '-u' option to avoid stdout buffering:
python -u script.py
If you are using a Procfile to manage a WSGI server, such as invoking gunicorn, flask, bottle, eve, etc., then you can add a ".env" file to the root of your python project, containing the following:
PYTHONUNBUFFERED=True

Resources