Ruby-on-rails 运行多个 Rails Server 实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4928680/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:21:30  来源:igfitidea点击:

Running multiple instances of Rails Server

ruby-on-rails

提问by Dave W.

I am new to Rails, so please forgive me if this is obvious.

我是 Rails 的新手,所以如果这很明显,请原谅我。

I am doing a lot of experimenting, creating to applications, testing features, etc. It got my first scaffolded app running great, but I wanted to create a second app to test a different feature.

我做了很多实验,创建应用程序,测试功能等。它让我的第一个脚手架应用程序运行良好,但我想创建第二个应用程序来测试不同的功能。

I backed up a folder level on my computer, ran $ rails new taskmaster(a test to-do list app). I ran the scaffolding for the Taskmodel, fired up the server via $ rails server, and attempted to load http://localhost:3000.

我在我的计算机上备份了一个文件夹级别,运行$ rails new taskmaster(一个测试待办事项列表应用程序)。我运行Task模型的脚手架,通过 启动服务器$ rails server,并尝试加载http://localhost:3000.

But I got a routing error, saying it couldn't find the "members"route. But memberswas from my first Rails app! I thought by firing off $ rails serverin the taskmasterdirectory, it would startup the server for that application.

但是我收到路由错误,说找不到"members"路由。但members来自我的第一个 Rails 应用程序!我认为通过$ rails servertaskmaster目录中启动,它将为该应用程序启动服务器。

How do I tell the Rails server which application to serve up?

如何告诉 Rails 服务器提供哪个应用程序?

UPDATE

更新

I just discovered that if I:

我刚刚发现,如果我:

  1. Roll back to the fresh install of the firstRails app, before I created the Member scaffold
  2. Fire up the rails server via $ rails serverin the application's root directory
  3. Check http://localhost:3000
  1. 在我创建 Member 脚手架之前,回滚到第一个Rails 应用程序的全新安装
  2. 通过$ rails server在应用程序的根目录中启动 rails 服务器
  3. 查看 http://localhost:3000

It still attempts to go for the membersroute, the one that no longer exists because I rolled back via git.

它仍然尝试寻找members路线,这条路线不再存在,因为我通过 git 回滚了。

I'm guessing this means something in my /usr/local/area, relating to my Ruby and Rails initial installs, is mainatining this info (my apps are setup in my Documents folder in my home dir).

我猜这意味着我所在/usr/local/地区的某些东西,与我的 Ruby 和 Rails 初始安装有关,正在维护此信息(我的应用程序设置在我的主目录的 Documents 文件夹中)。

I thought that Rails apps were essentially self contained apps inside the directory - you just needed a working Ruby install to get them going. Does the Rails server sit inside each app directory, or is the some overarching Rails server that accommodates all apps?

我认为 Rails 应用程序本质上是目录中的自包含应用程序 - 您只需要安装一个有效的 Ruby 即可让它们运行。Rails 服务器是否位于每个应用程序目录中,还是某个包含所有应用程序的总体 Rails 服务器?

采纳答案by Dave W.

Thanks for all your help - turns out it was a rather strange occurrence. Somehow, my entire project folder got copied into the Trash. When I started the server, I was starting the server instance in the Trash copy, while the copy I rolled back and edited stay in the same place. Not sure how that happened (perhaps it relates to git, another tool I am just learning). In any case, thanks for all the help, sorry it was something so simple!

感谢您的所有帮助 - 事实证明这是一个相当奇怪的事件。不知何故,我的整个项目文件夹都被复制到垃圾箱中。当我启动服务器时,我正在垃圾箱副本中启动服务器实例,而我回滚和编辑的副本则留在同一个地方。不知道这是怎么发生的(也许它与 git,我刚刚学习的另一个工具有关)。无论如何,感谢所有的帮助,对不起,事情太简单了!

回答by cam

I suspect the old server was still running and the new server failed to start. Try killing it first and then start it your new app.

我怀疑旧服务器仍在运行,而新服务器无法启动。尝试先杀死它,然后将其启动为您的新应用程序。

