systemdでunicornの自動起動を設定してみた


目次

1.はじめに
2.serviceファイル
3.自動起動設定
4.まとめ

はじめに

以前の記事「Webアプリデプロイ方法(AWS EC2編)」を通し、railsアプリをNginx+Unicornの環境で構築しましたが、unicornの自動起動設定が未済でしたので実施していきます。大まかな流れは以下の通りです。

①serviceファイルを作成する
②serviceファイルを設定する
③自動起動の設定をする

serviceファイル

serviceファイルの作成

EC2
#systemdの設定ファイルを格納するパスに移動する
$ cd /etc/systemd/system
#unicorn用のserviceファイルを作成する
$ sudo touch unicorn.service
#パーミッションが644でない場合はセキュリティを高める観点で644に設定する
$ sudo chmod 644 unicorn.service

serviceファイルの設定

EC2
#serviceファイルを編集する
$ sudo vi unicorn.service

1. 「i」を入力し、入力モードに切り替え、以下を追記する
-------------------------------------------------------------------------------------------------
[Unit]
#ユニットの説明
Description=The unicorn process
#設定するユニットの後に起動するユニット
Before=nginx.service
#設定するユニットの前に起動するユニット
After=network.target remote-fs.target

[Service]
#実行時のユーザ  ※適宜、自らのアプリに合わして設定すること
User=memadmin
#作業ディレクトリの指定  ※適宜、自分のアプリにおけるフルパスを記載すること
WorkingDirectory=/var/www/rails/memory_tank
#標準出力先をsyslogに設定
StandardOutput = syslog
#標準エラー出力先をsyslogに設定
StandardError = syslog
#syslogへ出力する際の識別子を設定
SyslogIdentifier = unicorn
#PIDファイルのフルパスを記載  ※適宜、自分のアプリにおけるパスを記載すること
PIDFile=/var/www/rails/memory_tank/tmp/pids/unicorn.pid
#サービスの起動完了を判定するタイミングを指定。指定コマンドがフォアグラウンドで実行を継続する場合。コマンドを実行したらすぐに起動完了と判定。
Type=simple
#プロセスが異常終了した場合にサービス再起動する
Restart=always
#変数  ※適宜、アプリの環境を指定
Environment=RAILS_ENV=production
#変数  ※適宜、unicornの設定ファイルのフルパスを記載すること
Environment=UNICORN_CONF=/var/www/rails/memory_tank/config/unicorn.conf.rb
#unicornの起動 ※ -lはログイン状態のシェルで-cは引数のコマンドを実行するという意味
ExecStart=/bin/bash -l -c 'bundle exec unicorn_rails -c ${UNICORN_CONF} -E ${RAILS_ENV} -D'
#unicornの停止
ExecStop=/usr/bin/kill -QUIT $MAINPID
#unicornの再起動
ExecReload=/usr/bin/kill -USR2 $MAINPID

[Install]
#enable時にこのUnitの.wantsディレクトリにシンボリックリンクをはる
WantedBy=multi-user.target

-------------------------------------------------------------------------------------------------
2. 「esc」を押して入力モードを終了する
3. 「:wq」を入力後、エンターを押して編集を完了する

自動起動設定

EC2
#unicornの自動起動設定
$ systemctl enable unicorn.service
#シンボリックリンクが貼られたことの確認
$ ll /etc/systemd/system/multi-user.target.wants/unicorn.service
lrwxrwxrwx 1 root root 35  1月 22 08:32 /etc/systemd/system/multi-user.target.wants/unicorn.service -> /etc/systemd/system/unicorn.service
#OS再起動
$ reboot
#unicornの自動起動確認
$ systemctl status unicorn
● unicorn.service - The unicorn process
   Loaded: loaded (/etc/systemd/system/unicorn.service; enabled; vendor preset: disabled)
   Active: active (running) since 土 2022-01-22 13:21:38 UTC; 3min 51s ago
 Main PID: 3488 (bundle)
   CGroup: /system.slice/unicorn.service
           ├─3488 unicorn_rails master -c /var/www/rails/memory_tank/config/unicorn.conf.rb -E production -D
           ├─3545 unicorn_rails worker[0] -c /var/www/rails/memory_tank/config/unicorn.conf.rb -E production -D
           └─3546 unicorn_rails worker[1] -c /var/www/rails/memory_tank/config/unicorn.conf.rb -E production -D

まとめ

今回はsystemdでunicornの自動起動を設定しました。これにより、OS再起動後も自動でunicornが起動されるため、EC2インスタンスの自動停止・再開(コスト削減のための夜間停止)を組み込んだケースにおいても、スムーズにアプリ利用が可能になると思います。

以上、最後まで読んで頂きありがとうございました!