その他
    ホーム技術発信DoRubyAWS + Nginx + UnicornでRailsアプリを公開してみる

    AWS + Nginx + UnicornでRailsアプリを公開してみる

    この記事はアピリッツの技術ブログ「DoRuby」から移行した記事です。情報が古い可能性がありますのでご注意ください。

    はじめに

    今ならAWSの諸機能が、特定条件下なら1年無料で使えるとのことだったので、試しにRailsアプリをAWS上で作成・公開出来るようにしてみました。

    以下のサイトなどを参考にしています。
    (デプロイ編①)世界一丁寧なAWS解説。EC2を利用して、RailsアプリをAWSにあげるまで
    【AWS】Ruby on Rails + Nginx + Unicorn + MySQL 環境構築

    Cloud9で環境を構築する。

    Cloud9とは、AWS上で利用できるクラウドIDEです。
    AWSコンソールのCloud9から、環境を構築できます。

    EC2インスタンスは、t2.microタイプであれば無料枠の範囲で利用できるので、こちらを選択します

    構築が終わると、cloud9のコンソールが開きます。

    rubyとrailsはインストールされているようなので、今回はこのままアプリを作成します。

    $ ruby -v
    ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
    $ rails -v
    Rails 5.2.0
    
    # アプリの作成
    $ rails new test-app
    $ rails g scaffold user
    
    # マイグレートとアセットプリコンパイル
    $ rake db:migrate RAILS_ENV=production
    $ rake assets:precompile

    Unicornの導入と設定

    Gemファイルに追記してUnicornを使えるようにします。

    # Gemfile 追記
    group :production, :staging do
        gem 'unicorn'
    end
    
    # インストール
    $ bundle install

    config配下に、unicornの設定ファイル(unicorn.rb)を作成・編集します。

      #  unicron.rb
      # set lets
      $worker  = 2
      $timeout = 30
      $app_dir = "/home/ec2-user/environment/test-app/" #アプリの場所
      $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

    Nginxの導入と設定

    # Nginxの導入
    $ sudo yum install nginx
    -- 省略 --
    Complete!
    
    # 設定ファイルの書き換え
    $ sudo vi /etc/nginx/nginx.conf
    
    pid "/var/run/nginx.pid";
    events {
      worker_connections 2048;
    }
    
    http {
      upstream unicorn {
        server unix:/home/ec2-user/environment/test-app/tmp/sockets/.unicorn.sock;
      }
      server {
        listen 80;
        server_name XXX.XXX.XXX.XXX; ←EC2インスタンスに割り当てられたIP
        root /home/ec2-user/environment/test-app/;
        error_log /home/ec2-user/environment/test-app/log/nginx.log;
        include /etc/nginx/mime.types;
        location / {
          if (-f $request_filename) { break; }
          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とnginxの起動

    NginxとUnicornを起動して、webページが閲覧出来るか確認してみます。

     # unicornの起動
    $ bundle exec unicorn_rails -E production -c config/unicorn.rb -D
     # 起動確認
    $ ps aux | grep unicorn
    
    #nginxの起動
    $ sudo service nginx start
    #起動確認
    $ ps aux | grep nginx

    起動後、EC2インスタンスのIPアドレスにアクセスしたところ無事繋がりました。
    (なにもない簡素なページですが。。。)

    終わりに

    AWSを利用して、railsで作成したアプリをweb公開できました。
    NginxやUnicornについて理解がまだ浅いので、どんな設定が出来るのかなど理解を深めていきたいと思います。

    モバイルバージョンを終了