How to set default port for Webrick?
Asked Answered
D

1

7

I want to set the default port when I do

rails s

to 3010, instead of having to say:

rails s -p 3010

...every time. Any ideas?

Detumescence answered 20/5, 2012 at 19:53 Comment(0)
C
11

You can override the Port by adding the following code to config/boot.rb

require 'rails/commands/server'
module Rails
  class Server
    alias :default_options_alias :default_options
    def default_options
      default_options_alias.merge!(:Port => 3010)
    end    
  end
end
Censorship answered 20/5, 2012 at 20:6 Comment(3)
This doesn't seem to work for me (using Eclipse/Aptana). The port is still 3000 after starting the server.Recite
FWIW, Rails 4.2 and higher now default to binding only to localhost instead of all interfaces. You can use default_options_alias.merge!(:Host => '0.0.0.0') in the above example to restore the former functionality if you need to access WEBrick from an outside machine.Altaf
More info on this at #3843318 , especially the use of super.merge({Port: 3010}) instead of the alias merge line. Both versions are working for me under Rails 4.2.11.3, Ruby 2.2.3.Pyrotechnics

© 2022 - 2024 — McMap. All rights reserved.