15

I'm new to Rails and I'm wondering if there is an option to change the default rails server, i.e., webrick, for another one such as 'puma' or 'thin'. I know it is possible to specify which server to run with 'rails server' command, however I would like to use this command without specify the name of the server so it can run the default rails server. Is there a way to change the default rails server into a configuration file or something like this? Thanks in advance for your help!

airin
  • 447
  • 2
  • 5
  • 13

8 Answers8

23

Based on James Hebden's answer:

Add Puma to gemfile

# Gemfile
gem 'puma'

Bundle install it

bundle

Make it default, paste this code into script/rails above require 'rails/commands':

require 'rack/handler'
Rack::Handler::WEBrick = Rack::Handler.get(:puma)

So script/rails (in Rails 3.2.12) will look like:

#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.

APP_PATH = File.expand_path('../../config/application',  __FILE__)
require File.expand_path('../../config/boot',  __FILE__)
require 'rack/handler'
Rack::Handler::WEBrick = Rack::Handler.get(:puma)
require 'rails/commands'

Run server

rails s
=> Booting Puma
denis.peplin
  • 9,585
  • 3
  • 48
  • 55
  • 1
    To avoid an error when the puma gem isn't installed, you can wrap `Rack::Handler::WEBrick = Rack::Handler.get(:puma)` with `begin` ... `rescue LoadError` `end` – Abe Voelker May 03 '13 at 13:59
  • Important note (as mentioned): insert lines above `require ´rails/commands´`. Thanks for the solution. – Linus Jan 25 '16 at 07:14
9

Rack (the interface between rails and a web server) has handlers for the default WEBrick, and also for Thin. If you place the following in your Gemfile in the root of your rails project

gem 'thin'

rails server will automatically use Thin. This has been the case since 3.2rc2.

This unfortunately only applies to Thin, as Rack does not have built-in support for Unicorn, and others.

For servers that have Rack handlers (again, sadly Unicorn does not), you can do a bit of a hack to get rails server to use them. In your scripts/rails file in the root of your rails project, you can add the below just above `require 'rails/commands'

require 'rack/handler'
Rack::Handler::WEBrick = Rack::Handler::<name of handler class>

This essentially resets the handler for WEBrick to point to the handler for the server you would like to use.

To get an understanding of the supported Rack handlers, take a look at the comments in the source: https://github.com/rkh/rack/blob/master/lib/rack/handler.rb

  • 2
    Should be require `rails/commands`, not `rack/commands`. Can't edit your post, stupid StackOverflow says: `Edits must be at least 6 characters; is there something else to improve in this post?` :) – denis.peplin Feb 16 '13 at 15:42
  • This also applies to puma, if thin is not present. – Joshua Pinter Aug 20 '19 at 02:16
6

I think rails simply passes on the server option provided to rack. Rack has the following logic to determine what server to run:

https://github.com/rack/rack/blob/master/lib/rack/server.rb#L271-L273

def server
  @_server ||= Rack::Handler.get(options[:server]) || Rack::Handler.default(options)
end

The first case is when a :server option was passed to the rails server command. The second is to determine the default. It looks like:

https://github.com/rack/rack/blob/master/lib/rack/handler.rb#L46-L59

def self.default(options = {})
  # Guess.
  if ENV.include?("PHP_FCGI_CHILDREN")
    # We already speak FastCGI
    options.delete :File
    options.delete :Port

    Rack::Handler::FastCGI
  elsif ENV.include?("REQUEST_METHOD")
    Rack::Handler::CGI
  else
    pick ['thin', 'puma', 'webrick']
  end
end

Thin and Puma should be automatically picked up. The fallback is Webrick. Of course other web servers could override this behavior to make them the first in the chain.

If your Webserver is not picked up by default you could monkey-patch the default method to work like you want it. Of course this could break in future versions of rack.

Yves Senn
  • 2,006
  • 16
  • 13
  • What's exactly is going on in the line `pick ['thin', 'puma', 'webrick']`? `pick` doesn't seem to be part of the standard library (correct me if I'm wrong), so the snippet is a bit out of context. – Max Wallace May 21 '15 at 18:14
4

Rack will now look at a RACK_HANDLER environment variable file to see if you've specified a default rack handler. You can add a line like this to your .env file to set the default if you're using dotenv, or specify the assignment from the command line.

`RACK_HANDLER=webrick`

This should work as of this pull request:

https://github.com/rack/rack/pull/590

  • 1.6.0+ https://github.com/rack/rack/blob/e4e4c397e89c026f9c23500cf7fc14ccdb756010/lib/rack/handler.rb#L56 – dwschrashun Jul 11 '16 at 20:16
  • Worked for me thanks, did not want puma to load by default. – abhishek77in Oct 16 '17 at 13:11
  • Sorry but adding to `.env` does not work, passing in the shell command does – abhishek77in Oct 16 '17 at 13:12
  • Yeah, sorry. Adding to the .env will only work if Rails is reading your environment variables from there, as it does if you're using dotenv. The important point is that it'll read the environment variable however you get it in there. – dwschrashun Sep 20 '18 at 02:25
2

If you want unicorn/thin/etc, just add the gem to your gemfile

i.e. gem 'unicorn', gem 'thin', etc. then run bundle install at the command line.

As far as I can tell, adding either of these gems runs the appropriate server via rails server

UPDATE

Apparently this only works for Thin or Puma.

regulatethis
  • 2,322
  • 14
  • 17
  • when I add gem 'thin' to the gemfile it works, but for gem 'unicorn' it doesn't work because when I run the command rails server it starts Webrick instead of unicorn, for this reason I'm asking if there is another option. Thanks. – airin Jan 03 '13 at 21:47
2

I wouldn't get hung up on specifically using the rails server command. Just install whichever gem you want and alias the command (e.g. rails s Puma) to something simple like rs.

Tom L
  • 3,389
  • 1
  • 16
  • 14
2

If you have thin in your Gemfile, you need to do this:

require 'rack/handler'
Rack::Handler::Thin = Rack::Handler.get(:puma)
Tomasz Kapłon
  • 552
  • 6
  • 4
  • This did work for me although I don't have `thin` in my Gemfile and do have `puma` instead. Nevertheless, before this change Thin kept starting as the default server and the `Rack::Handler::WEBrick = Rack::Handler.get(:puma)` approach didn't work. I'm on Rails 4.2.6. Thanks! – Nic Nilov Nov 14 '16 at 20:39
1

If you use bash run: export RACK_HANDLER=webrick