LoginSignup
8
0
お題は不問!Qiita Engineer Festa 2023で記事投稿!

APIサーバをデーモンで動かすためのsupervisorの設定

Last updated at Posted at 2023-07-21

SupervisorとはPython製のプロセス管理ツール
プログラムなどを簡単にデーモン化することができる。

今回はEC2にデプロイしたGolangのAPIサーバをsupervisorで動かすことを想定する。

導入手順

bash
sudo yum install python3
sudo pip3 install supervisor
echo_supervisord_conf > ~/supervisord.conf

# 移動させる
sudo mkdir /etc/supervisord/
sudo mkdir /etc/supervisord/conf.d
sudo mv ~/supervisord.conf /etc/supervisord/supervisord.conf

# オリジナルとっとく
sudo cp /etc/supervisord/supervisord.conf /etc/supervisord/supervisord.conf.org

設定ファイルの作成

/etc/supervisord/supervisord.confの編集(長いから抜粋)

supervisord.conf
[inet_http_server]
port=127.0.0.1:9001 ; # superviserdを立ち上げるport。デバッグしたいときはnginxをここにつなげる
;username=user ; # ここら辺つけるとBasic認証みたいになる
;password=123

[supervisorctl]
;serverurl=unix:///tmp/supervisor.sock
serverurl=http://127.0.0.1:3000 ; # APIサーバを立ち上げるport

[include]
files = /etc/supervisord/conf.d/*.conf

権限の設定

実行権限をsupervisordにするため、serviceファイルを作成

bash
sudo touch /etc/systemd/system/supervisord.service

内容は以下

supervisord.service
[Unit]
Description=supervisord - Supervisor process control system for UNIX
Documentation=http://supervisord.org
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/bin/supervisord -c /etc/supervisord/supervisord.conf
ExecReload=/usr/local/bin/supervisorctl reload
ExecStop=/usr/local/bin/supervisorctl shutdown
User=root

[Install]
WantedBy=multi-user.target

言語ごとの設定

go用のconfigファイル用意

bash
sudo touch /etc/supervisord/conf.d/example_server.conf

設定内容

example_server.conf
[program:example_server]
command=/var/www/api/app/main # ここはフルパスで
directory=/var/www/api/app/ # 実行するディレクトリを決定してる
process_name=%(program_name)s_%(process_num)02d
autostart=true
autorestart=true
user=ec2-user
numprocs=1
redirect_stderr=true
stdout_logfile=/tmp/example_server.log

実行と永続化

bash
sudo systemctl start supervisord
sudo systemctl status supervisord
sudo systemctl enable supervisord

参考記事

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