Error Deploying Rails on Dokku - on Digital Ocean - ruby-on-rails

I'm trying to run dokku on DigitalOcean with a sample rails app, but when I deploy, I get this error:
Puma starting in single mode...
* Version 3.12.1 (ruby 2.6.3-p62), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: production
* Listening on tcp://0.0.0.0:5000
bundler: failed to load command: rackup (/app/vendor/bundle/ruby/2.6.0/bin/rackup)
Errno::ENOENT: No such file or directory # rb_sysopen - tmp/pids/server.pid
/app/vendor/bundle/ruby/2.6.0/gems/puma-3.12.1/lib/puma/launcher.rb:133:in `initialize'
/app/vendor/bundle/ruby/2.6.0/gems/puma-3.12.1/lib/puma/launcher.rb:133:in `open'
/app/vendor/bundle/ruby/2.6.0/gems/puma-3.12.1/lib/puma/launcher.rb:133:in `write_pid'
/app/vendor/bundle/ruby/2.6.0/gems/puma-3.12.1/lib/puma/launcher.rb:106:in `write_state'
/app/vendor/bundle/ruby/2.6.0/gems/puma-3.12.1/lib/puma/single.rb:103:in `run'
/app/vendor/bundle/ruby/2.6.0/gems/puma-3.12.1/lib/puma/launcher.rb:186:in `run'
/app/vendor/bundle/ruby/2.6.0/gems/puma-3.12.1/lib/rack/handler/puma.rb:73:in `run'
/app/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/server.rb:297:in `start'
/app/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/server.rb:148:in `start'
/app/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/bin/rackup:4:in `<top (required)>'
/app/vendor/bundle/ruby/2.6.0/bin/rackup:23:in `load'
/app/vendor/bundle/ruby/2.6.0/bin/rackup:23:in `<top (required)>'
I've looked through other support sites for a similar error - but it looks like the web app container starts then immediately stops.
I can see my database container running, and I see a container with dokkku/myapp:latest - and it's never up for more than a few seconds.
Anyone have an idea why?

