首个教程的目的在于尽量快地让一个Phoenix应用构建好并运行起来.node
在咱们开始前,请花一分钟阅读Installation Guide.安装好了全部必要的依赖,咱们才能让应用流畅地构建和运行.web
因此,咱们须要安装Elixir, Erlang, Hex,和Phoenix.还须要安装好PostgreSQL和node.js用于构建一个默认应用.数据库
Ok, 咱们已经准备好了!npm
咱们能够在任何目录中运行mix phoenix.new
,来新建咱们的Phoenix应用.Phoenix可接受目录的相对路径或绝对路径.假设咱们的应用名为hello_phoenix
,下列方法均可运做.浏览器
$ mix phoenix.new /Users/me/work/elixir-stuff/hello_phoenix
$ mix phoenix.new hello_phoenix
在开始前,咱们要注意Brunch.io: Phoenix会默认使用Brunch.io进行资源管理.Brunch.io的依赖是经过node包管理工具安装的,而不是mix. Phoenix会在
mix phoenix.new
的末尾提示安装它们.若是咱们选择no
, 而以后没有经过npm install
安装那些依赖,那么当咱们试图启动应用时就会抛出错误,并且咱们的资源可能没有合适地被载入。若是咱们不想使用Brunch.io,咱们能够简单地在mix phoenix.new
以后加上--no-brunch
。服务器
如今,让咱们运行mix phoenix.new
,使用相对路径。app
mix phoenix.new hello_phoenix * creating hello_phoenix/config/config.exs * creating hello_phoenix/config/dev.exs * creating hello_phoenix/config/prod.exs ... * creating hello_phoenix/web/views/layout_view.ex * creating hello_phoenix/web/views/page_view.ex Fetch and install dependencies? [Yn]
Phoenix生成了目录结构,和咱们的应用所需的全部文件。当完成后,它会询问咱们是否须要安装依赖。让咱们选择yes。框架
Fetch and install dependencies? [Yn] Y * running mix deps.get * running npm install && node node_modules/brunch/bin/brunch build We are all set! Run your Phoenix application: $ cd hello_phoenix $ mix phoenix.server You can also run your app inside IEx (Interactive Elixir) as: $ iex -S mix phoenix.server Before moving on, configure your database in config/dev.exs and run: $ mix ecto.create
当依赖安装好后,它会提示咱们进入项目文件夹,并启动咱们的应用。ide
Phoenix假设咱们的PostgreSQL数据库中有一个postgres
用户帐号,它有正确的权限且密码是postgres
。若是状况不符合,请查阅mix任务ecto.create 。工具
Ok,让咱们来试一下。首先,咱们会cd
到刚建立的hello_phoenix/
目录:
$ cd hello_phoenix
如今,咱们将建立咱们的数据库:
$ mix ecto.create The database for HelloPhoenix.Repo has been created.
注意:若是你是第一次运行这个命令,Phoenix可能会请你安装Rebar。请完成安装,Rebar是用于构建Erlang包的。
最后,咱们将启动Phoenix服务器:
$ mix phoenix.server [info] Running HelloPhoenix.Endpoint with Cowboy using http on port 4000 23 Nov 05:25:14 - info: compiled 5 files into 2 files, copied 3 in 1724ms
若是咱们选择了不让Phoenix在生成新应用时安装依赖,那么phoenix.new
任务会在咱们想要安装它们时,提示咱们执行必要的步骤。
Fetch and install dependencies? [Yn] n We are all set! Run your Phoenix application: $ cd hello_phoenix $ mix deps.get $ mix phoenix.server You can also run your app inside IEx (Interactive Elixir) as: $ iex -S mix phoenix.server Before moving on, configure your database in config/dev.exs and run: $ mix ecto.create Phoenix uses an optional assets build tool called brunch.io that requires node.js and npm. Installation instructions for node.js, which includes npm, can be found at http://nodejs.org. After npm is installed, install your brunch dependencies by running inside your app: $ npm install If you don't want brunch.io, you can re-run this generator with the --no-brunch option.
Phoenix默认在4000端口接收请求。若是咱们在浏览器中输入http://localhost:4000 ,咱们将看到Phoenix框架的欢迎页面。
若是你的屏幕看上去和图中同样,那么恭喜你!你如今有了一个正在运做的Phoenix应用。若是你看不到上面的页面,试着访问http://127.0.0.1:4000 并确认你的系统将“localhost”设定为“127.0.0.1”。
本地,咱们的应用运行于一个iex
会话中。咱们要按两次ctrl-c
来中止它,就像中止普通的iex
那样。
下一步 咱们将稍稍自定义咱们的应用,了解一下一个Phoenix应用是如何拼接起来的。