代码之家  ›  专栏  ›  技术社区  ›  Pierre Olivier Martel

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

  •  162
  • Pierre Olivier Martel  · 技术社区  · 15 年前

    rails s -p 10524
    

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

    9 回复  |  直到 15 年前
        1
  •  132
  •   Radek Paviensky    15 年前

    在项目中编辑 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
    

    但是这个解决方案有一个严重的缺点-它非常无聊。

        2
  •  131
  •   Spencer    15 年前

    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
    
        3
  •  29
  •   Ross    14 年前

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

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

    rake start 而不是 rails server

        4
  •  17
  •   Thilo    11 年前

    结合前面的两个答案,对于rails4.0.4(以及更高版本,大概是这样),这就足够了 config/boot.rb :

    require 'rails/commands/server'
    
    module Rails
      class Server
        def default_options
          super.merge({Port: 10524})
        end
      end
    end
    
        5
  •  8
  •   declan    10 年前

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

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

    但是,你必须从 foreman start rails s ,否则puma配置无法正确读取。

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

        6
  •  4
  •   TuK    12 年前

    灵感来自拉德克和斯宾塞。。。

    # 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 仍然设置,并且命令行开关仍然覆盖默认值。

        7
  •  3
  •   Nowaker    12 年前

    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'
    
        8
  •  1
  •   Mshka    9 年前

    $ gem install foreman ,并使用foreman启动服务器,如 Procfile 比如:

    web: bundle exec rails -p 10524
    

    你可以查一下 foreman gem文档: https://github.com/ddollar/foreman 更多信息

    这种方法的好处是不仅可以轻松地在配置中设置/更改端口,而且不需要添加太多代码,还可以在配置中添加不同的步骤 那个工头会为你奔走,这样你就不必每次启动应用程序时都要经过他们,比如:

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

    干杯

        9
  •  0
  •   Mathieu Champagne    5 年前

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

    如果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
    

        10
  •  -4
  •   Casual Coder    13 年前

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