Phoenix: Default (4000) port busy

Created on 13 Jun 2015  路  22Comments  路  Source: phoenixframework/phoenix

I am using OS X and I have tried (many time with computer restarts) to run phoenix server on port 4000 and each time I receive information:

[error] Running PhoenixJobs.Endpoint with Cowboy on port 4000 (http) failed, port already in use

=INFO REPORT==== 13-Jun-2015::00:35:13 ===
    application: logger
    exited: stopped
    type: temporary
** (Mix) Could not start application phoenix_jobs: PhoenixJobs.start(:normal, []) returned an error: shutdown: failed to start child: PhoenixJobs.Endpoint
    ** (EXIT) shutdown: failed to start child: Phoenix.Endpoint.Server
        ** (EXIT) shutdown: failed to start child: :http
            ** (EXIT) shutdown: failed to start child: :ranch_acceptors_sup
                ** (EXIT) an exception was raised:
                    ** (MatchError) no match of right hand side value: {:error, :eaddrinuse}
                        (ranch) src/ranch_acceptors_sup.erl:30: :ranch_acceptors_sup.init/1
                        (stdlib) supervisor.erl:243: :supervisor.init/1
                        (stdlib) gen_server.erl:306: :gen_server.init_it/6
                        (stdlib) proc_lib.erl:237: :proc_lib.init_p_do_apply/3

Port 4000 isn't busy because in other time I can use this port to run other apps (in different language).

I am using the latest phoenix framework version and Elixir 1.0.4.

Maybe you can add support to OPTIONAL port numer in config (for development environment). When I changed config and pass port 4001, application have started without problems.

Most helpful comment

May be helpful for people finding this later.

Running sudo lsof -i :4000 will give you the list of processes using the default port which you can further inspect in Activity Monitor.

In my particular case it was a NoMachine (nx) service running there.

All 22 comments

As a sanity check, can you change the port in dev.exs to something else, say 4004 and see if it works?

Of course I can and I did. But I think it will be better to pass port number in terminal. I think this way is more obvious for new people outside elixir ecosystem. If you agree thats nice, if you don't agree thats ok.

So something must be taking up port 4000 on your system. To achieve what you want, you can simply default to System.get_env("PORT") in dev.exs:

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

The you can dynamically set the port with:

$ PORT=4004 mix phoenix.server

thanks a lot

The you can dynamically set the port with:
$ PORT=4004 mix phoenix.server

How do you give this command in Windows?

I think it is:

set /a "PORT=4004" && mix phoenix.server

Thanks Jose.

Tried all the options above and changed the port number in dev file. But the terminal is getting hung while executing mix phoenix.server

mix phoenix.server
Compiling 12 files (.ex)
Generated app app
[info] Running App.Endpoint with Cowboy using http://localhost:4004
24 Aug 17:38:00 - info: compiled 6 files into 2 files, copied 3 in 1 sec

The below is my mix.exs file.

defmodule App.Mixfile do
use Mix.Project

def project do
[app: :app,
version: "0.0.1",
elixir: "~> 1.2",
elixirc_paths: elixirc_paths(Mix.env),
compilers: [:phoenix, :gettext] ++ Mix.compilers,
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
aliases: aliases(),
deps: deps()]
end

# Configuration for the OTP application.
#
# Type mix help compile.app for more information.
def application do
[mod: {App, []},
applications: [:phoenix, :phoenix_pubsub, :phoenix_html, :cowboy, :logger, :gettext,
:phoenix_ecto, :postgrex]]
end

# Specifies which paths to compile per environment.
defp elixirc_paths(:test), do: ["lib", "web", "test/support"]
defp elixirc_paths(_), do: ["lib", "web"]

# Specifies your project dependencies.
#
# Type mix help deps for examples and options.
defp deps do
[{:phoenix, "~> 1.2.1"},
{:phoenix_pubsub, "~> 1.0"},
{:phoenix_ecto, "~> 3.0"},
{:postgrex, ">= 0.0.0"},
{:phoenix_html, "~> 2.6"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:gettext, "~> 0.11"},
{:cowboy, "~> 1.0"}]
end

@Radhika-Adoni everything looks fine. Is the app accessible at the port you are using? If you were wanting an iex prompt, you need to run with iex -S mix phoenix.server

Used this command..and the issue got resolved :)

MIX_ENV=prod PORT=80 iex -S mix phoenix.server..used port 80..

Its one week into Elixir and my first Functional Programming language and I am loving it..

Hello Chris

The command is working for prod environment but for dev mix phoenix.server is still getting hung.

You can use something like lsof -i :4000 to find out which process is listening on this port and then try to find more information about it. See this article for examples: http://www.cyberciti.biz/faq/what-process-has-open-linux-port/ (there is a section about lsof at the end).

or you can just do killall beam.smp

or you can just do killall beam.smp

That only works if it is a beam program that has port 4000, in addition it will kill all other beam programs as well, and it only works on *nix-like OS's (and even then not all as technically killall is nonstandard).

I am missing something probably but I cannot make it work.
In dev.exs:
http: [port: System.get_env("PORT") || 8081]

Then tries: $ PORT=5688 mix phoenix.server and Cowboy starts on port 4000.

Running: PORT=8085 iex -S mix phoenix.server Cowboy still starts on port 4000 but
iex(2)> System.get_env "PORT"
"8085"

How can I define default port to 8081 while make it still possible to configure it from command line?
Thanks.

You most likely did not configure the http: [port: System.get_env("PORT") || 8081] for the dev env. Are you sure you made those changes in config/dev.exs ?

I am sure and I think I have found the answer.
Just to double check: what will happen when I would call http: [] twice?
The second call will override the first one, right?

yes

To be clear, the 2nd one, in a separate mix config would be merged with the prior one

May be helpful for people finding this later.

Running sudo lsof -i :4000 will give you the list of processes using the default port which you can further inspect in Activity Monitor.

In my particular case it was a NoMachine (nx) service running there.

Was this page helpful?
0 / 5 - 0 ratings