AWS Ruby Rails4 Nginx Unicorn 構築！ (簡単に早めで)

## 仕様

* AWSの仕様
 * t2.micro


## 実装
ポチポチして、サーバーに入れることを確認(sudo yum update)。
※セキュリティグループで、22番は、myIPで。
　　　　　　　　　　　　 80番が、anywayにしないとNginxが動かないぞ！！！
そこから、

```
[ec2-user@ip-172-31-35-121 ~]$ git -v
-bash: git: コマンドが見つかりません
[ec2-user@ip-172-31-35-121 ~]$ ruby -v
ruby 2.0.0p451 (2014-02-24 revision 45167) [x86_64-linux]
[ec2-user@ip-172-31-35-121 ~]$ $ 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-172-31-35-121 ~]$ git --version
git version 1.8.3.1
```

次に、rbenvを入れる。

### 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-172-31-11-135 ~]$ ruby -v
ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-linux]
[ec2-user@ip-172-31-11-135 ~]$ which gem
~/.rbenv/shims/gem
[ec2-user@ip-172-31-11-135 ~]$ which ruby
~/.rbenv/shims/ruby
[ec2-user@ip-172-31-11-135 ~]$
```

### 3.bundle 入れる
```
[ec2-user@ip-172-31-35-121 ~]$ bundle list
-bash: bundle: コマンドが見つかりません
[ec2-user@ip-172-31-35-121 ~]$ 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-172-31-35-121 ~]$ 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-172-31-35-121 ~]$ sudo rbenv rehash
[ec2-user@ip-172-31-35-121 ~]$ ll
合計 0
```

### 4. rails 入れる
```
[ec2-user@ip-172-31-11-135 ~]$ gem install rails
[ec2-user@ip-172-31-11-135 ~]$ rbenv rehash
[ec2-user@ip-172-31-11-135 ~]$ rails -v
rails 4.1.4
[ec2-user@ip-172-31-11-135 ~]$ which rails
~/.rbenv/shims/
```

### 5.sqlite3いれる
```
$ sudo yum install sqlite-devel
$ gem install sqlite3;rbenv rehash
```

###5`.mysql を入れる
```
[ec2-user@ip- ~]$ sudo yum install mysql-server mysql-devel
[ec2-user@ip- ~]$ sudo service mysqld start
Initializing MySQL database:  Installing MySQL system tables...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h ip-172-31-1-139 password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl

Please report any problems at http://bugs.mysql.com/

                                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
[ec2-user@ip- ~]$ mysqladmin -u root password 'new-password!!'
[ec2-user@ip- ~]$ mysql -u root -p　#でログインできるか確認
```
### 6.unicornを入れる。
これは、gemfileに
```
vi Gemfile
gem 'unicorn'をコメントイン
bundle install --path vender/bundle
```

### 7. railsの確認
```
rails server
curl -I localhost:3000
```

### 8. unicornを入れる
```
cd myapp
vi config/unicorn.conf
# 中身
worker_processes 2
listen '/tmp/unicorn.sock'
stderr_path File.expand_path('unicorn.log', File.dirname(__FILE__) + '/../log')
stdout_path File.expand_path('unicorn.log', File.dirname(__FILE__) + '/../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 unicornapp {
        server unix:/tmp/unicorn.sock;
    }

    server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass http://unicornapp;
        }
    }
}
```
編集後。
```
sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx start
```
でブラウザを見る。


※nginx とunicornの詳細の設定は、また今度。



### 参考
 - [無料クラウドサーバEC2にRuby,環境を構築する手順まとめ](http://www.sakc.jp/blog/archives/32865)
 - [AmazonLinuxにrbenvでrubyを入れてみる](https://github.com/mechamogera/MyTips/wiki/AmazonLinux%E3%81%ABrbenv%E3%81%A7ruby%E3%82%92%E5%85%A5%E3%82%8C%E3%81%A6%E3%81%BF%E3%82%8B)
 - [のEC2上で+Unicorn+Nginxを実現する](https://gist.github.com/honjo2/5023446) ←ほぼこれ