How to change Rails 3 server default port in develoment?
Asked Answered
P

10

171

On my development machine, I use port 10524. So I start my server this way :

rails s -p 10524

Is there a way to change the default port to 10524 so I wouldn't have to append the port each time I start the server?

Paintbox answered 1/10, 2010 at 20:24 Comment(1)
simple answer is edit config/puma.rb if running default puma serverElectroform
S
137

First - do not edit anything in your gem path! It will influence all projects, and you will have a lot problems later...

In your project edit script/rails this way:

#!/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__)

# THIS IS NEW:
require "rails/commands/server"
module Rails
  class Server
    def default_options
      super.merge({
        :Port        => 10524,
        :environment => (ENV['RAILS_ENV'] || "development").dup,
        :daemonize   => false,
        :debugger    => false,
        :pid         => File.expand_path("tmp/pids/server.pid"),
        :config      => File.expand_path("config.ru")
      })
    end
  end
end
# END OF CHANGE
require 'rails/commands'

The principle is simple - you are monkey-patching the server runner - so it will influence just one project.

UPDATE: Yes, I know that the there is simpler solution with bash script containing:

#!/bin/bash
rails server -p 10524

but this solution has a serious drawback - it is boring as hell.

Seamus answered 1/10, 2010 at 21:22 Comment(6)
Or even an alias! alias rs='rails server -p 10524'Breadwinner
Be sure to put the require 'rails/commands' AFTER the new stuff you paste in. Otherwise it will still try port 3000.Careycarfare
doesn't work for me, still starts at :3000. Howver @Spencer solution (on this page) worksConard
Worked in one instance for me but not another. When I had to create my own script folder and rails file -> not great success. Probably has to do with whether or not rails is running off of the script or not?Geriatrician
@Breadwinner Could you explain how to create alias rsLavonlavona
@Lavonlavona in the shell window, run alias rs='rails server -p 10524'. After that, you'll be able to type rs in that shell to perform the full command. You may want to make this alias permanent by adding it to ~/.bashrc.Rezzani
N
131

I like to append the following 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 => 3333)
    end    
  end
end
Nilson answered 30/6, 2011 at 18:10 Comment(4)
Use super instead of alias hack.Azure
Unfortunately, if super is used instead of alias, it calls the wrong method. It calls the ::Rack::Server version of default_options.Assurbanipal
With ruby 2.0 you could prepend an anonymous module instead of using alias. This allows a clean use of super.Giulietta
This answer will cause Rails::Server to become defined in contexts when it shouldn't be (e.g. running the Rails console). So I recommend putting the code at the end of application.rb, guarded with an if defined?(Rails::Server).Pileous
B
30

One more idea for you. Create a rake task that calls rails server with the -p.

task "start" => :environment do
  system 'rails server -p 3001'
end

then call rake start instead of rails server

Blacking answered 18/7, 2012 at 3:35 Comment(0)
L
17

Combining two previous answers, for Rails 4.0.4 (and up, presumably), this suffices at the end of config/boot.rb:

require 'rails/commands/server'

module Rails
  class Server
    def default_options
      super.merge({Port: 10524})
    end
  end
end
Languet answered 6/5, 2014 at 8:32 Comment(2)
How can I retrieve this option in the running app? Specifically, I want to set config.action_mailer.default_url_options, otherwise it's still pointing to port 3000.Counterreply
I added a question related to this here: #29431815Counterreply
C
10

We're using Puma as a web server, and dotenv to set environment variables in development. This means I can set an environment variable for PORT, and reference it in the Puma config.

# .env
PORT=10524


# config/puma.rb
port ENV['PORT']

However, you'll have to start your app with foreman start instead of rails s, otherwise the puma config doesn't get read properly.

I like this approach because the configuration works the same way in development and production, you just change the value of the port if necessary.

Catfish answered 11/5, 2016 at 21:52 Comment(0)
B
4

Inspired by Radek and Spencer... On Rails 4(.0.2 - Ruby 2.1.0 ), I was able to append this to config/boot.rb:

# config/boot.rb

# ...existing code

require 'rails/commands/server'

module Rails
  # Override default development
  # Server port
  class Server
    def default_options
      super.merge(Port: 3100)
    end
  end
end

All other configuration in default_options are still set, and command-line switches still override defaults.

Birgit answered 5/4, 2014 at 7:25 Comment(0)
A
3

Solution for Rails 2.3 - script/server:

#!/usr/bin/env ruby
require 'rack/handler'
module Rack::Handler
  class << WEBrick
    alias_method :old_run, :run
  end

  class WEBrick
    def self.run(app, options={})
      options[:Port] = 3010 if options[:Port] == 3000
      old_run(app, options)
    end
  end
end

require File.dirname(__FILE__) + '/../config/boot'
require 'commands/server'
Azure answered 5/11, 2013 at 11:13 Comment(2)
Thanks! I came here looking for a solution with rails 2.3.18 :-)Mirilla
I did have to move the require File.dirname(__FILE__) + '/../config/boot' before require 'rack/handler' for it to work.Mirilla
I
3

If you're using puma (I'm using this on Rails 6+):

To change default port for all environments:

The "{3000}" part sets the default port if undefined in ENV.

~/config/puma.rb

change:
    port ENV.fetch('PORT') { 3000 }
for:
    port ENV.fetch('PORT') { 10524 }

To define it depending on the environment, using Figaro gem for credentials/environment variable:

~/application.yml
local_db_username: your_user_name
​local_db_password: your_password
PORT: 10524

You can adapt this to you own environment variable manager.

Ilona answered 5/2, 2021 at 20:40 Comment(0)
L
2

For ruby > 3 and For rails > 7

in file app/config/puma.rb, update the port number.

port ENV.fetch("PORT") { 3200 }
Lette answered 29/9, 2022 at 11:54 Comment(0)
C
1

You could install $ gem install foreman, and use foreman to start your server as defined in your Procfile like:

web: bundle exec rails -p 10524

You can check foreman gem docs here: https://github.com/ddollar/foreman for more info

The benefit of this approach is not only can you set/change the port in the config easily and that it doesn't require much code to be added but also you can add different steps in the Procfile that foreman will run for you so you don't have to go though them each time you want to start you application something like:

bundle: bundle install
web: bundle exec rails -p 10524
...
...

Cheers

Coordinate answered 3/9, 2017 at 7:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.