LoginSignup
23
28

More than 5 years have passed since last update.

Ubuntu + Nginx + Puma + Rails5

Last updated at Posted at 2017-10-23

はじめに

一度やったら当分やらないようなインフラ設定の方法ってどんどん忘れていく。

というわけで、
ubuntu + unix + puma + rails5
構成を立ち上げるまでの備忘録です。

unicorn と pumaの違いみたいな話

このあたりが参考になる。今回はPumaを選択しました。

Unicorn vs. Puma vs. Passenger: which app server is right for you?
http://blog.scoutapp.com/articles/2017/02/10/which-ruby-app-server-is-right-for-you

version

$ nginx -V
nginx version: nginx/1.4.6 (Ubuntu)
built by gcc 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)

$ rails -v
Rails 5.1.4

& puma --version
puma version 3.10.0

やること

rubyapp -> puma -> nginx
という風に、socketの場所をpumaとnginxに設定することでrubyappをnginxから見れるようにする。
(下記の図のUnicornとpumaを入れ替えて考える)
あとpumaを起動するfileを用意するみたいな感じ。

alt
http://blog.honeybadger.io/how-unicorn-talks-to-nginx-an-introduction-to-unix-sockets-in-ruby/

pumaの設定

railsのアプリディレクトリの中に
ファイルは存在しているのでsockで通信できるようにするために下記設定を足す

app/config/puma.rb
_app_path = "#{File.expand_path("../..", __FILE__)}"
_app_name = File.basename(_app_path)
_home = ENV.fetch("HOME") { "/home/ubuntu" }
pidfile "#{_home}/run/#{_app_name}.pid"
bind "unix://#{_home}/run/#{_app_name}.sock"
directory _app_path

pidが出力されるdirを作る

mkdir /home/ubuntu/run/

nginxの設定

nginxの場合はconf.dにアプリ名+.confというファイルを設置すると
設定を読み込んでくれる.

その前に
デフォルトだと
/etc/nginx/sites-available/defaultという
デフォルトファイルを設定に読みに行っていつまでたってもnginxの.htmlを読みにいくので、
書きをコメントアウトする必要がある。(またはdefaultを消す)

/etc/nginx/nginx.conf
#コメントアウトする
#include /etc/nginx/sites-enabled/*;
/etc/nginx/conf.d/appname.conf
upstream appname {
    server unix:///home/ubuntu/run/appname.sock;
}
/etc/nginx/conf.d.appname.conf
server {
    listen 80;
    server_name 192.168.0.1; # 本番環境のipアドレス
    root /home/ubuntu/appname/public; # アプリケーション名
    location / {
        proxy_pass http://appname; # アプリケーション名
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }

    access_log /var/log/appname.access.log;
    error_log /var/log/appname.error.log;

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
}

再起動

& sudo service nginx restart

起動ファイルの作成

/etc/init.d.puma
#!/bin/bash

NAME=puma
USER=ubuntu
APP_NAME=appname
APP_DIR=/home/$USER/$APP_NAME
TMP_DIR=$APP_DIR/tmp
PID_DIR=$TMP_DIR/pids
SOCKET_DIR=$TMP_DIR/sockets
LOG_DIR=$TMP_DIR/logs
PUMA_BIN=/home/$USER/.rbenv/shims/puma

RAILS_ENV=staging

sudo -u $USER -H sh -c "mkdir -p $PID_DIR"
sudo -u $USER -H sh -c "mkdir -p $SOCKET_DIR"
sudo -u $USER -H sh -c "mkdir -p $LOG_DIR"

PIDFILE=$PID_DIR/puma.pid


start() {
  cd $APP_DIR;
  sudo -u $USER -H sh -c "echo \$\$ > $PIDFILE; RAILS_ENV=$RAILS_ENV $PUMA_BIN -C $APP_DIR/config/puma.rb -d"
}

stop() {
  echo -n "Stopping puma"
  kill -s SIGTERM `cat $PIDFILE`
}

restart() {
  stop
  start
}

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

起動

$ sudo /etc/init.d/puma start

参考

23
28
2

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
23
28