Rails 6 added a line to config/puma.rb that specifies the location of the pidfile (line 20 by default, at the time of my writing this), with the default value as follows:
pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }
I ran into the same problem you mentioned, and addressed it by changing that line to
pidfile ENV.fetch("PIDFILE") { "server.pid" }
Removing the tmp dir from both .gitignore and .dockerignore (in my case, since my app is Dockerized) also worked, but having tmp dirs in git and on my production containers smelled funny, so I opted for the aforementioned approach.
Either way, I'm glad you got it working! ... hope this helps
PS - you may want to consider selecting one of these answers as correct (even if it's an answer you provided) Happy coding!

tmp/pids/server.pid is the file Rails uses to check if it is already running, and to check what process ID it is running with.
IIRC with Dokku, there is a user called dokku that all Dokku processes run under - make sure that user (or whichever user Dokku/the Rails app is running under), has read/write permission on that directory.
Alternatively, you may be able to just call touch tmp/pids/server.pid to create the file so that Rails can use it.

Not sure why this is an issue - but my .gitignore file (a default, I think) kept the /tmp folder, but ignored contents, so there wasn't a /tmp/pids in the repo, and there probably wasn't on the web container.
I forced the /tmp/pids folder into the repo... and now it works!

Related

Elastic Beanstalk & Rails - nginx error "undefined local variable or method `location_config_filename' "

I'm trying to deploy my Rails5 app to eb container ( ruby 2.5 passenger standalone), but nginx error persists. how should I do to fix this?
: Completed activity. Result:
+ service passenger restart
According to the PID file '/var/app/support/pids/passenger.pid',
Phusion Passenger Standalone doesn't seem to be running.
If you know that Phusion Passenger Standalone *is* running then one of these
might be the cause of this error:
* The Phusion Passenger Standalone instance that you want to stop isn't running
on port 80, but on another port. If this is the case then you
should specify the right port with --port.
If the instance is listening on a Unix socket file instead of a TCP port,
then please specify the PID file's filename with --pid-file.
* The instance that you want to stop has stored its PID file in a non-standard
location. In this case please specify the right PID file with --pid-file.
/opt/elasticbeanstalk/support/conf/nginx_config_healthd.erb:49:in `get_binding': undefined local variable or method `location_config_filename' for #<PhusionPassenger::Standalone::StartCommand:0x000056170aa02450> (NameError)
from /opt/rubies/ruby-2.5.0/lib/ruby/2.5.0/erb.rb:885:in `eval'
from /opt/rubies/ruby-2.5.0/lib/ruby/2.5.0/erb.rb:885:in `result'
from /opt/rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/passenger-5.2.1/src/ruby_supportlib/phusion_passenger/standalone/start_command/nginx_engine.rb:187:in `block in write_nginx_config_file'
from /opt/rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/passenger-5.2.1/src/ruby_supportlib/phusion_passenger/standalone/start_command/nginx_engine.rb:180:in `open'
from /opt/rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/passenger-5.2.1/src/ruby_supportlib/phusion_passenger/standalone/start_command/nginx_engine.rb:180:in `write_nginx_config_file'
from /opt/rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/passenger-5.2.1/src/ruby_supportlib/phusion_passenger/standalone/start_command/nginx_engine.rb:42:in `start_engine_real'
from /opt/rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/passenger-5.2.1/src/ruby_supportlib/phusion_passenger/standalone/start_command.rb:323:in `start_engine'
from /opt/rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/passenger-5.2.1/src/ruby_supportlib/phusion_passenger/standalone/start_command.rb:63:in `run'
from /opt/rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/passenger-5.2.1/src/ruby_supportlib/phusion_passenger/standalone/main.rb:51:in `run!'
from /opt/rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/passenger-5.2.1/bin/passenger:45:in `<top (required)>'
from /opt/rubies/ruby-2.5.0/bin/passenger:23:in `load'
from /opt/rubies/ruby-2.5.0/bin/passenger:23:in `<main>'
Are you including the passenger gem in your Gemfile? If so, make sure it's only used in non-production groups. EB comes with passenger pre-installed, so if you're installing it as part of your application, you will see conflicts.
Speaking from experience, you may need to tear down your instance and relaunch it to recover from this...

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.

puma start causes "There is already a server bound to: <socket>" error

I haven't been able to reliably reproduce this issue, so I'll describe what's happening and hope one of you wise puma kids out there can help me out.
I always stop and start puma at the end of my deploy process. I run pumactl -F <config_path> stop, which works, and then the command puma -q -d -e staging -C <config_path>, which sometimes (not always) causes the following error:
[12802] Puma starting in cluster mode...
[12802] * Version 2.11.3 (ruby 2.1.2-p95), codename: Intrepid Squirrel
[12802] * Min threads: 2, max threads: 4
[12802] * Environment: staging
[12802] * Process workers: 2
[12802] * Phased restart available
[12802] * Listening on unix:///<app_dir>/tmp/puma/sockets/puma.sock
<ruby_path>/2.1.0/gems/puma-2.11.3/lib/puma/binder.rb:284:in `add_unix_listener': There is already a server bound to: <app_dir>/tmp/puma/sockets/puma.sock (RuntimeError)
<ruby_path>/2.1.0/gems/puma-2.11.3/lib/puma/binder.rb:124:in `block in parse'
<ruby_path>/2.1.0/gems/puma-2.11.3/lib/puma/binder.rb:82:in `each'
<ruby_path>/2.1.0/gems/puma-2.11.3/lib/puma/binder.rb:82:in `parse'
<ruby_path>/2.1.0/gems/puma-2.11.3/lib/puma/cluster.rb:325:in `run'
<ruby_path>/2.1.0/gems/puma-2.11.3/lib/puma/cli.rb:216:in `run'
<ruby_path>/2.1.0/gems/puma-2.11.3/bin/puma:10:in `<top (required)>'
<ruby_path>/2.1.0/bin/puma:23:in `load'
<ruby_path>/2.1.0/bin/puma:23:in `<main>'
If I try to run the same puma start command again afterwards, it works.
Here's my puma config:
#!/usr/bin/env puma
directory "<app_dir>/current"
rackup "<app_dir>/current/config.ru"
environment "staging"
pidfile "<app_dir>/tmp/puma/pid"
state_path "<app_dir>/tmp/puma/state"
activate_control_app "<app_dir>/tmp/puma/sockets/pumactl.sock"
stdout_redirect "<app_dir>/shared/log/puma.error.log", "<app_dir>/shared/log/puma.access.log", true
threads 2,4
bind "unix:///<app_dir>/tmp/puma/sockets/puma.sock"
workers 2
My questions:
How can I reproduce this error?
How can I fix it? Is this a problem with puma or with my configuration?
Instead of running both commands...
pumactl -F <config_path> stop
puma -q -d -e <env> -C <config_path>
just use the restart command:
pumactl -F <config_path> restart
Explanation
The puma README has a section on Restarting that contains no mention of calling the stop and start commands one after the other. Because that use case is unsupported, using the established method to restart the puma server will most likely clear up this issue.
Note
I was unable to consistently reproduce this puma error, so I can't 100% confirm that using pumactl restart fixes the problem. (Even though puma's lack of support for pumactl stop followed by puma implies that it does.) If someone continues to have this issue while using the supported pumactl restart, please let me know so I can modify this answer.

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.

Why do I get an error when starting ruby on rails app with mongrel_rails

Why do I get following error when trying to start a ruby on rails application with mongrel_rails start?
C:\RailsTest\cookbook2>mongrel_rails start
** WARNING: Win32 does not support daemon mode.
** Daemonized, any open files are closed. Look at log/mongrel.pid and log/mongr
el.log for info.
** Starting Mongrel listening at 0.0.0.0:3000
c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/../lib/mongrel/t
cphack.rb:12:in `initialize_without_backlog': Only one usage of each socket addr
ess (protocol/network address/port) is normally permitted. - bind(2) (Errno::EAD
DRINUSE)
from c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/../
lib/mongrel/tcphack.rb:12:in `initialize'
from c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/../
lib/mongrel.rb:93:in `new'
from c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/../
lib/mongrel.rb:93:in `initialize'
from c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/../
lib/mongrel/configurator.rb:139:in `new'
from c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/../
lib/mongrel/configurator.rb:139:in `listener'
from c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/mon
grel_rails:99:in `cloaker_'
from c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/../
lib/mongrel/configurator.rb:50:in `call'
from c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/../
lib/mongrel/configurator.rb:50:in `initialize'
from c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/mon
grel_rails:84:in `new'
from c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/mon
grel_rails:84:in `run'
from c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/../
lib/mongrel/command.rb:212:in `run'
from c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/mon
grel_rails:281
from c:/ruby/bin/mongrel_rails:19:in `load'
from c:/ruby/bin/mongrel_rails:19
You already have a process listening on port 3000 (the default port for mongrel).
Try:
mongrel_rails start -p 3001
and see whether you get a similar error.
If you're trying to install more than one Rails app, you need to assign each mongrel to a separate port and edit you apache conf accordingly.
If you not trying to do that, the most direct way of killing all mongrels is to open windows task manager and kill all the 'ruby' processes.
Note that if you have mongrel installed as a service that starts automatically
mongrel_rails install::service ...
...the ruby process will regenerate automatically. In that case, you'll have to edit the process properties through the windows services panel. Let me know if you need more info.
I don't use mongrel on windows myself, but I guess that error is the equivalent of Linux' "port in use" error. Are you trying to bind the server to a port where something else is already listening?
On Windows, I found two possible ways for fixing this issue:
Work around: Start the mongrel web server in another port
Solution: Find the ruby.exe process in your task manager and finish it

Resources