Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在开发中更改Rails3服务器默认端口?_Ruby On Rails - Fatal编程技术网

Ruby on rails 如何在开发中更改Rails3服务器默认端口?

Ruby on rails 如何在开发中更改Rails3服务器默认端口?,ruby-on-rails,Ruby On Rails,在我的开发机器上,我使用端口10524。因此,我通过以下方式启动服务器: rails s -p 10524 有没有办法将默认端口更改为10524,这样我就不必在每次启动服务器时都附加端口?在shell中为具有指定端口的命令创建别名 首先-不要编辑gem路径中的任何内容!它会影响所有的项目,你以后会有很多问题 在项目中,按以下方式编辑script/rails: #!/usr/bin/env ruby # This command will automatically be run when yo

在我的开发机器上,我使用端口10524。因此,我通过以下方式启动服务器:

rails s -p 10524

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

在shell中为具有指定端口的命令创建别名

首先-不要编辑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
require 'rails/commands/server'

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

再给你一个主意。创建一个用-p调用rails服务器的rake任务

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

然后调用
rake start
而不是
rails服务器

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'
#/usr/bin/env ruby
需要“机架/处理器”
模块机架::处理程序

课程灵感来自Radek和Spencer。。。 在Rails 4(.0.2-Ruby 2.1.0)上,我能够将其附加到config/boot.rb中:


中的所有其他配置仍处于设置状态,并且命令行开关仍覆盖默认值

结合前面的两个答案,对于Rails 4.0.4(以及更高版本),这在
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
require 'rails/commands/server'

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

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

# .env
PORT=10524


# config/puma.rb
port ENV['PORT']
但是,您必须使用
foreman start
而不是
rails s
启动应用程序,否则无法正确读取puma配置


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

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

web: bundle exec rails -p 10524
您可以在此处查看
foreman
gem文档:了解更多信息

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

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

如果您正在使用puma(我在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

您可以根据自己的环境变量管理器对其进行调整。

在gem路径中编辑文件是。。。好吧,只为最勇敢的人。它将无法通过gem更新,无法在更多的计算机上工作,等等。我真的不推荐它。你是对的。你的解决方案好多了。我不知道,我可以在
script/rails
中重写它。谢谢。请使用
super
而不是alias hack。不幸的是,如果使用
super
而不是alias,则会调用错误的方法。它调用默认选项的
::Rack::Server
版本。使用ruby 2.0,您可以
prepend
匿名模块,而不是使用
alias
。这允许干净地使用
super
。此答案将导致
Rails::Server
在不应该定义的上下文中定义(例如运行Rails控制台)。因此,我建议将代码放在
application.rb
的末尾,如果定义了
(Rails::Server)
,则使用
保护,甚至使用别名
alias rs='rails server-p 10524'
请确保在粘贴新内容后添加
require'rails/commands'
。否则它仍将尝试端口3000。对我不起作用,仍然从:3000开始。然而@Spencer solution(在本页上)在一个实例中为我工作,而不是在另一个实例中。当我不得不创建我自己的脚本文件夹和rails文件->不是很成功。可能与rails是否脱离脚本有关?@trisweb您能否解释一下如何创建
alias rs
谢谢!我来这里是为了寻找rails 2.3.18的解决方案:-)我确实必须将
require File.dirname(\uu File\uuu)+'/../config/boot'
移动到
require'rack/handler'
之前才能工作。我如何在正在运行的应用程序中检索此选项?具体来说,我想设置
config.action\u mailer.default\u url\u options
,否则它仍然指向端口3000。我在这里添加了一个与此相关的问题:如果运行默认puma服务器,简单的答案是编辑
config/puma.rb