LoginSignup
10
6

More than 5 years have passed since last update.

AmazonLinuxでpumaの自動起動設定を行う

Posted at

AmazonLinuxにNginxとRailsをインストールして80番ポートからrailsに繋げるでnginxとrailsを立ち上げたのですが、nginxは自動起動設定したもののpumaは自動で起動しないので、サーバが再起動した場合にrailsアプリケーションが立ち上がりません。

今回はpumaの自動起動設定を行います。

/etc/init.d/puma の作成

以下の内容で/etc/init.d/pumaに実行ファイルを作成しました。NAMEやAPP_PATHは適宜自分の環境に合わせて変更してください。

#!/bin/bash
#
# railsapp

# chkconfig: 2345 82 55
# processname: railsapp
# description: Runs railsapp for nginx integration.

# Include RedHat function library
. /etc/rc.d/init.d/functions

# The name of the service
NAME=railsapp

# The username and path to the myapp source
USER=webapp
APP_PATH=/home/$USER/$NAME

# The PID and LOCK files used by puma and sidekiq
UPID=$APP_PATH/tmp/pids/puma.pid
ULOCK=/var/lock/subsys/$NAME

# The options to use when running puma
#OPTS="-F $APP_PATH/config/puma.rb"

# Ruby related path update
RUBY_PATH_PATCH="PATH=$PATH:/usr/local/bin:/usr/local/lib && export PATH && "
BUNDLE_CMD=bundle
PUMA_CMD=pumactl
. /etc/profile.d/rbenv.sh

start() {
  cd $APP_PATH
  # Start puma
  echo -n $"Starting $NAME: "
  daemon --pidfile=$UPID --user=$USER $BUNDLE_CMD exec $PUMA_CMD $OPTS start
  puma=$?
  [ $puma -eq 0 ] && touch $ULOCK
  echo

  return $puma
}

stop() {
  cd $APP_PATH

  # Stop puma
  echo -n $"Stopping $NAME: "
  killproc -p $UPID
  puma=$?
  [ $puma -eq 0 ] && rm -f $ULOCK
  echo

  return $puma
}

restart() {
  stop
  start
}

get_status() {
  status -p $UPID $NAME
}

query_status() {
  get_status >/dev/null 2>&1
}

case "$1" in
  start)
    query_status && exit 0
    start
    ;;
  stop)
    query_status || exit 0
    stop
    ;;
  restart)
    restart
    ;;
  status)
    get_status
    exit $?
    ;;
  *)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|status}" >&2
    exit 1
    ;;
esac

exit 0

作成し終わったら実行権限を付与しておきます。

$ ls -l /etc/init.d/puma
-rw-r--r-- 1 root root 1359 Jul  5 10:51 /etc/init.d/puma
$ sudo chmod +x /etc/init.d/puma
$ ls -l /etc/init.d/puma
-rwxr-xr-x 1 root root 1359 Jul  5 10:51 /etc/init.d/puma

自動起動設定

まずinitスクリプトを使ってpumaが起動することを確認しておきます。

$ sudo /etc/init.d/puma start
Starting railsapp: Puma starting in single mode...
* Version 3.9.1 (ruby 2.4.1-p111), codename: Private Caller
* Min threads: 5, max threads: 5
* Environment: development
* Daemonizing...
                                                           [  OK  ]

起動したようなので chkconfig でシステム起動時に起動するようにしておきます。

$ sudo chkconfig puma on
$ sudo chkconfig | grep puma
puma            0:off   1:off   2:on    3:on    4:on    5:on    6:off

これで再起動してpumaが起動してたら完了です。

参考

10
6
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
10
6