22
20

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.

CentOS6.4でRuby2.0+Rails4+pumaデーモン+Nginx

Last updated at Posted at 2014-06-02

Gitlabの設定を参考にconfig/puma.rb, /etc/init.d/puma-myproject, /etc/nginx/conf.d/myproject.confを作成すればOKでした.

config/puma.rb

# Start Puma with next command:
# RAILS_ENV=production bundle exec puma -C ./config/puma.rb

# uncomment and customize to run in non-root path
# note that config/puma.yml web path should also be changed
application_path = "#{File.expand_path("../..", __FILE__)}"

# The directory to operate out of.
#
# The default is the current directory.
#
directory application_path

# Set the environment in which the rack's app will run.
#
# The default is “development”.
#
environment 'production'

# Daemonize the server into the background. Highly suggest that
# this be combined with “pidfile” and “stdout_redirect”.
#
# The default is “false”.
#
daemonize true

# Store the pid of the server in the file at “path”.
#
pidfile "#{application_path}/tmp/pids/puma.pid"

# Use “path” as the file to store the server info state. This is
# used by “pumactl” to query and control the server.
#
state_path "#{application_path}/tmp/pids/puma.state"

# Redirect STDOUT and STDERR to files specified. The 3rd parameter
# (“append”) specifies whether the output is appended, the default is
# “false”.
#
stdout_redirect "#{application_path}/log/puma.stdout.log", "#{application_path}/log/puma.stderr.log"
# stdout_redirect '/u/apps/lolcat/log/stdout', '/u/apps/lolcat/log/stderr', true

# Disable request logging.
#
# The default is “false”.
#
# quiet

# Configure “min” to be the minimum number of threads to use to answer
# requests and “max” the maximum.
#
# The default is “0, 16”.
#
# threads 0, 16

# Bind the server to “url”. “tcp://”, “unix://” and “ssl://” are the only
# accepted protocols.
#
# The default is “tcp://0.0.0.0:9292”.
#
# bind 'tcp://0.0.0.0:9292'
bind "unix://#{application_path}/tmp/sockets/puma.socket"

# Instead of “bind 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert'” you
# can also use the “ssl_bind” option.
#
# ssl_bind '127.0.0.1', '9292', { key: path_to_key, cert: path_to_cert }

# Code to run before doing a restart. This code should
# close log files, database connections, etc.
#
# This can be called multiple times to add code each time.
#
# on_restart do
#   puts 'On restart...'
# end

# Command to use to restart puma. This should be just how to
# load puma itself (ie. 'ruby -Ilib bin/puma'), not the arguments
# to puma, as those are the same as the original process.
#
# restart_command '/u/app/lolcat/bin/restart_puma'

# === Puma control rack application ===

# Start the puma control rack application on “url”. This application can
# be communicated with to control the main server. Additionally, you can
# provide an authentication token, so all requests to the control server
# will need to include that token as a query parameter. This allows for
# simple authentication.
#
# Check out https://github.com/puma/puma/blob/master/lib/puma/app/status.rb
# to see what the app has available.
#
# activate_control_app 'unix:///var/run/pumactl.sock'
# activate_control_app 'unix:///var/run/pumactl.sock', { auth_token: '12345' }
# activate_control_app 'unix:///var/run/pumactl.sock', { no_token: true }

/etc/init.d/puma-myproject

#!/bin/bash
#
# puma-myproject

# chkconfig: 2345 82 55
# processname: puma-myproject
# description: Runs puma-myproject for nginx integration.

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

# The name of the service
NAME=puma-myproject

# The username and path to the myapp source
USER=rails
APP_PATH=/path/to/myproject

# 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="-C $APP_PATH/config/puma.rb -e production"

# Ruby related path update
RUBY_PATH_PATCH="PATH=$PATH:/usr/local/bin:/usr/local/lib && export PATH && "
BUNDLE_CMD=bundle
PUMA_CMD=puma

start() {
  cd $APP_PATH

  # Start puma
  echo -n $"Starting $NAME: "
  daemon --pidfile=$UPID --user=$USER "$RUBY_PATH_PATCH $BUNDLE_CMD exec $PUMA_CMD $OPTS"
  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

起動スクリプトを作成したら以下のように起動&停止を確認します.

$ sudo /etc/init.d/puma-myproject status
puma-myproject は停止しています
$ sudo /etc/init.d/puma-myproject start
Starting puma-trendword: Puma starting in single mode...
* Version 2.8.2 (ruby 2.0.0-p481), codename: Sir Edmund Percival Hillary
* Min threads: 0, max threads: 16
* Environment: production
* Listening on unix:///path/to/myproject/tmp/sockets/puma.socket
* Daemonizing...
                                                        [  OK  ]
$ sudo /etc/init.d/puma-myproject status
puma-myproject (pid  7514) を実行中...
$ sudo /etc/init.d/puma-myproject stop
Stopping puma-myproject:                                [  OK  ]
$ sudo /etc/init.d/puma-myproject status
puma-myproject は停止しています

/etc/nginx/conf.d/myproject.conf

upstream myproject {
  server unix:/home/git/myproject/tmp/sockets/myproject.socket;
}

server {
  listen myhost:80 default_server;
  server_name myhost;
  root /home/git/myproject/public;

  # individual nginx logs for this myproject vhost
  access_log  /var/log/nginx/myproject_access.log;
  error_log   /var/log/nginx/myproject_error.log;

  location /myproject {
    # serve static files from defined root folder;.
    # @myproject is a named location for the upstream fallback, see below
    try_files $uri $uri/index.html $uri.html @myproject;
  }

  # if a file, which is not found in the root folder is requested,
  # then the proxy pass the request to the upsteam (myproject unicorn)
  location @myproject {
    proxy_read_timeout 300; # https://github.com/myprojecthq/myprojecthq/issues/694
    proxy_connect_timeout 300; # https://github.com/myprojecthq/myprojecthq/issues/694
    proxy_redirect     off;

    proxy_set_header   X-Forwarded-Proto $scheme;
    proxy_set_header   Host              $http_host;
    proxy_set_header   X-Real-IP         $remote_addr;

    proxy_pass http://myproject;
  }
}

Nginx設定後に設定を反映すればOKです.

22
20
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
22
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?