3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

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

Last updated at Posted at 2020-05-06

#参考文献

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

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

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

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

#Unicorn設定

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

unicorn.rb

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?