Start Phoenix app with cowboy server on different port
Asked Answered
T

3

31

Is it possible to start locally a few Phoenix apps on different ports from the console using some command like mix phoenix.server --port=4001? This one does not work, of course, but, maybe, there is similar way.

Turgescent answered 29/5, 2015 at 23:18 Comment(0)
P
47

Yep! Make sure you set the mix config to reference the env port, i.e.

config :my_app, MyApp.Endpoint,
  http: [port: {:system, "PORT"}],

Then from the terminal:

$ PORT=4001 mix phoenix.server
$ PORT=4002 mix phoenix.server
$ PORT=4003 mix phoenix.server
Peephole answered 30/5, 2015 at 4:29 Comment(2)
Note: Doing this in config.exs does not work, the reason I guess is the dev.exs overrides the config. Hence put this in dev.exs. For more info see this issue on phoenix github.com/phoenixframework/phoenix/issues/962Dickman
Note 2: You shouldn't add the lines to your config files but edit the existing lines. I got a cryptic error by just adding the lines for my app.Unreflective
S
41

Edit your config/dev.exs and change the Endpoint http port like the following:

config :my_app, MyApp.Endpoint,
  http: [port: System.get_env("PORT") || 4000],

This allows the port to be set, or left as the default 4000:

PORT=4002 mix phoenix.server # to run on port 4002
mix phoenix.server # to run on port 4000

This answer was described by @chris-mccord on github.

Sagerman answered 19/6, 2016 at 22:59 Comment(1)
This worked. The command to start Phoenix is now mix phx.serverHerringbone
M
2

This was needed for me as a solution since my issue was that I needed to let C9.io dictate the port, for me, adding this code to the dev.exs file solved the problem:

config :my_app, MyApp.Endpoint,
  http: [port: {:system, "PORT"}],

and then in the Terminal, I just needed to run the server as normal:

mix phoenix.server
Muticous answered 1/4, 2016 at 15:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.