Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the default rails server in Rails 3?

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!

like image 249
airin Avatar asked Jan 03 '13 20:01

airin


People also ask

What is the default Rails server?

Rails. Puma is the default server for Rails, included in the generated Gemfile.

How do I change the default port in Rails?

Go to your browser and open http://localhost:3000, you will see a basic Rails app running. You can also use the alias "s" to start the server: bin/rails s . The server can be run on a different port using the -p option. The default development environment can be changed using -e .

What is application RB in Rails?

The configuration file config/application. rb and environment-specific configuration files (such as config/environments/production. rb ) allow you to specify the various settings that you want to pass down to all of the components. For example, you could add this setting to config/application.rb file: config.

What is the purpose of environment RB and application RB file?

In the environment. rb file you configure these run-levels. For example, you could use it to have some special settings for your development stage, which are usefull for debugging. The purpose of this file is to configure things for the whole application like encoding.


2 Answers

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
like image 59
denis.peplin Avatar answered Sep 19 '22 20:09

denis.peplin


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

like image 41
James Hebden Avatar answered Sep 20 '22 20:09

James Hebden