Sarp Kaya
Sarp Kaya

Reputation: 3784

Rails 4 Change port number only for production environment

I found this question before: How to change Rails 3 server default port in develoment?

However what I really want is to change my port number for the production environment only. I am using RoR 4. It would be very nice if I could type something on production.rbin config/environments. Is there a way to do that?

Upvotes: 1

Views: 1070

Answers (1)

RyanWilcox
RyanWilcox

Reputation: 13974

The answer in brief

The rails stack has your application and then the server that runs your application (aka the "application server"). This server could be webrick (not a good idea in production), thin, gunicorn, passenger etc etc.

You should be telling that server which port to run under. You'll (likely) need to specify this outside Rails - not in config/production.rb because by the time Rails boots it's already running inside some application server.

A deeper dive with an example:

Let's use Heroku for an example, because port numbers there are essentially randomized (at least from the view of us looking in).

Heroku will pick a random port for us, then tell us through the PORT environmental variable. With Heroku you need a Procfile to tell it what services to launch, and your Procfile may look something like this:

web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb

See here, we use -p $PORT to tell unicorn - our application server in this example - to run on some port Heroku gave us.

Back to your question:

However you kick off your application serving process in your production environment, you should tell it to specify the port number to your web server. There's a bunch of ways to kick off your application serving process at a production level: from upstart (built into Ubuntu), to supervisord to god... all these methods run commands and make sure the process stays up (an important part of production level deployment ;) )

Upvotes: 2

Related Questions