I'm new to Rails and I'm wondering if there is an option to change the default rails server, ie, 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!
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
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
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.
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:
If you want unicorn/thin/etc, just add the gem to your gemfile
ie 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.
I wouldn't get hung up on specifically using the rails server
command. Just install whichever gem you want and alias the command (eg rails s Puma
) to something simple like rs
.
If you have thin in your Gemfile, you need to do this:
require 'rack/handler'
Rack::Handler::Thin = Rack::Handler.get(:puma)
如果你使用bash run: export RACK_HANDLER=webrick
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.