[英]How to change Rails 3 server default port in develoment?
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,这样我就不必在每次启动服务器时附加端口?
First - do not edit anything in your gem path!首先 - 不要在你的 gem 路径中编辑任何东西! It will influence all projects, and you will have a lot problems later...
会影响到所有的项目,以后会出很多问题...
In your project edit script/rails
this 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.但是这个解决方案有一个严重的缺点——它很无聊。
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
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 start
instead of rails server
然后调用
rake start
而不是rails server
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
We're using Puma as a web server, and dotenv to set environment variables in development.我们使用 Puma 作为 Web 服务器,并使用dotenv在开发中设置环境变量。 This means I can set an environment variable for
PORT
, and reference it in the Puma config.这意味着我可以为
PORT
设置一个环境变量,并在 Puma 配置中引用它。
# .env
PORT=10524
# config/puma.rb
port ENV['PORT']
However, you'll have to start your app with foreman start
instead 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.我喜欢这种方法,因为配置在开发和生产中的工作方式相同,您只需在必要时更改端口的值。
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_options are still set, and command-line switches still override defaults. default_options中的所有其他配置仍然设置,命令行开关仍然覆盖默认值。
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'
You could install $ gem install foreman
, and use foreman to start your server as defined in your Procfile
like:您可以安装
$ gem install foreman
,并使用 foreman 来启动您在Procfile
定义的服务器,例如:
web: bundle exec rails -p 10524
You can check foreman
gem docs here: https://github.com/ddollar/foreman for 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 Procfile
that 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干杯
If you're using puma (I'm using this on Rails 6+):如果您使用的是 puma(我在 Rails 6+ 上使用它):
To change default port for all environments:要更改所有环境的默认端口:
The "{3000}" part sets the default port if undefined in ENV.如果在 ENV 中未定义,“{3000}”部分设置默认端口。
~/config/puma.rb
change:
port ENV.fetch('PORT') { 3000 }
for:
port ENV.fetch('PORT') { 10524 }
To define it depending on the environment, using Figaro gem for credentials/environment variable:要根据环境定义它,请使用 Figaro gem 作为凭据/环境变量:
~/application.yml
local_db_username: your_user_name
local_db_password: your_password
PORT: 10524
You can adapt this to you own environment variable manager.您可以将其调整为您自己的环境变量管理器。
在 shell 中为具有指定端口的命令创建别名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.