LoginSignup
6
4

More than 5 years have passed since last update.

Cloud9でPhoenixプロジェクト作成

Last updated at Posted at 2016-10-06

動機

Cloud9のシンタックスハイライトにelixirがあったので入門がてらやってみた。
一つ一つのコマンドに関しては、あんまり詳しく書かないけど勘弁して下さい。

asdf導入

nvmとかrvmみたいなバージョンマネージャー。
elixir導入も簡単。

$ git clone https://github.com/asdf-vm/asdf.git ~/.asdf
$ echo '. $HOME/.asdf/asdf.sh' >> ~/.bashrc
$ echo '. $HOME/.asdf/completions/asdf.bash' >> ~/.bashrc
$ exec $SHELL -l

Elixirインストール

以下のコマンドをTerminalに入力する。1

$ asdf plugin-add erlang https://github.com/asdf-vm/asdf-erlang.git
$ asdf plugin-add elixir https://github.com/asdf-vm/asdf-elixir.git

$ asdf install erlang 18.3
$ asdf global erlang 18.3

$ asdf install elixir 1.2.5
$ asdf global elixir 1.2.5

最後にelixirがインストールされたか確認

$ elixir -v

PostgreSQLセットアップ

Phoenixは、デフォルトでPostgreSQLを使用する。
便利なことにCloud9にはデフォルトでPostgreSQLがインストールされている。

以下のコマンドを入力する。

$ sudo service postgresql start
$ sudo sudo -u postgres psql 

postgres-# \password postgres
Enter new password: (passwordを入力)

終わったらCtrl + dでコンソールを抜ける。

Phoenixインストール

パッケージマネージャーとPhoenix本体をインストール

# [Yn]みたいに聞かれたらYを入力する

$ mix local.hex
$ mix archive.install https://github.com/phoenixframework/archives/raw/master/phoenix_new.ez

Phoenixプロジェクト作成

とりあえずハローワールド

$ mix phoenix.new hello_phoenix
$ cd hello_phoenix

このままだと起動できないので設定ファイルを編集する

hello_phoenix/config/dev.exs
...

config :hello_phoenix, HelloPhoenix.Endpoint,
  http: [port: {:system, "PORT"}], # 起動するPORTを環境変数から取得するように変更

...

config :hello_phoenix, HelloPhoenix.Repo,
  adapter: Ecto.Adapters.Postgres,
  username: "postgres",
  password: "password" # PostgreSQLに設定したpassword入れる,
  database: "hello_phoenix_dev",
  template: "template0", # この行を追加しないと、起動時にDBを作成できない
  hostname: "localhost",
  pool_size: 10

Phoenix起動

# 初期セットアップ
$ mix ecto.create

# 起動
$ mix phoenix.server

起動したらTerminalのhttp://localhost:8080/をクリック -> Openをクリック。
ブラウザの新規タブでPhoenixの初期画面が表示される。

感想

結構まだ面倒くさい。
Cloud9標準で対応される日が早く来ることを願う。

注釈

6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4