# \[n00b\] How to CONFIGURE port other then 3000?

**URL:** <https://www.ruby-forum.com/t/n00b-how-to-configure-port-other-then-3000/116604>\
**Category:** Rails\
**Created:** [October 7, 2007, 10:07pm UTC](https://www.ruby-forum.com/t/n00b-how-to-configure-port-other-then-3000/116604 "2007-10-07T22:07:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![olie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c4cdca/32.png) [@olie](https://www.ruby-forum.com/u/olie)\
**Post date:** [October 7, 2007, 10:07pm UTC](https://www.ruby-forum.com/t/n00b-how-to-configure-port-other-then-3000/116604/1 "2007-10-07T22:07:32Z")

</div>

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.

Is that possible?

Thanks!

---

<div class="post-metadata">

**Author:** ![olie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c4cdca/32.png) [@olie](https://www.ruby-forum.com/u/olie)\
**Post date:** [October 7, 2007, 10:11pm UTC](https://www.ruby-forum.com/t/n00b-how-to-configure-port-other-then-3000/116604/2 "2007-10-07T22:11:32Z")

</div>

On 10/7/07, Olie D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> rails scripts/server  
> Is that possible?
> 
> ## Thanks!
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

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

* * *

port: 3000  
servers: 3  
environment: development

and you run it with  
mongrel\_rails cluster::start

[http://mongrel.rubyforge.org/docs/mongrel\_cluster.html](http://mongrel.rubyforge.org/docs/mongrel_cluster.html)

Pat

---

<div class="post-metadata">

**Author:** ![olie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c4cdca/32.png) [@olie](https://www.ruby-forum.com/u/olie)\
**Post date:** [October 7, 2007, 10:12pm UTC](https://www.ruby-forum.com/t/n00b-how-to-configure-port-other-then-3000/116604/3 "2007-10-07T22:12:42Z")

</div>

On 10/7/07, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> in production (go figure) but you can use it to run multiple instances  
> [http://mongrel.rubyforge.org/docs/mongrel\_cluster.html](http://mongrel.rubyforge.org/docs/mongrel_cluster.html)
> 
> Pat

oh, wait, I’m dumb. You said three different apps.

I would just write a little wrapper around script/server. Not that  
hard.

Pat

---

<div class="post-metadata">

**Author:** ![olie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c4cdca/32.png) [@olie](https://www.ruby-forum.com/u/olie)\
**Post date:** [October 7, 2007, 10:30pm UTC](https://www.ruby-forum.com/t/n00b-how-to-configure-port-other-then-3000/116604/4 "2007-10-07T22:30:20Z")

</div>

On 10/7/07, Olie D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Pat M. wrote:
> 
> > 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.

---

<div class="post-metadata">

**Author:** ![olie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c4cdca/32.png) [@olie](https://www.ruby-forum.com/u/olie)\
**Post date:** [October 12, 2007, 5:03pm UTC](https://www.ruby-forum.com/t/n00b-how-to-configure-port-other-then-3000/116604/5 "2007-10-12T17:03:00Z")

</div>

Pat M. wrote:

> On 10/7/07, Olie D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Pat M. wrote:
> > 
> > > 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.

Thanks!

---

<div class="post-metadata">

**Author:** ![olie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c4cdca/32.png) [@olie](https://www.ruby-forum.com/u/olie)\
**Post date:** [October 12, 2007, 6:37pm UTC](https://www.ruby-forum.com/t/n00b-how-to-configure-port-other-then-3000/116604/6 "2007-10-12T18:37:27Z")

</div>

On 10/12/07, Olie D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > By “wrapper” I meant a little shell script
> 
> 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.

Pat

---

<div class="post-metadata">

**Author:** ![olie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c4cdca/32.png) [@olie](https://www.ruby-forum.com/u/olie)\
**Post date:** [October 7, 2007, 10:22pm UTC](https://www.ruby-forum.com/t/n00b-how-to-configure-port-other-then-3000/116604/7 "2007-10-07T22:22:41Z")

</div>

Pat M. wrote:

> 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:

#!/usr/bin/env ruby  
require File.dirname( **FILE** ) + ‘/…/config/boot’  
require ‘commands/server’

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’…” 🙂

Thanks!

---

<div class="post-metadata">

**Author:** ![olie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c4cdca/32.png) [@olie](https://www.ruby-forum.com/u/olie)\
**Post date:** [October 12, 2007, 6:48pm UTC](https://www.ruby-forum.com/t/n00b-how-to-configure-port-other-then-3000/116604/8 "2007-10-12T18:48:28Z")

</div>

Pat M. wrote:

> On 10/12/07, Olie D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > By “wrapper” I meant a little shell script

> > 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.)

Thanks!

---

<div class="post-metadata">

**Author:** ![olie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c4cdca/32.png) [@olie](https://www.ruby-forum.com/u/olie)\
**Post date:** [October 12, 2007, 7:24pm UTC](https://www.ruby-forum.com/t/n00b-how-to-configure-port-other-then-3000/116604/9 "2007-10-12T19:24:57Z")

</div>

On 10/12/07, Olie D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > that hard.
> 
> 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.

Also, I didn’t know that trick until two minutes ago. I just saw that  
script/server requires commands/server. So I googled “rails commands  
server” and the first result was  
[http://dev.rubyonrails.org/browser/trunk/railties/lib/commands/server.rb?rev=5103](http://dev.rubyonrails.org/browser/trunk/railties/lib/commands/server.rb?rev=5103)  
Then I looked at  
[http://dev.rubyonrails.org/browser/trunk/railties/lib/commands/servers/webrick.rb?rev=5103](http://dev.rubyonrails.org/browser/trunk/railties/lib/commands/servers/webrick.rb?rev=5103)  
to see exactly how it was getting the port…and modified  
script/server to do that. So this stuff is pretty easy to figure out  
if you spend a few minutes exploring.

Pat
