LoginSignup
17
16

More than 5 years have passed since last update.

CentOS+Rails4+Apache2.2でPumaを動かす設定

Posted at

この記事は、自分の記事「CentOS+Rails4+Apache2.2でPumaを設定する | noriaki blog」の設定部分を備忘的に抜き出したものなので、詳細な手順は記事を参照のこと。

Puma設定

ディレクトリ作成

% mkdir -p tmp/pids
% mkdir -p tmp/sockets

設定ファイル(config/puma.rb)

config/puma.rb
require 'active_support'
require 'active_support/core_ext'

rails_root = Dir.pwd

unless ENV['RACK_ENV'] == 'production'
  environment ENV['RACK_ENV'] || 'development'
  daemonize true  # デーモン化

  workers 0  # 1以上を指定するとCluster化する
  threads 0,2  # スレッド数(最小, 最大)

  bind 'unix://' + File.join(rails_root, 'tmp', 'sockets', 'puma.sock')  # /PATH/TO/MYAPP/tmp/sockets/puma.sock
  port 9293  # Port番号

  pidfile File.join(rails_root, 'tmp', 'pids', 'puma.pid')  # /PATH/TO/MYAPP/tmp/pids/puma.pid
  state_path File.join(rails_root, 'tmp', 'puma.state')  # /PATH/TO/MYAPP/tmp/puma.state

  stdout_redirect(  # pumaのログをlog/以下に出力する。trueは追記モード。
    File.join(rails_root, 'log', 'puma.log'),
    File.join(rails_root, 'log', 'puma-error.log'),
    true
  )

  # pumactlのトークンを指定 via. http://qiita.com/takkkun/items/ecdee7e7dec1bcc9a5b5
  activate_control_app('auto', auth_token: rails_root.camelize.parameterize)
end

Apache設定

アプリ用バーチャルホスト

conf.d/virtualhost_MYAPP.conf
# -*- coding: utf-8; mode: conf; -*-

##
# MYAPP
#
<virtualhost *:80>
    DocumentRoot "/PATH/TO/MYAPP/public" # Railsアプリのpublicディレクトリを指定
    ServerName myapp.example.com  # Railsアプリのドメインを指定

    RewriteLog logs/MYAPP-rewrite_log
    RewriteLogLevel 1
    ErrorLog logs/MYAPP-error_log
    CustomLog logs/MYAPP-access_log combined env=!no_log

    SetEnv RACK_ENV developmen  # Railsアプリの実行環境(development/test/production)

    # for puma config
    include conf.d/puma-conf  # ここでPuma共通設定ファイルを読み込んでいる
    RewriteRule ^/(.*)$ http://0.0.0.0:9293%{REQUEST_URI} [P]  # '9293'のところにはconf/puma.rbで指定したPort番号を設定

    <directory "/PATH/TO/MYAPP/public">
        AllowOverride All
        Options FollowSymLinks -MultiViews
        Order deny,allow
        Allow from all
    </directory>

</virtualhost>

Puma用共通設定ファイル

conf.d/puma-conf
# -*- coding: utf-8; mode: conf; -*-

    # Redirect all requests that don't match a file on disk
    # under DocumentRoot get proxied to Puma
    RewriteEngine On
    RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f

    # Don't allow client to fool Puma into thinking connection is secure
    RequestHeader unset X-Forwarded-Proto

    # Disable ETags (https://github.com/h5bp/server-configs-apache/tree/master/doc#configure-etags)
    # Set Expiration date for all assets to one year in the future
    <locationmatch "^/assets/.*$">
        Header unset ETag
        FileETag None

        ExpiresActive On
        ExpiresDefault "access plus 1 year"
    </locationmatch>

    # Rewrite requests for js and css to gzipped versions
    # if client and server support it
    <locationmatch "^/assets/.*\.(css|js)$">
        RewriteEngine on
        RewriteCond %{HTTP:Accept-Encoding} \b(x-)?gzip\b
        RewriteCond %{REQUEST_FILENAME}.gz -s
        RewriteRule ^(.+)$ $1.gz
    </locationmatch>

    # Set type and headers for gzipped css
    <locationmatch "^/assets/.*\.css\.gz$">
        ForceType text/css
        Header set Content-Encoding gzip
        Header add Vary Accept-Encoding
    </locationmatch>

    # Set type and headers for gzipped js
    <locationmatch "^/assets/.*\.js\.gz$">
        ForceType application/javascript
        Header set Content-Encoding gzip
        Header add Vary Accept-Encoding
    </locationmatch>

    # Compress HTML on the fly
    AddOutputFilterByType DEFLATE text/html

詳細な手順が必要な方は拙ブログ記事(CentOS+Rails4+Apache2.2でPumaを設定する | noriaki blog)をどうぞ。

環境情報

Ruby, Rails, RubyGems

% rake about
About your application's environment
Rails version        4.2.0
Ruby version         2.1.5-p273 (x86_64-linux)
RubyGems version     2.4.4
Rack version         1.5

Puma

% puma --version
puma version 2.10.2

Apache

% /usr/sbin/httpd -v
Server version: Apache/2.2.15 (Unix)
Server built:   Oct 16 2014 14:48:21

CentOS

% inxi -SM
System:    Host: hakone.vps.sakura.ne.jp Kernel: 2.6.32-431.1.2.0.1.el6.x86_64 x86_64 (64 bit)
           Console: tty 1 Distro: CentOS release 6.6 (Final)
Machine:   System: Red Hat product: KVM v: RHEL 6.4.0 PC
           Mobo: N/A model: N/A Bios: Sea v: 0.5.1 date: 01/01/2007
17
16
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
17
16