この記事について
この記事はインフラ初心者がWebページをVPS上で公開したい!って思ってやったことを備忘録として書き残したものです。素人が書いているため情報の信憑性は保証しません。詳細は各自で調べながらやることをおすすめします。記事のとおりにやっておかしなことになっても責任は取れませんのでご了承ください。
serviceファイルの作成
sudo vi /etc/systemd/system/unicorn.service
でserviceファイルの編集を行います。serviceファイルの内容は次のようにします。
[Unit]
Description=The unicorn process
Before=nginx.service
After=network.target remote-fs.target
[Service]
User=memadmin
WorkingDirectory=/var/www/rails_app/current
SyslogIdentifier=unicorn
PIDFile=/var/www/rails_app/shared/tmp/pids/unicorn.pid
Type=simple
Restart=always
EnvironmentFile=/path/to/.env
Environment=RAILS_ENV=production
Environment=UNICORN_CONF=/var/www/rails_app/current/config/unicorn.conf.rb
ExecStart=/bin/zsh -l -c 'path/to/bundle exec unicorn -c ${UNICORN_CONF} -E ${RAILS_ENV} -D'
ExecStop=/usr/bin/kill -QUIT $MAINPID
ExecReload=/usr/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
起動設定
下記のコマンドで、起動時に自動でunicornのサービスが立ち上がるようになります。
systemctl enable unicorn.service
手動で起動/停止したい場合は
systemctl start unicorn.service # 起動
systemctl stop unicorn.service # 停止
systemctl restart unicorn.service # 再起動
で出来ます。
systemctl status unicorn.service
で正常に起動されたか確認しましょう。
参考文献
systemdでunicornの自動起動を設定してみた
https://qiita.com/Unimaru/items/c4af76e8f28837b85a44