1. 安装Elixir/Phoenix

1.1 Exlixir 安装

  1. $ brew install elixir
  2. $ elixir -v
  3. Erlang/OTP 23 [erts-11.1.7] [source] [64-bit] [smp:6:6] [ds:6:6:10] [async-threads:1] [hipe] [dtrace]
  4. Elixir 1.11.3 (compiled with Erlang/OTP 23)

之后安装Elixir的包管理工具hex

  1. $ mix local.hex

1.2 Phoenix安装

  1. $ mix archive.install hex phx_new
  2. $ mix phx.new -v
  3. Phoenix v1.5.8

此外,Phoenix还需安装node,数据库等。

2. 测试工程

新建一个hello工程用于测试安装:

  1. $ mix phx.new hello
  2. * creating hello/config/config.exs
  3. * creating hello/config/dev.exs
  4. * creating hello/config/prod.exs
  5. * creating hello/config/prod.secret.exs
  6. * creating hello/config/test.exs
  7. * creating hello/lib/hello/application.ex
  8. * creating hello/lib/hello.ex
  9. * creating hello/lib/hello_web/channels/user_socket.ex
  10. * creating hello/lib/hello_web/views/error_helpers.ex
  11. * creating hello/lib/hello_web/views/error_view.ex
  12. * creating hello/lib/hello_web/endpoint.ex
  13. * creating hello/lib/hello_web/router.ex
  14. * creating hello/lib/hello_web/telemetry.ex
  15. * creating hello/lib/hello_web.ex
  16. * creating hello/mix.exs
  17. * creating hello/README.md
  18. * creating hello/.formatter.exs
  19. * creating hello/.gitignore
  20. * creating hello/test/support/channel_case.ex
  21. * creating hello/test/support/conn_case.ex
  22. * creating hello/test/test_helper.exs
  23. * creating hello/test/hello_web/views/error_view_test.exs
  24. * creating hello/lib/hello/repo.ex
  25. * creating hello/priv/repo/migrations/.formatter.exs
  26. * creating hello/priv/repo/seeds.exs
  27. * creating hello/test/support/data_case.ex
  28. * creating hello/lib/hello_web/controllers/page_controller.ex
  29. * creating hello/lib/hello_web/templates/layout/app.html.eex
  30. * creating hello/lib/hello_web/templates/page/index.html.eex
  31. * creating hello/lib/hello_web/views/layout_view.ex
  32. * creating hello/lib/hello_web/views/page_view.ex
  33. * creating hello/test/hello_web/controllers/page_controller_test.exs
  34. * creating hello/test/hello_web/views/layout_view_test.exs
  35. * creating hello/test/hello_web/views/page_view_test.exs
  36. * creating hello/lib/hello_web/gettext.ex
  37. * creating hello/priv/gettext/en/LC_MESSAGES/errors.po
  38. * creating hello/priv/gettext/errors.pot
  39. * creating hello/assets/webpack.config.js
  40. * creating hello/assets/.babelrc
  41. * creating hello/assets/js/app.js
  42. * creating hello/assets/css/app.scss
  43. * creating hello/assets/js/socket.js
  44. * creating hello/assets/package.json
  45. * creating hello/assets/static/favicon.ico
  46. * creating hello/assets/css/phoenix.css
  47. * creating hello/assets/static/images/phoenix.png
  48. * creating hello/assets/static/robots.txt
  49. Fetch and install dependencies? [Yn] y
  50. * running mix deps.get
  51. * running mix deps.compile
  52. * running cd assets && npm install && node node_modules/webpack/bin/webpack.js --mode development
  53. We are almost there! The following steps are missing:
  54. $ cd hello
  55. Then configure your database in config/dev.exs and run:
  56. $ mix ecto.create
  57. Start your Phoenix app with:
  58. $ mix phx.server
  59. You can also run your app inside IEx (Interactive Elixir) as:
  60. $ iex -S mix phx.server

运行工程,访问http://localhost:4000

  1. $ mix phx.server

image.png

3. mix命令帮助

mix命令格式: mix task.subtask args

运行mix task 显示task 相关帮助,运行mix task.subtask显示subtask相关帮助

  1. $ mix phx
  2. Phoenix v1.5.8
  3. Productive. Reliable. Fast.
  4. A productive web framework that does not compromise speed or maintainability.
  5. Available tasks:
  6. mix phx.digest # Digests and compresses static files
  7. mix phx.digest.clean # Removes old versions of static assets.
  8. mix phx.gen.cert # Generates a self-signed certificate for HTTPS testing
  9. mix phx.gen.channel # Generates a Phoenix channel
  10. mix phx.gen.context # Generates a context with functions around an Ecto schema
  11. mix phx.gen.embedded # Generates an embedded Ecto schema file
  12. mix phx.gen.html # Generates controller, views, and context for an HTML resource
  13. mix phx.gen.json # Generates controller, views, and context for a JSON resource
  14. mix phx.gen.live # Generates LiveView, templates, and context for a resource
  15. mix phx.gen.presence # Generates a Presence tracker
  16. mix phx.gen.schema # Generates an Ecto schema and migration file
  17. mix phx.gen.secret # Generates a secret
  18. mix phx.new # Creates a new Phoenix v1.5.8 application
  19. mix phx.new.ecto # Creates a new Ecto project within an umbrella project
  20. mix phx.new.web # Creates a new Phoenix web project within an umbrella project
  21. mix phx.routes # Prints all routes
  22. mix phx.server # Starts applications and their servers

4. 参考链接

https://elixirschool.com/blog/instrumenting-phoenix-with-live-dashboard/