How to change Rails 3 server default port in develoment?

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.

Leave a Comment