LoginSignup
15
21

More than 5 years have passed since last update.

EC2 : Rails, MySQL, Redis, nginx, Unicorn 環境構築

Last updated at Posted at 2015-01-27

環境構築

  • EC2インスタンス生成

  • EC2インスタンスのSecurity groups確認

Ports 22 / Source : MyIP 
Ports 80 / Source : Anyway 
Ports 8081 / Source : Anyway 
  • SSHでEC2インスタンスへConnect

  • yumアップデート

sudo yum update

** sudo su - rootに変更してからやった方がいいかもです。
** またはuserを追加してそのuserに切り替えて使う。
http://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/install-LAMP.html

  • 基本インストール
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
  • nginxインストール
sudo yum install nginx
sudo /etc/init.d/nginx start
  • redisインストール
sudo yum --enablerepo=epel install redis

起動する。ついでに自動起動の設定もする。

sudo service redis start
sudo chkconfig redis on
  • mysqlインストール
sudo yum install mysql-server mysql-devel
mysqladmin -u root password <new password> 
sudo service mysqld start
  • ruby-buildインストール
git clone git://github.com/sstephenson/ruby-build.git
cd ruby-build
sudo ./install.sh
  • rbenvインストール
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
  • rubyインストール
rbenv install -l
rbenv install 2.5.0
rbenv rehash
rbenv global 2.5.0
  • bundlerインストール(bundler -vでバージョン確認ができない場合)
gem install bundler --no-rdoc --no-ri
  • rails インストール
sudo gem install rails
  • Rails アプリケーション新規作成
rails new <アプリ名> -d mysql

-dを指定しない場合はsqliteが設置されている必要がある。

  • Gemfile修正(#を外す)
cd <アプリ名>
vi Gemfile
gem 'therubyracer', :platforms => :ruby
gem 'unicorn'
bundle install
  • Rails アプリ確認
cd <アプリ名>
rails s

別のタブで curl -I localhost:3000 確認

  • Unicornインストール
gem install unicorn;rbenv rehash
  • config修正
cd <アプリ名>
vi config/unicorn.conf

# in 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
  • nginxの設定ファイル編集
sudo vi /etc/nginx/nginx.conf
# in 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;
        }
    }
}
  • nginx起動
sudo /etc/init.d/nginx start
  • unicorn起動
cd <アプリ名>
unicorn -c config/unicorn.conf -D
  • 動作確認 Public DNSもしくはPublic IPをブラウザで入力してRailsのWelcome aboardが表示されればOK

その他

sudo /etc/init.d/nginx stop
ps ax|grep unicorn|grep -v grep

これで表示されるmasterのプロセスをkillする

sudo /etc/init.d/nginx restart
15
21
2

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
15
21