Saturday, April 22, 2017

The trick to start Rails server

Today, I have got a simple and practical method to start my Rails server in order to make it work properly. If I want to start Rails server, I have to type this in Linux Terminal:
$ rails s -b 0.0.0.0 -p 3000

I did not know this until today I wanted to connect to Rails server remotely using my MacBook Air.

1. At the beginning, I started Rails server using my old method:
$ rails s

=> Booting Puma
=> Rails 5.0.2 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.8.2 (ruby 2.4.0-p0), codename: Sassy Salamander
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop

2. I could navigate to the Rails server locally using the Firefox connection localhost port 3000 from my Dell desktop which is hosting the Rails server:


3. But I could not navigate to Rails server remotely using the Firefox in my MacBook Air.


 
4. Afterwards, I did some research using Google search and read some articles in stackoverflow.com and serverfault.com. To sum up, I have to start the Rails server by:
$ rails s -b 0.0.0.0 -p 3000
instead of
$ rails s

5. The next step, I stopped the Rails server by pressing Ctrl + C. And then I typed: $ rails s -b 0.0.0.0 -p 3000:
$ rails s -b 0.0.0.0 -p 3000

=> Booting Puma
=> Rails 5.0.2 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.8.2 (ruby 2.4.0-p0), codename: Sassy Salamander
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

6. This time, I can connect to Rails server remotely using Firefox from my MacBook Air.


In refer to the article from Stackoverflow, I am not fully understand what does the parameter -b 0.0.0.0 mean. But basically, if I start the Rails server without parameter, the Rails server only accept local connection. If I add the -b 0.0.0.0 parameter Rails is listening on all interfaces, not just the loopback interface.


References:
Can't access ports assigned to Rails 4.2, but 4.04 works fine
https://serverfault.com/questions/625841/cant-access-ports-assigned-to-rails-4-2-but-4-04-works-fine

Why does Ruby on Rails use http://0.0.0.0:3000 instead of http://localhost:3000?
http://stackoverflow.com/questions/1478747/why-does-ruby-on-rails-use-http-0-0-0-03000-instead-of-http-localhost3000

1 comment:

How to kill an abandoned process in Linux/Unix

I remembered it, then I forgot, then I remembered it, and then I forgot again. In case of a Linux/Unit process hang, I have to figure out ...