Sometimes when developing a backend, you want to use HTTPS even on localhost. I ran into this situation not long ago when I was building a mock service providing OAuth 2.0 authentication.
Here’s how you can enable SSL on Elixir Phoenix in dev mode.
For development purposes, you can generate a self-signed certificate with the following commands:
# generate key
$ openssl genrsa -out localhost.key 2048
# generate cert
$ openssl req -new -x509 -key localhost.key -out localhost.cert -days 3650 -subj /CN=localhost
You can of course also use Letsencrypt for a free signed certificate.
Put them in a directory called priv/keys/
.
In your config/dev.exs
file:
config :phoenix_oauth2_mock_server, PhoenixOauth2MockServer.Endpoint,
http: [port: 4000],
https: [port: 4443,
otp_app: :phoenix_oauth2_mock_server,
keyfile: "priv/keys/localhost.key",
certfile: "priv/keys/localhost.cert"],
debug_errors: true,
code_reloader: true,
check_origin: false,
watchers: [node: ["node_modules/brunch/bin/brunch", "watch", "--stdin"]]
Done!