LoginSignup
2
3

More than 5 years have passed since last update.

「Rails5 x Nginx x Unicorn」かんたん環境構築

Last updated at Posted at 2017-03-15

yum モジュールインストール

$ sudo yum install -y openssl-devel
$ sudo yum install -y readline-devel
$ sudo yum install -y sqlite-devel ## MySQL 使用するならいらないかも

Rails アプリケーション雛形作成

$ cd /www/
$ rails new your_app -d mysql

nginx インストール

$ sudo rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
$ sudo yum -y install nginx

1. unicorn の設定

/www/your_app/config/unicorn.rb
# -*- coding: utf-8 -*-
worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3)
timeout 15
preload_app true  # 更新時ダウンタイム無し

listen "/www/your_app/tmp/unicorn.sock"
pid "/www/o/tmp/unicorn.pid"

before_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
    Process.kill 'QUIT', Process.pid
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection
end

# ログの出力
stderr_path File.expand_path('log/unicorn.log', ENV['RAILS_ROOT'])
stdout_path File.expand_path('log/unicorn.log', ENV['RAILS_ROOT'])

2. nginx の設定を行います。

/etc/nginx/conf.d/ 配下は自動でロードしてくれますので、下記 "service.conf" の service の文字列は適宜変更して下さい。

/etc/nginx/conf.d/service.conf
server {
  listen 80 default_server;
}

server {
  listen 80; 
  server_name your_app.com; 

  access_log /var/log/nginx/your_app_access.log;
  error_log /var/log/nginx/your_app_error.log;

  root /www/your_app/public;

  location ~ ^/assets/ {
    root         /www/your_app/public;
    access_log   off;
    expires      max;
    add_header   Cache-Control public;
    add_header   ETag "";
  }

  error_page 404 /404.html;
  location = /404.html {
    root /usr/share/nginx/html;
  }
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root /usr/share/nginx/html;
  }

  try_files $uri/index.html $uri @unicorn;

  location @unicorn {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;

    proxy_pass http://unicorn-unix-domain-socket; # unix domain socketを使う場合
    # proxy_pass http://unicorn-tcp;              # tcpを使う場合
  }
}
unicornを起動
$ ./bin/bundle exec unicorn_rails -c config/unicorn.rb -E production -D

これで本番環境の 80 番ポートでのアクセスが可能になります。

開発環境 3000 番ポートの対応

/etc/nginx/conf.d/local.conf
server {
  listen 3001; # 3001 番で受け付ける
  server_name upsnap.jp;
  root /www/upsnap;
  location / {
      proxy_pass http://localhost:3000;
  }
}

開発サーバ起動

$ ./bin/rails s

その後、http://your_domain.com:3001/ にアクセスし開発環境のページが表示されれば成功

以上の設定で環境構築が終了し、アプリケーションの開発に取り掛かる事ができるかと思います。

2
3
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
2
3