Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change my default localhost port in Rails 5

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

like image 611
Alaap Dhall Avatar asked Apr 23 '17 16:04

Alaap Dhall


People also ask

How do I change the default port in rails?

Go to your browser and open http://localhost:3000, you will see a basic Rails app running. You can also use the alias "s" to start the server: bin/rails s . The server can be run on a different port using the -p option. The default development environment can be changed using -e .

What is default rails port?

For example, Rails pass a default port of 3000 to the server. So if you boot your app using rails server and you don't specify a PORT environment variable you would expect this to connect to port 4000 but instead it connects to 3000 .


3 Answers

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!

like image 120
BertD Avatar answered Oct 19 '22 19:10

BertD


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.

like image 44
Md. Farhan Memon Avatar answered Oct 19 '22 18:10

Md. Farhan Memon


Change default port in rails 5

change config/puma.rb

port        ENV.fetch("PORT") { 3000 }
like image 1
Programming-Lover Avatar answered Oct 19 '22 20:10

Programming-Lover