DEV Community

dala00
dala00

Posted on • Edited on • Originally published at crieit.net

2 2

Systemdを使ったPhoenixの本番デプロイ詳細例

サーバーを準備し、コンテナを使わずに運用できるところまでの準備まで一通りまとめてみた。
Systemdで動作させる。

(以前残したログをまとめているだけなので正確でない可能性あり)

前提

  • Elixir1.5
  • Phoenix1.3
  • Debian

初期設定

最初だけ本番で調整したものをコミットしたりもするのでgitもちょっと設定。

sudo apt-get update
sudo apt-get install -y git dbus
sudo timedatectl set-timezone Asia/Tokyo
git config --global user.name "name"
git config --global user.email "email"

Elixir & Phoneix & Node.js

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
wget https://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb && sudo dpkg -i erlang-solutions_1.0_all.deb
sudo apt-get update
sudo apt-get install -y nodejs inotify-tools esl-erlang elixir
mix local.hex --force
mix local.rebar --force
mix archive.install https://github.com/phoenixframework/archives/raw/master/phx_new.ez --force
sudo apt-get install --reinstall build-essential

DB

MySQLとかPostgreSQLとか。
普通は別サーバーだろうし省略。

ビルド

ssh-keygenかtokenかでcloneできるように準備。
適当なので環境変数とかユーザーは適宜いい感じにしてもらった方が良いかもしれない。

git clone ********.git
cd appdir
mix deps.get --only prod
sudo MIX_ENV=prod mix compile
cd assets
npm install
sudo npm install -g brunch
brunch build --production
cd ..
sudo MIX_ENV=prod mix phx.digest
sudo MIX_ENV=prod mix ecto.migrate
sudo MIX_ENV=prod mix phx.server

とりあえずここまでで動くか確認する。

Let's Encrypt

省略。下記の記事に一応詳細は書いてある。

[http://alphabrend.hatenablog.com/entry/2017/10/10/214222:title]

Systemd

sudo vi /etc/systemd/system/myapp.service
[Unit]
Description=My app

[Service]
WorkingDirectory=/home/username/appdir

Restart=always
Environment=MIX_ENV=prod HOME=/root
ExecStart=/usr/local/bin/mix phx.server

[Install]
WantedBy=multi-user.target
sudo systemctl enable myapp
sudo systemctl start myapp

運用

pull, compile, brunch build, digest, migrate, restart

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay