2015年2月3日火曜日

Ruby on Rails 5.0 on AWS


環境

  • Ruby 2.1.0p0
  • Rails 4.2.0
  • nginx
  • unicorn




手順

各種インストールする。

[ec2-user@ip-xxx ~]$ sudo yum install -y gcc-c++ patch readline readline-devel zlib zlib-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison git
[ec2-user@ip-xxx ~]$ git --version
git version 1.8.3.1

1. ruby-buildインストール
$ cd
$ git clone git://github.com/sstephenson/ruby-build.git
$ cd ruby-build
$ sudo ./install.sh
2.rbenv, rubyインストール
$ cd
$ git clone git://github.com/sstephenson/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
$ source ~/.bashrc
$ exec $SHELL -l
$ rbenv install -l
$ rbenv install 2.1.0;rbenv rehash
$ rbenv global 2.1.0

結果

[ec2-user@ip-xxx ~]$ ruby -v
ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-linux]
[ec2-user@ip-xxx ~]$ which gem
~/.rbenv/shims/gem
[ec2-user@ip-xxx ~]$ which ruby
~/.rbenv/shims/ruby
[ec2-user@ip-xxx ~]$

3.bundle 入れる

[ec2-user@ip-xxx ~]$ bundle list
-bash: bundle: コマンドが見つかりません
[ec2-user@ip-xxx ~]$ gem install bundler --no-rdoc --no-ri
Fetching: bundler-1.6.3.gem (100%)
Successfully installed bundler-1.6.3
1 gem installed
[ec2-user@ip-xxx ~]$ gem list

*** LOCAL GEMS ***

bigdecimal (1.2.3)
bundler (1.6.3)
io-console (0.4.2)
json (1.8.1)
minitest (4.7.5)
psych (2.0.2)
rake (10.1.0)
rdoc (4.1.0)
test-unit (2.1.0.0)
[ec2-user@ip-xxx ~]$ sudo rbenv rehash
[ec2-user@ip-xxx ~]$ ll
合計 0

4. Rails 入れる

[ec2-user@ip-xxx ~]$ gem install rails 
[ec2-user@ip-xxx ~]$ rbenv rehash
[ec2-user@ip-xxx ~]$ rails -v
 4.1.4
[ec2-user@ip-xxx ~]$ which rails 
~/.rbenv/shims/rails

5. Unicornインストール

Railsアプリ${my_app}のGemfileに以下を追記して、$ bundle installするだけ。
${my_app}/Gemfile


gem 'unicorn', '4.8.3'
$ bundle install

8. Unicorn の設定

$ cd myapp
$ vi config/unicorn.rb

# ファイルの記載内容


@dir = "/home/ec2-user/rails/WebApp/"

worker_processes 2

working_directory @dir

timeout 30

listen "#{@dir}tmp/sockets/unicorn.sock", :backlog => 64

pid "#{@dir}tmp/pids/unicorn.pid"

stderr_path "#{@dir}log/unicorn.stderr.log"

stdout_path "#{@dir}log/unicorn.stdout.log"

preload_app true


9.nginxを入れる

sudo yum -y install nginx

10.nginxの確認

sudo /etc/init.d/nginx start
# http://xxx.compute.amazon.com/
# Welcome to nginx on the Amazon Linux AMI!と出れば成功
sudo /etc/init.d/nginx stop
# 公開パス /usr/share/ngin/html/

11.nginxの設定ファイル編集

sudo vi /etc/nginx/nginx.conf
worker_processes  1;

events {
    worker_connections  1024;
}

http {

    upstream unicorn_server {

        server unix:/home/ec2-user/rails/WebApp/tmp/sockets/unicorn.sock fail_timeout=0;

    }

    server {
        listen       80;
        server_name  localhost;
        location ~ ^/assets/(.*) {
           alias /home/ec2-user/rails/WebApp/public/assets/$1;
        }

        location / {

            root /usr/share/nginx/html;

            index  index.html index.htm;

            try_files $uri/index.html $uri.html $uri @unicorn_rails_app;

        }

        location @unicorn_rails_app {

           if (-f $request_filename) { break; }



           proxy_set_header X-Real-IP  $remote_addr;

           proxy_set_header X-Forwarded-Server $http_host;

           proxy_set_header X-Forwarded-Host   $http_host;

           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

           proxy_set_header Host $http_host;

           proxy_redirect off;


           proxy_pass http://unicorn_server;

        }

    }
}

sudo vi /etc/nginx/nginx.conf worker_processes 1; events { worker_connections 1024; } http { upstream unicorn_server { server unix:/home/ec2-user/rails/WebApp/tmp/sockets/unicorn.sock fail_timeout=0; } server { listen 80; server_name localhost; location ~ ^/assets/(.*) { alias /home/ec2-user/rails/WebApp/public/assets/$1; } location / { root /usr/share/nginx/html; index index.html index.htm; try_files $uri/index.html $uri.html $uri @unicorn_rails_app; } location @unicorn_rails_app { if (-f $request_filename) { break; } proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Server $http_host; proxy_set_header X-Forwarded-Host $http_host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://unicorn_server; } } }

sudo vi /etc/nginx/nginx.conf
worker_processes  1;

events {
    worker_connections  1024;
}

http {

    upstream unicorn_server {


        server unix:/home/ec2-user/rails/WebApp/tmp/sockets/unicorn.sock fail_timeout=0;


    }

    server {
        listen       80;
        server_name  localhost;
        location ~ ^/assets/(.*) {
           alias /home/ec2-user/rails/WebApp/public/assets/$1;
        }

        location / {


            root /usr/share/nginx/html;


            index  index.html index.htm;


            try_files $uri/index.html $uri.html $uri @unicorn_rails_app;


        }


        location @unicorn_rails_app {


           if (-f $request_filename) { break; }







           proxy_set_header X-Real-IP  $remote_addr;


           proxy_set_header X-Forwarded-Server $http_host;


           proxy_set_header X-Forwarded-Host   $http_host;


           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;


           proxy_set_header Host $http_host;


           proxy_redirect off;





           proxy_pass http://unicorn_server;


        }

    }
}


再起動する。
sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx start


---

1 件のコメント:

  1. Really Good blog post.provided a helpful information.I hope that you will post more updates like this Ruby on Rails Online Course

    返信削除