0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

systemd で Phoenix を管理する

Last updated at Posted at 2018-10-22

Phoenix をサービス化して systemd で管理できないかと思い、試してみました。
環境は、CentOS 7 の Docker コンテナを利用しています。

ユニットファイルの作成

ユニットファイルを以下のように作成してみました。
どのタイミングで参照するのかわかりませんが、環境変数 HOME を定義しないと起動に失敗しました。


[root@phoenix ~]# vim /etc/systemd/system/phoenix.service
[Unit]
Description=Phoenix web service
After=syslog.target
After=network.target
After=nginx.target

[Service]
WorkingDirectory=/apl/test
Environment=MIX_ENV=prod PORT=4001 HOME=/home/homulilly
ExecStartPre=/bin/mix ecto.migrate
ExecStartPre=/bin/mix clean
ExecStartPre=/bin/mix compile
ExecStartPre=/apl/test/compile-assets
ExecStartPre=/bin/mix phx.digest
ExecStart=/bin/mix phx.server
User=homulilly
Group=homulilly
StandardError=syslog
SyslogIdentifier=phoenix

[Install]
WantedBy=multi-user.target

assets のコンパイルのためのスクリプト作成

ユニットファイルの中で ExecStartPre に指定している compile-assets によって assets をコンパイルしています。


[homulilly@phoenix test]$ vim compile-assets
# !/bin/sh

LANG=en_US.UTF-8
NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

# clean
/bin/rm -f ./priv/static/css/*

# compile
cd ./assets
node node_modules/brunch/bin/brunch build

syslogd の設定ファイル作成

ログは syslogd で収集します。


[root@phoenix ~]# vim /etc/rsyslog.d/phoenix.conf
if $programname == 'phoenix' then /var/log/phoenix/phoenix.log

syslogd 再起動と Phoenix 起動


[root@phoenix ~]# systemctl restart rsyslog
[root@phoenix ~]# systemctl start phoenix

以上で、ソースコード変更がサービス再起動で反映されるようになりました。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?