I’ve Googled and checked the archives, but the only answer I can find
about firing up on port other than 3000 is to either modify the library
default (?!) or use the command-line argument --port=# (eh…)
What I’d LIKE to do is run 3 Rails apps (all in development, so they all
want port 3000), but run them on ports 3000, 3001, 3002. For
ease-of-administration, it’d be convenient if I could start them all
with
rails scripts/server
and not have to remember which one is one which port as I flit between
them. (In addition, as I share them with another developer, I don’t
want to have to worry about conflicts with HIS development rails apps
running, nor whether he’ll remember to use the --port=# option.)
So, what I’m hoping to do is put some magic incantation somewhere in my
config directory (config/environment.rb?), and have a different
default-port for each app.
Look in to mongrel_cluster. Usually we use it for clustering mongrels
in production (go figure) but you can use it to run multiple instances
mapped to different ports automatically. An example config would be
I would just write a little wrapper around script/server. Not that
hard.
Did I mention “n00b”?
By “wrapper” I meant a little shell script
$ cat bin/run_server
ruby script/server -p 3001
Now in your project when you run
./bin/run_server
it runs script/server on port 3001. Copy that file into each project
you want and change the port to be whatever.
If I could count on my installation people to run my little shell
script, I could just ask them to use -p 3001 The whole thing I’m
trying to accomplish, here, is to make it fail-safe.
If I could count on my installation people to run my little shell
script, I could just ask them to use -p 3001 The whole thing I’m
trying to accomplish, here, is to make it fail-safe.
So, tell them to run bin/run_server instead of script/server? Not
that hard. If they forget to, they’ll know when mongrel tries to
start up.
If you’re super paranoid though, just move script/server to some other
file, maybe script/rails_server, and save the wrapper file to
script/server. Then when people run that they actually get your
custom wrapper.
I would just write a little wrapper around script/server. Not that
hard.
Did I mention “n00b”?
(To be fair, I’m (theoretically) a Sr. developer, but I’m uber-new to
Ruby/rails…) FWIW, one thing n00bs find interesting is that
script/server is only 2 lines:
And, until one figures out that “require” = “#include” (or “import” ;),
the fact that server does anything is a little mystery not covered in
any of the n00b-tutorials…
Well, the hack-solution would be to figure out a way to stuff ARGV
before the 1st require, but that’s pretty lame. I could modify
config/boot.rb, but it specifically says not to. That leaves me with
figuring out where commands/server is
(/usr/lib/ruby/some/path/I’m/sure…) and what I want to wrap around
that.
It’d be NICE if the answer was something like: change the 2nd line to
require ‘commands/server’ --port=3001
but I bet it’s not that simple. Or maybe
ENV[ENV_PORT]=“3001”
require ‘commands/server’
or something similar. Anyway, I’ll have a look at it – I’m sure it’s
good for me to learn how all this inner-gerwerkkins stuff works; I was
just being lazy (well, “wanting to get on with the ‘real work’…”
If I could count on my installation people to run my little shell
script, I could just ask them to use -p 3001 The whole thing I’m
trying to accomplish, here, is to make it fail-safe.
So, tell them to run bin/run_server instead of script/server? Not
that hard.
I appreciate your reply – I really do! – but that’s not what I’m
asking…
I’m trying to learn how to CONFIGURE my app such that, when I start it
with “ruby scripts/server”, it defaults to some port other than 3000.
I’m not trying to learn how to create a wrapper script. I’m not trying
to learn how to change the behaviour of a group of people I don’t
control. I’m trying to learn how to configure my rails app.
One rather heavy-handed approach is to use mongrel clusters. Mongrel
has a configuration file that lets me specify that my app fire up on
whatever port I put in the config file. This works quite well, but is a
bit ham-fisted for what I’m trying to do. I’m trying to ask if there’s
a way to do this using standard “out of the box” rails (implied:
w/WEBrick.)
One rather heavy-handed approach is to use mongrel clusters. Mongrel
has a configuration file that lets me specify that my app fire up on
whatever port I put in the config file. This works quite well, but is a
bit ham-fisted for what I’m trying to do. I’m trying to ask if there’s
a way to do this using standard “out of the box” rails (implied:
w/WEBrick.)
Thanks!
Yes, there is, and you already know about it. It’s the -p option.
Here’s what you’re failing to understand. The web server is
independent from the app itself. So, you’re going to need to
configure the web server, not the rails app. This means either
passing in an extra option to the server, or creating a config file in
mongrel’s case.
You can change script/server to be
#!/usr/bin/env ruby
unless ARGV.include?(“-p”)
ARGV << “-p”
ARGV << “3001”
end
require File.dirname(FILE) + ‘/…/config/boot’
require ‘commands/server’
which will start it up on port 3001 unless some other port is
specified. If that’s not good enough for you, sorry.