facebook twitter hatena line email

「Ruby/rails/unicorn」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(プロジェクトにunicorn設定追加)
(プロジェクトにunicorn設定追加)
 
(同じ利用者による、間の21版が非表示)
行3: 行3:
  
 
==unicornインストール==
 
==unicornインストール==
gem install unicorn
+
$ gem install unicorn
  
 
==プロジェクトにunicorn設定追加==
 
==プロジェクトにunicorn設定追加==
 
新規追加
 
新規追加
  
config/unicorn.rb
+
$ vi config/unicorn.rb
  
 
  rails_root = File.expand_path('../../', __FILE__)
 
  rails_root = File.expand_path('../../', __FILE__)
行18: 行18:
 
  stdout_path "#{rails_root}/log/unicorn.log"
 
  stdout_path "#{rails_root}/log/unicorn.log"
 
  listen 8080
 
  listen 8080
 +
 +
別のアプリで8080を使っている場合はport番号を変える
 +
 +
アプリ別、環境別にport番号を別にする。
  
 
==unicorn起動==
 
==unicorn起動==
  $ unicorn_rails -c config/unicorn.rb -E development -D
+
  $ unicorn_rails -c config/unicorn.rb -D
 +
環境変数はdevelopment(開発用)となる
  
 
==unicorn停止==
 
==unicorn停止==
 
  $ kill -QUIT `cat tmp/unicorn.pid`
 
  $ kill -QUIT `cat tmp/unicorn.pid`
 +
 +
==以下エラーが出る場合==
 +
Bundler Error Backtrace: from /home/linux/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.16.0/lib/bundler/runtime.rb:80:in `block (2 levels) in require'
 +
 +
以下のコメントアウトを外す
 +
 +
vi Gemfile
 +
gem 'therubyracer', platforms: :ruby
 +
 +
==unicorn起動(環境指定する)==
 +
developmentの場合
 +
$ unicorn_rails -c config/unicorn.rb -E development -D
 +
testの場合
 +
$ unicorn_rails -c config/unicorn.rb -E test -D
 +
productionの場合
 +
$ unicorn_rails -c config/unicorn.rb -E production -D
 +
 +
==productionで500errorとなる場合==
 +
[[ruby/rails/SECRET_KEY_BASE設定]] [ショートカット]
 +
 +
を確認する
 +
 +
==productionの場合は以下でassetsを作っておく==
 +
rails assets:precompile
  
 
==unicorn-nginx連携==
 
==unicorn-nginx連携==
  upstream unicorn_server {
+
proxy_passとupstreamの文字列は同じにする。
 +
  upstream unicorn_server_production {
 
     server unix:/home/linux/helloworld/tmp/unicorn.sock;
 
     server unix:/home/linux/helloworld/tmp/unicorn.sock;
 
  }
 
  }
 
  server {
 
  server {
 
     listen 80;
 
     listen 80;
     server_name ruby.cloudcore2;
+
     server_name localhost;
 
     root /home/linux/helloworld/public;
 
     root /home/linux/helloworld/public;
 
     access_log /var/log/nginx/ruby_helloworld_access.log;
 
     access_log /var/log/nginx/ruby_helloworld_access.log;
行42: 行72:
 
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 
             proxy_set_header Host $http_host;
 
             proxy_set_header Host $http_host;
             proxy_pass http://unicorn_server;
+
             proxy_pass http://unicorn_server_production;
 
     }
 
     }
 
  }
 
  }
 +
 +
==nginxでPermissionエラーが出る時==
 +
connect() to unix:/~/tmp/unicorn.sock failed (13: Permission denied) while connecting to upstream, client:
 +
nginxをrootに変えればとりあえず、うまくいく。
 +
$ sudo vi /etc/nginx/nginx.conf
 +
# user  nginx;
 +
user  root;
 +
 +
==重い場合==
 +
proxy_passとupstreamの文字列に"ハイフン"を使ってたら"_"に修正する。
 +
 +
==envごとの変更処理反映について==
 +
developmentはunicornを再起動しなくてもscriptの変更が反映される。
 +
 +
productionはunicornを再起動するまでscriptの変更が反映されない。

2022年9月23日 (金) 01:00時点における最新版

unicornとは

rubyのアプリサーバー

unicornインストール

$ gem install unicorn

プロジェクトにunicorn設定追加

新規追加

$ vi config/unicorn.rb
rails_root = File.expand_path('../../', __FILE__)
worker_processes 2
working_directory rails_root
listen "#{rails_root}/tmp/unicorn.sock"
pid "#{rails_root}/tmp/unicorn.pid"
stderr_path "#{rails_root}/log/unicorn_error.log"
stdout_path "#{rails_root}/log/unicorn.log"
listen 8080

別のアプリで8080を使っている場合はport番号を変える

アプリ別、環境別にport番号を別にする。

unicorn起動

$ unicorn_rails -c config/unicorn.rb -D

環境変数はdevelopment(開発用)となる

unicorn停止

$ kill -QUIT `cat tmp/unicorn.pid`

以下エラーが出る場合

Bundler Error Backtrace: from /home/linux/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.16.0/lib/bundler/runtime.rb:80:in `block (2 levels) in require'

以下のコメントアウトを外す

vi Gemfile

gem 'therubyracer', platforms: :ruby

unicorn起動(環境指定する)

developmentの場合

$ unicorn_rails -c config/unicorn.rb -E development -D

testの場合

$ unicorn_rails -c config/unicorn.rb -E test -D

productionの場合

$ unicorn_rails -c config/unicorn.rb -E production -D

productionで500errorとなる場合

ruby/rails/SECRET_KEY_BASE設定 [ショートカット]

を確認する

productionの場合は以下でassetsを作っておく

rails assets:precompile

unicorn-nginx連携

proxy_passとupstreamの文字列は同じにする。

upstream unicorn_server_production {
    server unix:/home/linux/helloworld/tmp/unicorn.sock;
}
server {
    listen 80;
    server_name localhost;
    root /home/linux/helloworld/public;
    access_log /var/log/nginx/ruby_helloworld_access.log;
    error_log /var/log/nginx/ruby_helloworld_error.log;
    client_max_body_size 100m;
    error_page 500 502 503 504 /500.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_server_production;
    }
}

nginxでPermissionエラーが出る時

connect() to unix:/~/tmp/unicorn.sock failed (13: Permission denied) while connecting to upstream, client:

nginxをrootに変えればとりあえず、うまくいく。

$ sudo vi /etc/nginx/nginx.conf
# user  nginx;
user  root;

重い場合

proxy_passとupstreamの文字列に"ハイフン"を使ってたら"_"に修正する。

envごとの変更処理反映について

developmentはunicornを再起動しなくてもscriptの変更が反映される。

productionはunicornを再起動するまでscriptの変更が反映されない。