How to change default port of a Rails 4 app?
Asked Answered
P

5

26

I know that I can start a rails server on another port via -p option. But I'd like to setup another port per application as long as I start webrick.

Any ideas?

Regards Felix

Parasynapsis answered 7/8, 2013 at 12:24 Comment(0)
U
32

Quick solution: Append to Rakefile

task :server do
  `bundle exec rails s -p 8080`
end

Then run rake server

Unblinking answered 8/8, 2013 at 17:1 Comment(0)
T
36

Append this to config/boot.rb:

require 'rails/commands/server'

module DefaultOptions
  def default_options
    super.merge!(Port: 3001)
  end
end

Rails::Server.send(:prepend, DefaultOptions)

Note: ruby >= 2.0 required.

Tjon answered 25/10, 2013 at 10:0 Comment(8)
prepend is public, you don't need to go through send: Rails::Server.prepend(DefaultOptions).Gibrian
Not in recent versions no. Send is necessary: api.rubyonrails.org/classes/Rails/Server.htmlContemporize
How do I retrieve this port from somewhere within the application?Savoy
Thanks, this works too when trying to change the default binding address :). Can you explain what does the code works?Batter
I like this one because it allows me to run the server with rails server (as usual), rather than the chosen answer which requires a call to something else.Largeminded
Actually nothing has been changedProportioned
@MadsBuus Recent versions of what? With Rails 4.2.5 and Ruby 2.2.3 plain prepend works fine.Uncompromising
This stopped working for me in Rails 5.1. It had a good run though. Thanks.Pneumatology
U
32

Quick solution: Append to Rakefile

task :server do
  `bundle exec rails s -p 8080`
end

Then run rake server

Unblinking answered 8/8, 2013 at 17:1 Comment(0)
J
22

Option 1:

You can launch WEBrick like so:

    rails server -p 8080

Where 8080 is your port. If you like, you can throw this in a bash script for convenience.

Option 2:

You could install $ gem install foreman, and use foreman to start your production webserver (e.g. unicorn) as defined in your Procfile like so: $ foreman run web. If unicorn is your web server you can specify the port in your unicorn config file (as with most server choices). The benefit of this approach is not only can you set the port in the config, but you're using an environment which is closer to production.

Jadotville answered 8/8, 2013 at 7:55 Comment(0)
N
4

If you put the default options on config/boot.rb then all command attributes for rake and rails fails (example: rake -T or rails g model user)! So, append this to bin/rails after line require_relative '../config/boot' and the code is executed only for the rails server command:

if ARGV.first == 's' || ARGV.first == 'server'
  require 'rails/commands/server'
  module Rails
    class Server
      def default_options
        super.merge(Host:  '0.0.0.0', Port: 3000)
      end
    end
  end
end

The bin/rails file loks like this:

#!/usr/bin/env ruby
APP_PATH = File.expand_path('../../config/application',  __FILE__)
require_relative '../config/boot'

# Set default host and port to rails server
if ARGV.first == 's' || ARGV.first == 'server'
  require 'rails/commands/server'
  module Rails
    class Server
      def default_options
        super.merge(Host:  '0.0.0.0', Port: 3000)
      end
    end
  end
end

require 'rails/commands'
Niggle answered 9/9, 2015 at 8:15 Comment(1)
When you say "fails", what do you mean? It seems to work for me.Uncompromising
T
2

For Rails 5.1:

# config/boot.rb

# ... existing code

require 'rails/command'
require 'rails/commands/server/server_command'

Rails::Command::ServerCommand.send(:remove_const, 'DEFAULT_PORT')
Rails::Command::ServerCommand.const_set('DEFAULT_PORT', 3333)
Tenter answered 20/10, 2017 at 8:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.