Railsを80番ポートで起動するには

macにて。

デフォルトは3000番。

(! 501)-> rails s
=> Booting WEBrick
=> Rails 3.2.13 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2014-01-16 09:42:13] INFO  WEBrick 1.3.1
[2014-01-16 09:42:13] INFO  ruby 1.9.3 (2013-02-22) [x86_64-darwin11.4.2]
[2014-01-16 09:42:13] INFO  WEBrick::HTTPServer#start: pid=14786 port=3000

ポートを指定する場合は-p xxxまたは--port=xxx

(! 502)-> rails s --port=3001
=> Booting WEBrick
=> Rails 3.2.13 application starting in development on http://0.0.0.0:3001
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2014-01-16 09:43:24] INFO  WEBrick 1.3.1
[2014-01-16 09:43:24] INFO  ruby 1.9.3 (2013-02-22) [x86_64-darwin11.4.2]
[2014-01-16 09:43:24] INFO  WEBrick::HTTPServer#start: pid=14824 port=3001

80番ポートで動かす場合も同じかと思ったが、エラーが出る。

(! 503)-> rails s --port=80
=> Booting WEBrick
=> Rails 3.2.13 application starting in development on http://0.0.0.0:80
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2014-01-16 09:44:19] INFO  WEBrick 1.3.1
[2014-01-16 09:44:19] INFO  ruby 1.9.3 (2013-02-22) [x86_64-darwin11.4.2]
[2014-01-16 09:44:19] WARN  TCPServer Error: Permission denied - bind(2)
Exiting

sudoすればいける。

(! 505)-> sudo rails s --port=80
=> Booting WEBrick
=> Rails 3.2.13 application starting in development on http://0.0.0.0:80
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2014-01-16 09:46:21] INFO  WEBrick 1.3.1
[2014-01-16 09:46:21] INFO  ruby 1.9.3 (2013-02-22) [x86_64-darwin11.4.2]
[2014-01-16 09:46:21] INFO  WEBrick::HTTPServer#start: pid=14964 port=80

ちなみに、1023まではsudoが必要で、1024以降はsudo不要のように見える。

毎回--port=80を打つのが面倒なので、80をデフォルトにする。

config/boot.rb

# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)

require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])

require 'rails/commands/server'

module Rails
  class Server
    def default_options
      super.merge({
        :Port => 80
      })
    end
  end
end