首個教程的目的在於儘量快地讓一個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應用是如何拼接起來的。