Distillery and EDeliver on Custom Phoenix Port

I want to run 2 phoenix applications on the same server. The first one is working great on the default 4000 port. I want the second app to run on port 4001. I thought just setting up a custom env var would work but have had no success. I can manually start the 2nd app by passing in the port, but if I try to use edeliver it always uses the default port of 4000.

config/prod.exs

url: [host: “api.mydomainname.com”, port: {:system, “PORTAPI”}],

I’ve set the PORTAPI to 4001 locally, on my build server, and on the app server too. I also tried adding it to vm.args:

rel/vm.args

-my_appname_api port ${PORTAPI}

I’ve run out of things I can think of to try.

1 Like

is REPLACE_OS_VARS=true when you start the application?

yes

deploy/.profile

export PORT=4000
export PORTAPI=4001
export REPLACE_OS_VARS=true

Do you also have

http: [port: {:system, "PORTAPI"}]

set?

1 Like

url: [host: “api.mydomainname.com”, port: {:system, “PORTAPI”}]

Oh, you’re using distillery, it should be:
url: [host: "api.mydomainname.com", port: ${PORTAPI}]

the way you wrote is used on exrm.

edit: I’m not really sure if the ${PORTAPI} should be a string "${PORTAPI}" for numbers too…

1 Like

@cevado its building now with the “${PORTAPI}” tweak…fingers crossed :slight_smile:

2 Likes

{:system, "PORT"} is a Phoenix thing (and a convention used throughout other libraries).

Edit: To be clear for future readers, there’s no reason why {:system, "PORT"} shouldn’t work with Distillery.

1 Like

@net I don’t have that set…so should that be just another line in prod.exs like so?

config/prod.exs

url: [host: “api.mydomainname.com”, port: “${PORTAPI}”],
http: [port: {:system, “PORTAPI”}]

OR since I’m using Distillery

http: [port: “${PORTAPI}”],

Yes.

It should be part of the config generated by Phoenix, though. So make sure it’s not already there.

Oh, I forgot. Phoenix 1.3 moved it to :on_init. Go to lib/web/endpoint.ex and edit the load_from_system_env/1 function at the bottom to

def load_from_system_env(config) do
  port = System.get_env("PORTAPI") || raise "expected the PORTAPI environment variable to be set"
  {:ok, Keyword.put(config, :http, [:inet6, port: port])}
end

instead of adding to config/prod.exs.

@net ok thx for that…we are using rel candidate. Let me see if that’s the missing piece…this is driving me nuts :slight_smile:

It’s working!

@cevado and @net you guys rock. First off for taking the time to help me out, and being patient with me. I’m going to write a blog post eventually about all of this deployment stuff to give back to the community. It has taken me a ton of time to figure it out being a n00b. I’ve now got working ansible playbooks that setup both build and production servers. And also have circleci integrated for CI.

5 Likes

To be clear for future readers, there’s no reason why {:system, “PORT”} shouldn’t work with Distillery.

For sure, I totally get it wrong… I was reading about it to be sure… the change from exrm and distillery was only RELX_REPLACE_OS_VARS that started to be used as a REPLACE_OS_VARS.

exrm used relx and this "${env_var}" pattern is a relx thing.
distillery stoped using relx and kept the old way but now enforces the tuple pattern created by phoenix(using exrm {:system, "env_var"} would only work with phoenix configurations).

@net thanks for correcting it… my bad :disappointed_relieved:

2 Likes