Elixir
をインストールした流れで、Phoenix
もインストールしてみる。
Hexのインストール
Elixir
のパッケージマネージャであるHex
をインストール
$ mix local.hex
Are you sure you want to install "https://repo.hex.pm/installs/1.4.0/hex-0.16.0.ez"? [Yn] Y
* creating /home/app/.mix/archives/hex-0.16.0
Node.jsのインストール
node.js が存在しないため、yum install
# yum install nodejs
====================================
Package Arch Version Repository Size
====================================
Installing:
nodejs x86_64 0.10.48-3.el6 epel 2.1 M
Installing for dependencies:
libuv x86_64 1:0.10.34-1.el6 epel 57 k
Transaction Summary
====================================
Install 2 Package(s)
Total download size: 2.2 M
Installed size: 7.2 M
Is this ok [y/N]: N
しかし、普通にインストールされるnodejs
のバージョンがとても古い。
その為、下記にてレポジトリを更新する。
curl --silent --location https://rpm.nodesource.com/setup_6.x | bash -
今度は、nodejs
のバージョンが6.11と最新になっているので、そのままインストールする。
# yum install nodejs
====================================
Package Arch Version Repository Size
====================================
Installing:
nodejs x86_64 2:6.11.0-1nodesource.el6 nodesource 9.6 M
Transaction Summary
====================================
Install 1 Package(s)
Total download size: 9.6 M
Installed size: 33 M
Is this ok [y/N]: y
Installed:
nodejs.x86_64 2:6.11.0-1nodesource.el6
Complete!
最新であることを確認。
# node --version
v6.11.0
Phoenixのインストール
$ mix archive.install https://github.com/phoenixframework/archives/raw/master/phoenix_new.ez
Are you sure you want to install "https://github.com/phoenixframework/archives/raw/master/phoenix_new.ez"? [Yn] Y
Phoenixアプリの生成
オプションは普通の場合は必要ないが、ディレクトリ名と使用するDBは、変更したいのでオプション指定する。
$ mix phoenix.new PhoenixSample --database mysql --app phoenix_sample
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 PhoenixSample
$ 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
MySQLのDB設定
データベースにはMySQL
を使用するので、MySQL
のDBとユーザを作成。
mysql> CREATE DATABASE DB名 character SET utf8;
mysql> GRANT ALL ON DB名.* TO DBユーザ@localhost IDENTIFIED BY 'パスワード';
生成されたアプリのConfigファイルを書き換える。
config/dev.exs
# Configure your database
config :phoenix_sample, PhoenixSample.Repo,
adapter: Ecto.Adapters.MySQL,
username: "DBユーザ名",
password: "DBパスワード",
database: "DB名",
hostname: "ホスト名",
pool_size: 10
EctoにてDBアクセスに問題ないかチェック
$ mix ecto.create
==> connection
Compiling 1 file (.ex)
Generated connection app
==> phoenix_sample
Generated phoenix_sample app
The database for PhoenixSample.Repo has already been created
サーバ起動
$ mix phoenix.server
[info] Running PhoenixSample.Endpoint with Cowboy using http://localhost:4000
おなじみのWeb画面がサーバに表示された。
調子いい。