Elixir 的 Phoenix 框架对于开发 Web 应用很是方便,不只有 RoR 的便利,还有 Erlang 的性能和高并发优点。 可是应用的发布涉及到 Erlang 和 Elixir 环境,部署不是那么方便,特别是不少 package 须要访问国外的服务器。html
所以,若是能像 golang 那样,把整个应用打包成一个可执行的二进制,部署时会方便不少。 打包后不只包含应用引用的 packages,也包含 erlang 的运行环境。前端
使用 distillery 就能够完成需求。 注 这里打包的是 API 服务,也就是不包含前端的部分。git
distillery 提供丰富了 API,除了打包,还有升级/降级,代码热替换等功能,这里咱们只介绍打包的功能。github
$ mix phx.new hello --no-brunch --no-ecto
只是实验 phoenix 工程的打包功能,因此这里不安装前端的依赖,也不安装数据库相关依赖。golang
建立一个简单的 api lib/hello_web/router.exweb
scope "/api", HelloWeb do pipe_through(:api) get("/", PageController, :api) end
lib/hello_web/controllers/page_controller.ex数据库
def api(conn, _params) do json(conn, %{result: "success"}) end
mix.exs 中的 deps 中添加:json
defp deps do [ ... {:distillery, "~> 1.5", runtime: false} ] end
而后在 hello 工程目录下执行:api
mix deps.get
执行成功的话,在命令行界面上能够看到安装了 distillery 依赖。bash
首先,生成配置文件
mix release.init
这个命令生成的 rel/config.exs 没有什么要修改的。
修改 config/prod.exs
config :hello, HelloWeb.Endpoint, server: true, http: [port: 4001], url: [host: "localhost", port: 4001]
这里写死了 port,也能够改为从环境变量中读取。
MIX_ENV=prod mix release
编译成功后,在 _build/prod/rel/hello/releases/
将生成的 hello.tar.gz 放到其余机器也能够直接运行,不用安装 erlang 和 elixir 环境。
cd /home mkdir hello tar zxvf hello.tar.gz -C hello cd hello ./bin/hello foreground
distillery 的功能远不止此,更多的功能能够参考:https://hexdocs.pm/distillery/getting-started.html