40

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

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 Task model, fired up the server via $ rails server, and attempted to load http://localhost:3000.

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

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

UPDATE

I just discovered that if I:

  1. Roll back to the fresh install of the first Rails app, before I created the Member scaffold
  2. Fire up the rails server via $ rails server in the application's root directory
  3. Check http://localhost:3000

It still attempts to go for the members route, the one that no longer exists because I rolled back via 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).

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?

Dave W.
  • 1,576
  • 2
  • 18
  • 29

8 Answers8

50

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 -p switch (e.g. rails server -p 3001)

cam
  • 14,192
  • 1
  • 44
  • 29
  • This is beautiful. Now I can run two applications that communicate with each other locally – JohnMerlino May 09 '13 at 21:18
  • 19
    I have a application running on port 3000, I need to start one more server on 3001. I ran this command rails server -p 3001, but got a issue. "A server is already running. Check /home/chronus/projects/groups/tmp/pids/server.pid." – 123 Jan 21 '14 at 11:15
39

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

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 :

rails s -p 3007 -P 42342
Dorian
  • 22,759
  • 8
  • 120
  • 116
Jaswinder
  • 1,455
  • 14
  • 27
  • 10
    the -P option appears to specify the pid file, rather than the pid itself. Instead use rails s -p 3007 -P `pwd`/tmp/pids/alternate_server.pid. Rails will populate this file with a new assigned pid. – Les Nightingill Aug 31 '15 at 16:19
  • 1
    for some reason specifying the PID is the only solution that works for me but why is using a different port not enough? – Salomanuel Apr 29 '20 at 10:03
18
rails s -p 4000 --pid tmp/pids/server2.pid

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

iconoclast
  • 21,213
  • 15
  • 102
  • 138
Balaji Radhakrishnan
  • 1,010
  • 2
  • 14
  • 28
12

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 3001 on Rails 3 or script/server -p 3001 on Rails 2.

Alex Korban
  • 14,916
  • 5
  • 44
  • 55
  • I started it on a different port (3001), and it still attempts to go for the "members" route, even though I rolled back to before I created that route/model/scaffold at all (see my update). – Dave W. Feb 08 '11 at 01:43
  • 2
    The server sits inside each app. You could try to look for stray server instances, e.g. with `sudo netstat -l -p | grep 3000` (this will show the process id of whatever is listening on port 3000). – Alex Korban Feb 08 '11 at 02:57
  • @Alex Korban makes a really key point for us newbies: it's because the server sits inside the app. That wasn't intuitive to me, but once it was clear, then all server behavior became much more rational. – Alex Edelstein Dec 21 '12 at 13:31
  • I always start rails server with default port `3000`.. How did you change it to `3001`? Asking out of curiosity.. :) – Arup Rakshit Dec 16 '14 at 06:47
  • I haven't looked at Rails 4 but I assume the port is still set with `-p`, same as in Rails 3: `rails s -p 3001`. – Alex Korban Dec 16 '14 at 20:41
9

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

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

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

Albert Català
  • 2,026
  • 2
  • 29
  • 35
  • Doesn't the second -p need to be -P (uppercase)? -p specifies port. -P specifies PID. – aldefouw Jun 20 '18 at 17:51
  • No, as I did, I specfy differents ports and differents PIDs, and there is no conflict between them – Albert Català Jun 21 '18 at 12:51
  • `-b 0.0.0.0`, in this case is not necessary, I use it to connect from another PC – Albert Català Jun 21 '18 at 12:52
  • 1
    Apologies. I misread your command text. When I enlarge it, I can see that the -P for PID is uppercase. It was just hard to see in thumbnail version because the case differences in your system font are very subtle. You were correct to begin with. – aldefouw Jun 22 '18 at 14:08
4

To start rails server, run the command rails s or rails server The following options are valid

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

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

Peter T.
  • 8,757
  • 3
  • 34
  • 32
3

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

$ 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
Bijendra
  • 9,467
  • 8
  • 39
  • 66
1

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!

Dave W.
  • 1,576
  • 2
  • 18
  • 29