Alternatively, you could start the new server on a different port by using the -pswitch (e.g. rails server -p 3001)

或者,您可以使用-p开关(例如rails server -p 3001)在不同的端口上启动新服务器

回答by Jaswinder

You can run multiple instances of webrick server on localhost by assigning a different port number as:

您可以通过分配不同的端口号在本地主机上运行多个 webrick 服务器实例:

rails s -p 3007

But sometimes it may not work.

但有时它可能不起作用。

I have a tip for you.You can try using this along with other options provided by webrick. Just try with providing any number as PID using -P:

我有一个提示。您可以尝试将其与 webrick 提供的其他选项一起使用。只需尝试使用以下方法提供任何数字作为 PID -P

rails s -p 3007 -P 42342

回答by Alex Korban

You can't really tell the server which application to serve, but you can run a server for each application, and choose which one to load. If you want to run more than one server, you'll have to start them on different ports. The default port is 3000. To start a server on port 3001, run rails s -p 3001on Rails 3 or script/server -p 3001on Rails 2.

您无法真正告诉服务器要为哪个应用程序提供服务,但是您可以为每个应用程序运行一个服务器,并选择要加载哪个应用程序。如果要运行多个服务器,则必须在不同的端口上启动它们。默认端口是 3000。要在端口 3001 上启动服务器,请rails s -p 3001在 Rails 3 或script/server -p 3001Rails 2 上运行。

回答by Albert Català

In Rails 5 and Puma server, this is the way I could achieve this:

在 Rails 5 和 Puma 服务器中,这是我可以实现的方式:

With two terminals, run rails serverin each terminal specifying different Pid files and different ports: enter image description here

使用两个终端,rails server在每个终端中运行指定不同的 Pid 文件和不同的端口: 在此处输入图片说明

this way, I can simulate two domains for the same app on development

这样,我可以在开发中为同一个应用程序模拟两个域

回答by Balaji Radhakrishnan

rails s -p 4000 --pid tmp/pids/server2.pid

The above command will safely start a second server in port 4000

上面的命令将在 4000 端口安全地启动第二个服务器

回答by Peter T.

To start rails server, run the command rails sor rails serverThe following options are valid

要启动 Rails 服务器,请运行命令rails srails server以下选项有效

-p Port
-b Binding (ip address)
-c Config file (for custom rack configuration)
-d Daemonize server
-u Enable debugger
-e Change the environment (defaults to development)
-P Specify a PID file

So to run an instance to different port in local machine, use the following command

因此,要将实例运行到本地计算机的不同端口,请使用以下命令

rails s -b 127.0.0.1 -p 8081 

Note that you can remove "127.0.0.1" as "localhost" is the default host.

请注意,您可以删除“127.0.0.1”,因为“localhost”是默认主机。

For more information, check this reference http://guides.rubyonrails.org/command_line.html#rails-server

有关更多信息,请查看此参考http://guides.rubyonrails.org/command_line.html#rails-server

回答by Bijendra

In the current version Rails 5.2.0 and Ruby 2.4.1p111, starting two instances of server for the same app is possible with multiple PIDs.

在当前版本的 Rails 5.2.0 和 Ruby 2.4.1p111 中,可以使用多个 PID 为同一个应用程序启动两个服务器实例。

$ rails s 
=> Booting Puma
=> Rails 5.2.0 application starting in development 
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.11.4 (ruby 2.4.1-p111), codename: Love Song
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

Now starting one more server on different port fails with pid issues.

现在在不同的端口上再启动一台服务器会因 pid 问题而失败。

$ rails s -p 3001
=> Booting Puma
=> Rails 5.2.0 application starting in development 
=> Run `rails server -h` for more startup options
A server is already running. Check /Users/biju/app1/tmp/pids/server.pid.
Exiting

Below approach of starting server works to use multiple instances of application.

以下启动服务器的方法适用于使用多个应用程序实例。

$ rails s -p 3001 -P 321412
=> Booting Puma
=> Rails 5.2.0 application starting in development 
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.11.4 (ruby 2.4.1-p111), codename: Love Song
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3001
Use Ctrl-C to stop