Ruby-on-rails 如何在开发中更改 Rails 3 服务器的默认端口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3842818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change Rails 3 server default port in develoment?
提问by Pierre Olivier Martel
On my development machine, I use port 10524. So I start my server this way :
在我的开发机器上,我使用端口 10524。所以我以这种方式启动我的服务器:
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?
有没有办法将默认端口更改为 10524,这样我就不必在每次启动服务器时附加端口?
采纳答案by Radek Paviensky
First - do not edit anything in your gem path! It will influence all projects, and you will have a lot problems later...
首先 - 不要在你的 gem 路径中编辑任何东西!会影响到所有的项目,以后会出很多问题...
In your project edit script/railsthis way:
在您的项目中以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'
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:
更新:是的,我知道使用 bash 脚本有更简单的解决方案,其中包含:
#!/bin/bash
rails server -p 10524
but this solution has a serious drawback - it is boring as hell.
但是这个解决方案有一个严重的缺点——它很无聊。
回答by Spencer
I like to append the following to config/boot.rb:
我喜欢将以下内容附加到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
回答by Ross
One more idea for you. Create a rake task that calls rails server with the -p.
再给你一个主意。创建一个使用 -p 调用 rails 服务器的 rake 任务。
task "start" => :environment do
system 'rails server -p 3001'
end
then call rake startinstead of rails server
然后调用rake start而不是rails server
回答by Thilo
Combining two previous answers, for Rails 4.0.4 (and up, presumably), this suffices at the end of config/boot.rb:
结合之前的两个答案,对于 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
回答by declan
We're using Puma as a web server, and dotenvto set environment variables in development. This means I can set an environment variable for PORT, and reference it in the Puma config.
我们使用 Puma 作为 Web 服务器,并使用dotenv在开发中设置环境变量。这意味着我可以为 设置一个环境变量PORT,并在 Puma 配置中引用它。
# .env
PORT=10524
# config/puma.rb
port ENV['PORT']
However, you'll have to start your app with foreman startinstead of rails s, otherwise the puma config doesn't get read properly.
但是,您必须使用foreman start而不是来启动您的应用程序rails s,否则 puma 配置将无法正确读取。
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.
我喜欢这种方法,因为配置在开发和生产中的工作方式相同,您只需在必要时更改端口的值。
回答by TuK
Inspired by Radek and Spencer... On Rails 4(.0.2 - Ruby 2.1.0 ), I was able to append this to config/boot.rb:
受 Radek 和 Spencer 的启发...在 Rails 4(.0.2 - Ruby 2.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
All other configuration in default_optionsare still set, and command-line switches still override defaults.
default_options中的所有其他配置仍然设置,命令行开关仍然覆盖默认值。
回答by Nowaker
Solution for Rails 2.3 - script/server:
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'
回答by Mark Jad
You could install $ gem install foreman, and use foreman to start your server as defined in your Procfilelike:
您可以安装$ gem install foreman,并使用 foreman 启动您的服务器,如您所定义的Procfile:
web: bundle exec rails -p 10524
You can check foremangem docs here: https://github.com/ddollar/foremanfor more info
您可以foreman在此处查看gem 文档:https: //github.com/ddollar/foreman了解更多信息
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 Procfilethat foreman will run for you so you don't have to go though them each time you want to start you application something like:
这种方法的好处不仅是您可以轻松地设置/更改配置中的端口,并且不需要添加太多代码,而且您还可以在Procfile领班为您运行的过程中添加不同的步骤,这样您就不需要'每次要启动应用程序时都不必经过它们,例如:
bundle: bundle install
web: bundle exec rails -p 10524
...
...
Cheers
干杯
回答by Casual Coder
Create alias in your shell for command with a specified port.
在 shell 中为具有指定端口的命令创建别名。

