Prevent puma from running in locally (ruby on rails) - ruby-on-rails

I'm recently started to use Puma for my production server with nginx, however, when I now try to run my app locally, it tries to run Puma with all my production settings and fails. How can I prevent Puma from running locally?
AFAIK all I've done was added the puma gem to my gemfile, so I don't know how it's accessing my server config (I'm just not too knowledgeable in this area). I have it in my production group:
group :production do
gem 'pg'
gem 'rails_12factor'
gem 'puma'
end
Error:
→ rails s
=> Booting Puma
=> Rails 4.2.6 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[8917] Puma starting in cluster mode...
[8917] * Version 3.4.0 (ruby 2.0.0-p645), codename: Owl Bowl Brawl
[8917] * Min threads: 1, max threads: 6
[8917] * Environment: development
[8917] * Process workers: 1
[8917] * Phased restart available
[8917] * Listening on tcp://localhost:3000
[8917] Use Ctrl-C to stop
/rbenv/versions/2.0.0-p645/lib/ruby/gems/2.0.0/gems/puma-3.4.0/lib/puma/runner.rb:103:in `reopen': No such file or directory - /Users/me/mll/shared/log/puma.stdout.log (Errno::ENOENT)
Additionally, though less important to me right now, is it in my benefit to run Puma locally? If so, any tips/resources on how I can do that?

You need to put puma in your production group. Like this:
group :production do
gem 'puma'
end
That way puma will only be used on production and not development.
Update
Make sure that your bin/rails file looks like this:
#!/usr/bin/env ruby
APP_PATH = File.expand_path('../../config/application', __FILE__)
require_relative '../config/boot'
require 'rails/commands'

Having the exact same issue with Ben. I try to use web brick on local while developing and testing, and puma in production.
Gem file is perfectly define as puma only sits in production group. 'bin/rails' and 'config/application' are checked, same as #Răzvan Ciocănel suggested. Still boot with 'puma' on local.
At last, have a look into the 'bundle install' gem list, 'puma' is installed along with other 'production' gem. Run 'bundle install --without production' and now local will boot with web brick as ROR default.
I guess as long as puma is bundle installed, it will be loaded on local unless you config something to force it off. So the solution might be to remove the gem in the bundle list in development and test.

Rails 3 project (it keeps hanging around longer than expected...)
I changed from unicorn to puma in the production group of my gemfile.
group :production do
gem 'puma'
end
Then then when trying to run tests, or a dev server (which should have been thin) I got:
C:\Rails Projects\Rep>rails s
Could not find gem 'puma x86-mingw32' in any of the gem sources listed in your Gemfile or available on this machine.
Run `bundle install` to install missing gems.
After poking around a bit (including finding this question) I gave up and decided to go ahead and use puma in dev. I ran bundle install and tried running server and voila, thin was working again.
Then I realized I still only had it in the production group--I looked back at the bundle install and nothing was installed, puma still isn't installed. But now that bundle install has been run since the edit to the gemfile, everything works again.
C:\Rails Projects\Rep>rails s
=> Booting Thin
=> Rails 3.2.22.2 application starting in development on http://0.0.0.0:3000
So I guess the 'missing gem' error I wasn't because it was trying to actually run puma but rather some kind of bundler generated error due to unattempted gemfile? Just putting this out in case it speed things up for anyone in similar circumstances who ends up here.

Related

Using Puma with Rails application

My configuration:
Rails 5.2.3
Ruby 2.6.3
rbenv
puma
capistrano
nginx
Ubuntu 18.04
I've been reading up on how to get all this setup and configured for deployment, but I don't seem to be able to find one place, that's got a clear and complete guide.
One of the things that confuse me the most is Puma. Some of the guides, do not mention anything about configuring Puma on the server, and all the instructions, reference the Puma Gem. A couple of other guides, discuss setting up and configuring Puma on the server, and modifications in the /etc/init folder.
Do I need to setup and configure Puma on the server, separately, before I can start using it for my Rails application?
No you do not need to set up and configure Puma on the server. Puma is a gem. So if you list it in your application's Gemfile, when you run bundle install for your Rails application, Puma will be installed automatically.
Any configuration for puma should go in your application, under config/puma.rb.
Does this answer your question?
When running Rails locally, just use config/puma.rb for your Puma config, and run Puma similar to the following:
RAILS_LOG_TO_STDOUT=1 bin/pumactl -F config/puma.rb start
The above will run Puma in the foreground. I personally use Overmind in development to run Puma, Webpacker, Sidekiq, and sometimes ngrok. That means I only need one iTerm tab for all of those.
To get bin/pumactl, run bin/bundle binstubs puma.
You could also run Puma using bin/rails server, but Puma recommends not doing that because not all of Puma's configuration options are available through that method.
A sample Puma config (based on what the capistrano3-puma gem produces [https://github.com/seuros/capistrano-puma/blob/v4.0.0/lib/capistrano/templates/puma.rb.erb]):
# frozen_string_literal: true
# config/puma.rb
require 'dotenv/load' # load a .env file for environment variables; gem 'dotenv-rails'
require 'nenv' # nicer ENV handling; gem 'nenv'
directory(File.expand_path('..', __dir__))
rackup(File.expand_path('../config.ru', __dir__))
environment(Nenv.rack_env || Nenv.rails_env || 'development')
# or without nenv
#environment(ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'development')
threads(0, (Nenv.max_threads || 16).to_i)
# or without nenv
#threads(0, (ENV['MAX_THREADS'] || 16).to_i)
port((Nenv.port || 3_000).to_i)
# or without nenv
#port((ENV['PORT'] || 3_000).to_i)
workers 0
restart_command 'bundle exec puma'
prune_bundler
on_restart do
puts('Refreshing Gemfile...') # rubocop:disable Rails/Output
Nenv.bundle_gemfile = File.expand_path('../Gemfile', __dir__)
# or without nenv
#ENV['BUNDLE_GEMFILE'] = File.expand_path('../Gemfile', __dir__)
end
For handling Puma using Capistrano, I recommend the capistrano3-puma gem (https://github.com/seuros/capistrano-puma). This gem has various tasks, one of which is puma:config which will create a puma.rb file in the shared directory. You would then add puma.rb to the :linked_files Capistrano config option:
# config/deploy.rb
append(:linked_files, 'puma.rb')
As for setting up a server (one you deploy to), you want to install Nginx and set that up to serve your Rails app. Again, the capistrano3-puma gem can come in handy for this, as it includes an Nginx plugin which provides a task to upload a config to your server. If you don't want to use that plugin, then I'd at least recommend looking at the template (https://github.com/seuros/capistrano-puma/blob/v4.0.0/lib/capistrano/templates/nginx_conf.erb) for that config, and then adapting it to your needs.
As #matt-v-from-toronto mentioned, Puma is installed as part of your app, and not separately on the server. Doing a bin/bundle install, or similar, will install Puma along with all of your other gems listed in your Gemfile.

mri_21 is not a valid platform

Hi i switched my Ruby on Rails development platform from Windows 7 to Linux (Debian), I copied entire app directory to Linux and run bundle installand the command was successful. Than i run rake db:migrate and it quit with following error:
`mri_21` is not a valid platform. The available options are: [:ruby, :ruby_18, :ruby_19, :mri, :mri_18, :mri_19, :rbx, :jruby, :mswin, :mingw, :mingw_18, :mingw_19]
Than i Googled for the error and found this, i followed the suggested steps, i.e.:
gem update bundler
above command output:
Updating installed gems
Nothing to update
Than: bundle install and this was also successful.
But still getting same error on rake db:migrate
my ruby version: ruby 1.9.3p194
unable to get rails version by rails -v getting the same mri_21 error but in my app's Gemfile i have gem 'rails', '4.1.5'
EDIT: But when i tried bundle exec rake db:migrate it was successful. and than i run bundle exec rails s and app running successfully :
=> Booting Thin
=> Rails 4.1.5 application starting in development on 0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
=> Ctrl-C to shutdown server
>> Thin web server (v1.3.1 codename Triple Espresso)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:3000, CTRL+C to stop
Now my question is, how to get rid of this bundle exec every time, and what's going on with this.
Since bundle exec just fixes the PATH, and GEM_PATH variables, you should have all the gem binaries in the PATH variable before that are specified to wrong versions of rails, and gems. Also set GEM_PATH properly. So you be able to avoid the bundle exec call. All the values you are able to know with calls:
$ bundle exec echo GEM_HOME=$GEM_HOME\; GEM_PATH=$GEM_PATH\; PATH=$PATH
GEM_HOME=/home/malo/.rvm/gems/ruby-2.1.4; GEM_PATH=/home/malo/.rvm/gems/ruby-2.1.4:/home/malo/.rvm/gems/ruby-2.1.4#global; PATH=/home/malo/.rvm/gems/ruby-2.1.4/bin:/home/malo/.rvm/gems/ruby-2.1.4#global/bin:/home/malo/.rvm/rubies/ruby-2.1.4/bin:/home/malo/.rvm/bin:/usr/local/heroku/bin:/home/malo/.rbenv/bin:/home/malo/bin:/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin:/usr/games:/usr/lib/qt4/bin

Unicorn web server questions

I recently discovered this line in my Gemfile:
# Use unicorn as the app server
# gem 'unicorn'
I have 2 questions.
Why would I want to use unicorn over the default WEBrick?
How do I get it to work? I uncommented that line, ran bundle install and then rails server and it still booted up WEBrick
Reasons why you would use Unicorn instead of WEBrick?
Unicorn is supposed to be faster than WEBrick
You can spawn multiple processes
If you are using unicorn in production. You would want your development env to be as close as production.
How to run unicorn locally?
Uncomment gem 'unicorn' in Gemfile
Create unicorn.rb file in config/ and add the following line. You can increase the number of processes if you want to
worker_processes 1
start unicorn using the following command
unicorn -c config/unicorn.rb
While this is mostly an opinion answer, Unicorn supports multiple "worker" processes to handle concurrent web requests by executing one instance of Unicorn. The number of worker processes you can run depends on the specs of the hardware, but generally 3-4 workers is safe for small servers and even development machines. You'd need multiple WEBrick processes for concurrent requests. I've also found Unicorn to be faster than WEBrick, especially in production applications and apps running on Heroku. Heroku actually has some really good documentation on this that is applicable outside of Heroku as well.
Take a look at the Unicorn gem documentation as well as the Heroku docs above. TL;DR - you'll use the command unicorn instead of rails server to run your app using Unicorn.
You can also use the unicorn_rails gem which will override default webrick and unicorn instead
https://github.com/samuelkadolph/unicorn-rails

Specify which server a rails project will use

Just wondering if this can be done. You can specify you want a new rails project to use the postgresql server ike this:
rails new my-new-rails-project -d postgresql
and that takes care of the database yaml file.
Can an option be passed in here to specify puma as the development and production server so the relevant puma.rb configuration file is created?
Something like this:
rails new my-new-rails-project -d postgresql -s puma
By default Rails are using Webrick, but you can include different gem using Gemfile.
For example, you can use Thin (or puma, unicorn, whatever...) gem to your Gemfile and install it with bundler.
gem 'thin', group :development
When you start local server, rails will boot with custom webserver
rails server
=> Booting Thin
=> Rails 4.0.1 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Thin web server (v1.6.1 codename Death Proof)
Maximum connections set to 1024
Listening on 0.0.0.0:3000, CTRL+C to stop
There is no option for server configuration in Rails. You can check using below command.
rails new --help

How define his own server with Rails 3

I want use always thin when I start my rails server in development mode.
By default it webrick to use. So I add thin in my Gemfile
gem 'thin', :group => 'development'
Now If I want use it to launch my server in development mode I mandatory define it.
bundle exec rails s thin
If I don't define it, it's always use webrick. So How define using thin by default ?
Instead of rails s just type :
>> thin start -p 3000
Where 3000 is the number of your port.
You can also specify an enviornment :
>> thin start -e production
Assuming you are using bundler 1.0.x and your gems is vendorized:
bundle exec vendor/ruby/1.9.1/bin/thin start

Resources