この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
PhoenixはElixir用のWebアプリケーションフレームワークです。
公式ホームページで書かれているように生産性、信頼性、速度に強みを持つフレームワークです。
(Productive |> Reliable |> Fast)
Programing Phoenixで書かれているベンチマークでは以下の数値となっています。
以下サイトに検証環境の詳細情報が書いてあります。
phoenix showdown rackspace onmetal io.md
Framework | Throughput (req/s) | Latency (ms) | Consistency (σ ms) |
---|---|---|---|
Phoenix 0.13.1 | 179685.94 | 0.61 | 1.04 |
Play | 171236.03 | 1.89 | 14.17 |
Express Cluster | 92064.94 | 1.24 | 1.07 |
Sinatra | 30561.95 | 3.50 | 2.53 |
Rails | 11903.48 | 8.50 | 4.07 |
めちゃくちゃ早いですね!現在のバージョンとは異なりますので現在の数値とは若干異なるとは思いますが、
それを差し引いても素晴らしい性能です。
今回はこのPhoenixアプリケーションの環境構築とアプリケーションの起動までを試してみます。
(macOS限定の説明となります、ご了承ください)。
事前準備
Phoenixアプリケーションを開発するためのソフトウェアをインストールしていきます。
Elixirのインストール
前回書いた、以下の記事を参照にインストールしてください
MacにElixir環境を構築する
PostgresSQLのインストール
Phoenixは初期設定でPostgreSQLを使用します。Homebrewでインストールしてください。
インストール
$ brew install postgresql
起動、終了
インストールできたらpostgresqlを起動しましょう。
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log
停止は以下のコマンドです
pg_ctl -D /usr/local/var/postgres stop
環境変数PG_DATAを設定すれば起動オプションの-Dが不要になるので設定します。
zshrcに以下の内容を追加してください(使用しているシェルに合わせて書き込むファイルを変えてください)。
export PGDATA=/usr/local/var/postgres
これで起動、停止は以下コマンドで大丈夫です
pg_ctl -l /user/local/var/postgres/server.log start
pg_ctl stop
ユーザー作成
Phoenixアプリケーションの初期設定ではpostgres
ユーザーを使用するので、このユーザーを作成しておきます(passwordもpostgresです)
$ createuser -P -d postgres
Node.jsのインストール
postgresqlの話が長くなってしまいました。次はnode.jsをインストールします。
phoenixはアセットファイルのコンパイルにnode.jsのツールを使用します。
brew install node
Phoenixのインストール、プロジェクト作成
目的のPhoenixフレームワークを以下のコマンドでインストールします。
mix archive.install https://github.com/phoenixframework/archives/raw/master/phoenix_new.ez * creating ~/.mix/archives/phoenix_new.ez
インストールできたら、以下のコマンドでプロジェクトを作成します
$ mix phoenix.new hello
* creating hello/config/config.exs
* creating hello/config/dev.exs
* creating hello/config/prod.exs
* creating hello/config/prod.secret.exs
* creating hello/config/test.exs
* creating hello/lib/hello.ex
* creating hello/lib/hello/endpoint.ex
* creating hello/test/views/error_view_test.exs
* creating hello/test/support/conn_case.ex
* creating hello/test/support/channel_case.ex
* creating hello/test/test_helper.exs
* creating hello/web/channels/user_socket.ex
* creating hello/web/router.ex
* creating hello/web/views/error_view.ex
* creating hello/web/web.ex
* creating hello/mix.exs
* creating hello/README.md
* creating hello/web/gettext.ex
* creating hello/priv/gettext/errors.pot
* creating hello/priv/gettext/en/LC_MESSAGES/errors.po
* creating hello/web/views/error_helpers.ex
* creating hello/lib/hello/repo.ex
* creating hello/test/support/model_case.ex
* creating hello/priv/repo/seeds.exs
* creating hello/.gitignore
* creating hello/brunch-config.js
* creating hello/package.json
* creating hello/web/static/css/app.css
* creating hello/web/static/js/app.js
* creating hello/web/static/js/socket.js
* creating hello/web/static/assets/robots.txt
* creating hello/web/static/assets/images/phoenix.png
* creating hello/web/static/assets/favicon.ico
* creating hello/test/controllers/page_controller_test.exs
* creating hello/test/views/layout_view_test.exs
* creating hello/test/views/page_view_test.exs
* creating hello/web/controllers/page_controller.ex
* creating hello/web/templates/layout/app.html.eex
* creating hello/web/templates/page/index.html.eex
* creating hello/web/views/layout_view.ex
* creating hello/web/views/page_view.ex
(途中で依存関係のあるライブラリをインストールするか聞かれますのでy
を入力してインストールします)
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
$ 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
次にプロジェクトのディレクトリに移動してDBを作成します
$ cd hello
$ mix ecto.create
==> connection
Compiled lib/connection.ex
Generated connection app
==> fs (compile)
(中略)
The database for Hello.Repo has been created.
終わりましたら、Phoenixアプリケーションを起動してみましょう!
mix phoenix.server
[info] Running Hello.Endpoint with Cowboy using http on port 4000
16 Jun 18:34:11 - info: compiled 5 files into 2 files, copied 3 in 826ms
ブラウザでアクセスすると、Phoenixアプリケーションの画面が表示されます。
まとめ
今回はPhoenixの導入までを試してみました。
ここまでの作業でもRubyonRailsになんとなく似ているのが分かります。
PhoenixとRubyOnRailsを比較すると以下のようになります(ほとんどElixirとRubyの比較になってしまいますが)。
Phoenix | Rails | 役割 |
---|---|---|
mix | bundler | パッケージマネージャー |
mix.exs | Gemfile | ライブラリ記述ファイル |
postgresql | sqlite | 初期DB |
ecto | ActiveRecord | O/Rマッパー |
hex | RubyGems | ライブラリ管理サーバ |
次回はPhoenixのCRUDアプリケーションを作成してみます。