I\'d like to bind the rails server to 127.0.0.1, instead of 0.0.0.0 so its not accessible when I\'m working from coffee shops.
Is there a configuration file where I can
I use Foreman as a process manager in development.
After adding gem 'foreman'
to your Gemfile and running bundle install
, create a file Procfile
in the root of your application directory.
While you can add lines to manage other processes, mine just reads:
web: rails server -p $PORT -b 127.0.0.1
Then, to start the Rails server via the Procfile, run foreman start
. If you have other processes here (Redis, workers) they'll boot at the same time.
If you put the default options on config/boot.rb
then all command attributes for rake and rails fails (example: rake -T
or rails g model user
)! So, append this to bin/rails
after line require_relative '../config/boot'
and the code is executed only for the rails server command:
if ARGV.first == 's' || ARGV.first == 'server'
require 'rails/commands/server'
module Rails
class Server
def default_options
super.merge(Host: '127.0.0.1', Port: 10524)
end
end
end
end
The bin/rails
file loks like this:
#!/usr/bin/env ruby
APP_PATH = File.expand_path('../../config/application', __FILE__)
require_relative '../config/boot'
# Set default host and port to rails server
if ARGV.first == 's' || ARGV.first == 'server'
require 'rails/commands/server'
module Rails
class Server
def default_options
super.merge(Host: '127.0.0.1', Port: 10524)
end
end
end
end
require 'rails/commands'
You can make a bash script to just run the command by default:
#!/bin/bash
rails server -b 127.0.0.1
Put it in the same folder as your project, name it anything you want (e.g. devserv
), then
chmod +x devserv
And all you have to do is ./devserv
If you are searching for Rails 5: Answer
In Rails ~> 4.0
you can customize the boot section of the Server class:
In /config/boot.rb
add this lines:
require 'rails/commands/server'
module Rails
class Server
def default_options
super.merge({Port: 10524, Host: '127.0.0.1'})
end
end
end
As already answered on this questions:
How to change Rails 3 server default port in develoment?
How to change the default binding ip of Rails 4.2 development server?