如何在开发中更改 Rails 3服务器默认端口?

在我的开发机器上,我使用端口10524:

rails s -p 10524

有没有办法将默认端口更改为10524,这样我就不必在每次启动服务器时附加端口?

136086 次浏览

首先-不要编辑您的 gem 路径中的任何内容!它会影响到所有的项目,以后你会遇到很多问题..。

在你的项目中,这样编辑 script/rails:

#!/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'

原理很简单-你是猴子修补服务器运行器-所以它只会影响一个项目。

更新 : 是的,我知道 bash 脚本有更简单的解决方案,包括:

#!/bin/bash
rails server -p 10524

但是这个解决方案有一个严重的缺点——它无聊透顶。

我想在 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

还有一个想法: 创建一个 rake 任务,使用-p 调用 Railsserver。

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

然后调用 rake start而不是 rails server

Rails2.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'

受 Radek 和 Spencer 的启发..。 在 Rails4(. 0.2-Ruby2.1.0)中,我能够将这个附加到 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

Default _ options中的所有其他配置仍然被设置,命令行开关仍然覆盖默认值。

结合前面的两个答案,对于 Rails 4.0.4(可能还有更高版本) ,这在 config/boot.rb的末尾就足够了:

require 'rails/commands/server'


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

我们使用 Puma 作为 Web 服务器,使用 Dotenv在开发中设置环境变量。这意味着我可以为 PORT设置一个环境变量,并在 Puma 配置中引用它。

# .env
PORT=10524




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

但是,您必须使用 foreman start而不是 rails s启动应用程序,否则将无法正确读取彪马配置。

我喜欢这种方法,因为配置在开发和生产中的工作方式是相同的,只要在必要时更改端口的值即可。

您可以安装 $ gem install foreman,并使用 Foreman 启动 Procfile中定义的服务器,如:

web: bundle exec rails -p 10524

你可以在这里查看 foreman gem docs: https://github.com/ddollar/foreman获得更多信息

这种方法的好处是不仅可以很容易地设置/更改配置中的端口,而且不需要添加太多代码,还可以在 Procfile中添加 Foreman 为您运行的不同步骤,这样您就不必每次启动应用程序时都要经过这些步骤,比如:

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

干杯

如果你正在使用彪马(我在 Rails 6 + 上使用它) :

更改所有环境的默认端口:

如果在 ENV 中未定义,“{3000}”部分将设置默认端口。

~/config/puma.rb


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

要根据环境定义它,使用 Figaro gem 作为凭证/环境变量:

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

你可以把这个改造成你自己的环境变量经理。

对于 ruby > 3和 Rails> 7

在文件 app/config/puma.rb 中,更新端口号。

port ENV.fetch("PORT") { 3200 }