😊

【Nginx+Unicorn】 サーバ起動手順まとめ

2021/01/17に公開

参考文献

この記事は、以下の情報を参考にして執筆しました(かなりお世話になりました)

-(デプロイ編②)世界一丁寧なAWS解説。EC2を利用して、RailsアプリをAWSにあげるまで

-AWS+Nginx+Unicornを利用してRailsアプリをデプロイしてみた。〜その1〜

-[Unicorn設定のまとめ] (https://qiita.com/syou007/items/555062cc96dd0b08a610)

Unicorn設定

Unicorn.rbは、"自分のアプリケーション/config"直下に配置

$worker  = 2
  $timeout = 30
  $app_dir = "/var/www/XXXXXX” #アプリケーション名
  $listen  = File.expand_path 'tmp/sockets/.unicorn.sock', $app_dir
  $pid     = File.expand_path 'tmp/pids/unicorn.pid', $app_dir
  $std_log = File.expand_path 'log/unicorn.log', $app_dir
  # set config
  worker_processes  $worker
  working_directory $app_dir
  stderr_path $std_log
  stdout_path $std_log
  timeout $timeout
  listen  $listen
  pid $pid
  # loading booster
  preload_app true
  # before starting processes
  before_fork do |server, worker|
    defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
    old_pid = "#{server.config[:pid]}.oldbin"
    if old_pid != server.pid
      begin
        Process.kill "QUIT", File.read(old_pid).to_i
      rescue Errno::ENOENT, Errno::ESRCH
      end
    end
  end
  # after finishing processes
  after_fork do |server, worker|
    defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
  end

Unicorn起動コマンド

$ bundle exec unicorn_rails -c config/unicorn.rb

Unicorn起動確認

$ ps -ef | grep unicorn | grep -v grep

Unicorn停止コマンド

$ kill -QUIT `cat /path/to/unicorn.pid`

Nginx設定

" etc/nginx/nginx.conf "で設定
$ sudo vi /etc/nginx/nginx.conf

user  nginx;
    worker_processes  1;
​
    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;
​
​
    events {
        worker_connections  1024;
    }
​
    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;
​
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
​
        access_log  /var/log/nginx/access.log  main;
​
        sendfile        on;
        #tcp_nopush     on;
​
        keepalive_timeout  65;
​
        #gzip  on;
​
        # include /etc/nginx/conf.d/*.conf;
​
        upstream unicorn {
        server unix:/var/www/XXXXXXX/tmp/sockets/unicorn.sock;  #アプリケーション名
        }
    server {
          listen 3000;
          server_name XXXXXXXX; #IPアドレス
          access_log /var/log/nginx/app_access.log;
          error_log /var/log/nginx/app_error.log;
          try_files $uri/index.html @unicorn;
​
          root home/ec2-user/var/www/XXXXXXX/public; #アプリケーション名
          client_max_body_size 15M;
          client_body_temp_path /tmp/client_body;
​
          location @unicorn {
            proxy_pass http://unicorn;
          }
​
          location ~ ^/assets/ {
            root /var/www/XXXXXXXX/public;  #アプリケーション名
          }
          error_page 404 /404.html;
            location = /40x.html {
          }
          error_page 500 502 503 504 /50x.html;
            location = /50x.html {
          }
      }
    }

Nginx起動コマンド

$ sudo systemctl start nginx.service

Nginx起動確認

$ sudo systemctl status nginx.service

Nginx停止コマンド

$ sudo systemctl stop nginx.service

Discussion