6

I am using rails 5 which works with a default puma server and listen to localhost:3000

I want it to listen to a new port like 192.168.0.0:3000

Can anyone help ? thank you

Eyeslandic
  • 14,553
  • 13
  • 41
  • 54
Alaap Dhall
  • 351
  • 1
  • 3
  • 16

3 Answers3

12

Rails 5 comes with puma, which is configured in config/puma.rb. You can change the default port number in that file, or override it by setting the PORT environment variable before starting rails.

@Iceman: in Rails 5, it is not required to monkey patch Rails to override the default port, so the answer you referred to is no longer relevant.

Edit: upon re-reading the original question, I notice that you do not want to change the port, but rather the bind address. You can do that by editing config/puma.rb and replacing the port statement with a bind statement:

# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
#port        ENV.fetch("PORT") { 3000 }
bind        'tcp://192.168.0.1:3000'

@JohnLinux: Rails is not aware of the fact that Puma uses a different bind address, so it tells you about what it passed down to Puma (which Puma ignores). There are several issues in both Rails' and Puma's github issue trackers that deal with this, and AFAICT there have been changes on both ends to pass control of the bind address back to Rails, but I have not toyed yet with updated gems to see how far that got. It is important to comment out the port statement, otherwise Puma actually binds to both!

BertD
  • 618
  • 8
  • 11
  • This is useful, but why does it still say `Rails 5.0.3 application starting in development on http://localhost:3000` when starting it up, even though it's listening on 3003. – Steve Jul 10 '17 at 08:04
  • Funny you should mention that. I never noticed that. As it turns out, it listens on both 3000 and 3003, which needless to say fails miserably when you runtwo seperate instances on the same computer. There appears to be no easy switch to disable that behavior. There are a number os issues open on github for puma that address parts of this situation, but none that are release AFAICT. – BertD Nov 21 '17 at 13:56
  • 1
    Seems like this issue has been fixed with the latest version of Rails. – mutantkeyboard Feb 12 '18 at 15:13
3

You can bind the server using -b option like

rails s -p 3000 -b 0.0.0.0 where -p is for port option and 0.0.0.0 will bind to you localhost ip if it is 192.168.0.0 and you can open your app with connected devices in your network. If you are looking to change your local ip address..that's not a rails question.

Md. Farhan Memon
  • 6,055
  • 2
  • 11
  • 36
1

Change default port in rails 5

change config/puma.rb

port        ENV.fetch("PORT") { 3000 }
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Programming-Lover
  • 1,177
  • 10
  • 14