Phoenix app can't start in production because a Plug.Cowboy error

When i try to start my app with MIX_ENV=prod mix.phx.server i got this error:

19:10:27.652 [info] Application rm_api exited: RmApi.Application.start(:normal, []) returned an error: shutdown: failed to start child: RmApiWeb.Endpoint
    ** (EXIT) an exception was raised:
        ** (ArgumentError) expected a keyword list as the second argument, got: [{:dispatch, [_: [{:_, Phoenix.Endpoint.Cowboy2Handler, {RmApiWeb.Endpoint, []}}]]}, {:port, 4000}, {:otp_app, :rm_api}, :inet6]
            (elixir) lib/keyword.ex:686: Keyword.merge/2
            (plug_cowboy) lib/plug/cowboy.ex:165: Plug.Cowboy.child_spec/1
            (phoenix) lib/phoenix/endpoint/cowboy2_adapter.ex:44: Phoenix.Endpoint.Cowboy2Adapter.child_spec/3
            (phoenix) lib/phoenix/endpoint/supervisor.ex:108: anonymous fn/6 in Phoenix.Endpoint.Supervisor.server_children/4
            (elixir) lib/enum.ex:1899: Enum."-reduce/3-lists^foldl/2-0-"/3
            (phoenix) lib/phoenix/endpoint/supervisor.ex:99: Phoenix.Endpoint.Supervisor.server_children/4
            (phoenix) lib/phoenix/endpoint/supervisor.ex:59: Phoenix.Endpoint.Supervisor.init/1
            (stdlib) supervisor.erl:295: :supervisor.init/1
** (Mix) Could not start application rm_api: RmApi.Application.start(:normal, []) returned an error: shutdown: failed to start child: RmApiWeb.Endpoint
    ** (EXIT) an exception was raised:
        ** (ArgumentError) expected a keyword list as the second argument, got: [{:dispatch, [_: [{:_, Phoenix.Endpoint.Cowboy2Handler, {RmApiWeb.Endpoint, []}}]]}, {:port, 4000}, {:otp_app, :rm_api}, :inet6]
            (elixir) lib/keyword.ex:686: Keyword.merge/2
            (plug_cowboy) lib/plug/cowboy.ex:165: Plug.Cowboy.child_spec/1
            (phoenix) lib/phoenix/endpoint/cowboy2_adapter.ex:44: Phoenix.Endpoint.Cowboy2Adapter.child_spec/3
            (phoenix) lib/phoenix/endpoint/supervisor.ex:108: anonymous fn/6 in Phoenix.Endpoint.Supervisor.server_children/4
            (elixir) lib/enum.ex:1899: Enum."-reduce/3-lists^foldl/2-0-"/3
            (phoenix) lib/phoenix/endpoint/supervisor.ex:99: Phoenix.Endpoint.Supervisor.server_children/4
            (phoenix) lib/phoenix/endpoint/supervisor.ex:59: Phoenix.Endpoint.Supervisor.init/1
            (stdlib) supervisor.erl:295: :supervisor.init/1

In dev mode it is ok

this is my deps:

  defp deps do
    [   
      {:phoenix, "~> 1.4.11"},
      {:phoenix_pubsub, "~> 1.1"},
      {:phoenix_ecto, "~> 4.0"},
      {:ecto_sql, "~> 3.1"},
      {:postgrex, ">= 0.0.0"},
      {:gettext, "~> 0.11"},
      {:jason, "~> 1.0"},
      {:plug_cowboy, "~> 2.1"}
    ]   
  end 

And my application Endpoint:

defmodule RmApiWeb.Endpoint do
  use Phoenix.Endpoint, otp_app: :rm_api

  socket "/socket", RmApiWeb.UserSocket, 
    websocket: true,
    longpoll: false            

  # Serve at "/" the static files from "priv/static" directory.
  # 
  # You should set gzip to true if you are running phx.digest
  # when deploying your static files in production.
  plug Plug.Static,            
    at: "/",                   
    from: :rm_api,
    gzip: false,
    only: ~w(css fonts images js favicon.ico robots.txt)
       
  # Code reloading can be explicitly enabled under the
  # :code_reloader configuration of your endpoint.
  if code_reloading? do        
    plug Phoenix.CodeReloader  
  end
     
  plug Plug.RequestId
  plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]

  plug Plug.Parsers,
    parsers: [:urlencoded, :multipart, :json],
    pass: ["*/*"],
    json_decoder: Phoenix.json_library()

  plug Plug.MethodOverride
  plug Plug.Head

  # The session will be stored in the cookie and signed,
  # this means its contents can be read but not tampered with.
  # Set :encryption_salt if you would also like to encrypt it.
  plug Plug.Session,
    store: :cookie,
    key: "_rm_api_key",
    signing_salt: "rdRgm8Fd"

  plug RmApiWeb.Router
end

I created the app with phx.new my_app --no-html --no-webpack

is your prod.exs config correct?

It is the same that comes with phx.new generator

use Mix.Config

# For production, don't forget to configure the url host
# to something meaningful, Phoenix uses this information
# when generating URLs.
#
# Note we also include the path to a cache manifest
# containing the digested version of static files. This
# manifest is generated by the `mix phx.digest` task,
# which you should run after static files are built and
# before starting your production server.
config :example, ExampleWeb.Endpoint,
  url: [host: "example.com", port: 80],
  cache_static_manifest: "priv/static/cache_manifest.json"

# Do not print debug messages in production
config :logger, level: :info

# ## SSL Support
#
# To get SSL working, you will need to add the `https` key
# to the previous section and set your `:url` port to 443:
#
#     config :example, ExampleWeb.Endpoint,
#       ...
#       url: [host: "example.com", port: 443],
#       https: [
#         :inet6,
#         port: 443,
#         cipher_suite: :strong,
#         keyfile: System.get_env("SOME_APP_SSL_KEY_PATH"),
#         certfile: System.get_env("SOME_APP_SSL_CERT_PATH")
#       ]
#
# The `cipher_suite` is set to `:strong` to support only the
# latest and more secure SSL ciphers. This means old browsers
# and clients may not be supported. You can set it to
# `:compatible` for wider support.
#
# `:keyfile` and `:certfile` expect an absolute path to the key
# and cert in disk or a relative path inside priv, for example
# "priv/ssl/server.key". For all supported SSL configuration
# options, see https://hexdocs.pm/plug/Plug.SSL.html#configure/1
#
# We also recommend setting `force_ssl` in your endpoint, ensuring
# no data is ever sent via http, always redirecting to https:
#
#     config :example, ExampleWeb.Endpoint,
#       force_ssl: [hsts: true]
#
# Check `Plug.SSL` for all available options in `force_ssl`.

# Finally import the config/prod.secret.exs which loads secrets
# and configuration from environment variables.
import_config "prod.secret.exs"

I was in elixir 1.6.6 and phoenix 1.4.11

What i did was reinstall elixir in the last version

Now it works

2 Likes