LoginSignup
0
0

More than 5 years have passed since last update.

vagrant + nginx + unicorn + rails5.1

Last updated at Posted at 2018-04-07

VirtualBox vagrantを入れる部分、rubyを導入する部分は省略します

nginxを導入します

nginxのインストール
yum install http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

nginxの設定

nginx+unicornで動作するようにしたいのでそれを見越して設定ファイルを作成する
/etc/nginx/conf.d/配下に rails.confを作成する

rails.confの作成
upstream unicorn {
    server unix:/var/run/unicorn/unicorn.sock;
}

server {
    server_name 192.168.33.10;
    listen 80;

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

    root /var/www/myapp/public;

    client_max_body_size 100m;
    error_page  404              /404.html;
    error_page  500 502 503 504  /500.html;

    location / {
       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;
    }
}

unicorn用のsockとpidは/var/run/unicorn/フォルダー内に配置する予定です(所有権をvagrantにしておきます)

あと、/etc/nginx/nginx.confのユーザーも変更しておきます

nginx.confのuser変更
user vagrant;

railsアプリを適当なフォルダーに作成

個人的には/var/www配下に作るのが好きなのでそこに配置します。

railsのインストール
rails new myapp

作成したら早速config/unicorn.rbを作成しよう

unicorn.rb
rails_root = File.expand_path('../../', __FILE__)

worker_processes 2
working_directory rails_root

listen "/var/run/unicorn/unicorn.sock"
pid "/var/run/unicorn/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 "#{rails_root}/log/unicorn_error.log"
stdout_path "#{rails_root}/log/unicorn.log"

### unicorn

ここまできたら、unicornの起動を管理するtaskを作成しておこう

/{rails_app}/lib/tasks/unicorn.rake

unicorn用のtaskを作成
namespace :unicorn do
  # Tasks
  desc "Start unicorn"
  task(:start) {
    config = Rails.root.join('config', 'unicorn.rb')
    sh "unicorn -c #{config} -E development -D"
  }

  desc "Stop unicorn"
  task(:stop) {
    unicorn_signal :QUIT
  }

  desc "Restart unicorn with USR2"
  task(:restart) {
    unicorn_signal :USR2
  }

  desc "Increment number of worker processes"
  task(:increment) {
    unicorn_signal :TTIN
  }

  desc "Decrement number of worker processes"
  task(:decrement) {
    unicorn_signal :TTOU
  }

  desc "Unicorn pstree (depends on pstree command)"
  task(:pstree) do
    sh "pstree '#{unicorn_pid}'"
  end

  # Helpers
  def unicorn_signal signal
    Process.kill signal, unicorn_pid
  end

  def unicorn_pid
    begin
      File.read("/var/run/unicorn.pid").to_i
    rescue Errno::ENOENT
      raise "Unicorn does not seem to be running"
    end
  end
end

これで rake unicorn:start などでunicornを扱えるようになる

よっしゃこれでできた!と思い。設定していた http://192.168.33.10  

を開いてみると//// 502BadGateWayが出てくる。。。。。

色々といじってみたもののわからず。。。。ついに行き着いた先がこれ

SeLinuxをいじる

seLinuxの設定を変更
sudo setenforce 0

/etc/selinux/config
#SELINUX=enforcing
SELINUX=permissive

/etc/selinux/config

Nginxを再起動
systemctl reload nginx

無事Railsページを表示できました!

0
0
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
0
0