Rails 4 Change port number only for production environment - ruby-on-rails

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?

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 ;) )

Related

Heroku: How to open rails api dyno on port 3001

Here is my procfile:
web: cd client && npm start
api: bundle exec rails s -p 3001
The web app works perfectly and runs on port 80 (see here: https://trucktrack-demo.herokuapp.com/) However I cannot seem to find the port that the rails api is running on. Any ideas on how to find this port (it is not 3001)?
Thanks in advance :-)
From the Heroku Procfile docs:
The web process type is special as it’s the only process type that will receive HTTP traffic from Heroku’s routers. Other process types can be named arbitrarily.
If you want to have multiple HTTP applications running, you’re going to have to accomplish that a different way, as you can’t have more than one HTTP process type in an app. One option would be to run a completely separate Heroku app for the other HTTP app.

Ruby on rails : launch server with code reload

I'm using a server for testing my api. I use this command to launch the server :
rails s -p 3001 -e test -P 42342
I change the port to 3001, to not have a conflict with my dev server (on port 3000).
The thing is that when I change code, the server don't reload the code, I have to kill the server and relaunch.
Is there a parameters that I miss to launch my rails command ?
test is only designed for automated testing, with Rails running for a single test run. Since code should not be changing during a test case, it doesnt have many of the development mode features and is more like production in that regard. Its also intended to reset your database when running tests, which also doesnt seem to be your intention.
If you want (most) classes to auto-reload, always use the development environment.
If for some reason you need a different environment (more than just port, but different configs), you can look at creating a new environment, copying the configs for development (add new entries to config/environments/, config/database.yml, config/secrets.yml, and use as a group in the Gemfile.).

rails scope root and multiple apps

I have a working rails app accessible directly from http://0.0.0.0:3000/ . The app is also in staging and production in heroku.
Today, I want to start working on a new rails app in the same computer. How can I start working on the new unrelated app under a different path without messing up my staging/production urls ?
How can I have something like this locally and switch between the two apps
http://0.0.0.0:3000/existingApp/
http://0.0.0.0:3000/newapp/
I tried scope "/existingApp" do in my routes.rb for / and I suppose I should do that for the new app as well... but how do I specify this only for my local environment? I would like my heroku urls to stay unchanged (ie stay at the root).
I wouldn't recommend doing what you're doing, but if you're gonna do it anyway, you could try writing an engine and mounting it.
This could help get you started.
Almost any server would be able to be configured to listen to a port other than 3000. Thin, for example, can be started as:
thin -R config.ru -a 127.0.0.1 -p 8080 start
And would then listen on port 8080. Rails server can be started similarly using:
rails server -e production -p 4000
You may want to also consider starting your database using a port different than the standard one, but that is probably not necessary.

Rails webrick production port number

How can I permanently change my application port number in production?
I know I can specify the port number when starting the server rails s -p 3005, but was wondering how to change it so that I dont have to specifiy the port number everytime I start up the app.
Don't use webrick in production.
It is only optimized for development purposes. Use something like thin, unicorn or passenger for example. You can find capistrano recipes to start or restart web servers when deploying the app.
This seems to answer your question even though the question references running in development. I tend to agree with modifying the config/boot.rb as per.
But, iltempo is correct that you should be using a different stack in production. I've had good luck with Passenger.

Running more than Rails apps on a local Mac

I would like to set up multiple Rails apps running simultaneously.
I'm using a basic installation with Rails and Mongrel. I'm pretty newbie when it comes to a server side thing.
What kind of options do I have to run multiple Rails app running at the same time on a local Mac?
The only thing that stops you from running multiple rails apps on one machine is the ports.
If you want to run several apps while developing just use script/server -p <port number> for each of the apps.
If you have a production machine set up, I would recommend you to use phusion passenger with apache or nginx, and set up different virtual machines (or ports)
If you end up using Phusion Passenger, the Passenger Preference Pane can automatically configure your Apache virtual hosts for you. It's a lot easier than editing the Apache configuration and your /etc/hosts file each time you want to set up a new application.
Generally you start rails server using webrick or mongrel like
script/server
and
mongrel_rails start
respectively, that starts your server on port 3000 by default, ie. you can access your app on localhost:3000
To have multiple rails apps running on same machine, just start the servers by going to different rails application root directories on different ports like so
script/server -p 3001
or
mongrel_rails start -p 3001
Just for info, if you want to start rails apps in different environments then just pass -e option when you start the server like so
script/server -e production
or
script/server -e test_or_anyotherenv
If you don't give -e option, then it will by default start the server in development environment.
I'm a Django (not Rails) coder, but I think you should launch the servers at different ports.
Initially, I used mongrel on different ports. Works just fine. But, as agregoire mentioned, Phusion Passenger and the Passenger PrefPane make your life so much easier. Checkout Ryan Bates's RailsCast, Passenger in Development, for a good tutorial on setting it up.

Resources