13
15

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 5 years have passed since last update.

EC2にRails5+React+MySQLアプリをデプロイする環境をすばやく構築する

Last updated at Posted at 2016-09-24

ひとまずメモ的に。

前提

以下が完了している

Gitをいれる

sudo yum -y install git

Rubyをいれるための色々

yum でどーーん。

sudo yum -y install gcc-c++ glibc-headers openssl-devel readline libyaml-devel readline-devel zlib zlib-devel libffi-devel libxml2 libxslt libxml2-devel libxslt-devel sqlite-devel mysql mysql-server mysql-devel ImageMagick ImageMagick-devel epel-release

rbenvをいれる

git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
source .bash_profile

git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
rbenv rehash

rbenv install -v 2.2.4
rbenv global 2.2.4
rbenv rehash

ruby -v

gem install bundler

追記

もし rbenv installno such command と怒られる場合は以下を実行する

git clone git://github.com/sstephenson/ruby-build.git
cd ruby-build
sudo ./install.sh 

directoryを掘る

cd /
sudo chown [user] var
cd var
sudo mkdir www
cd www
sudo mkdir rails

ログ用

mkdir /var/www/rails/shared
mkdir /var/www/rails/shared/log

unicornをいれる

group :production, :staging do
  gem 'unicorn'
end
unicorn.conf.rb
# set lets
$worker  = 2
$timeout = 30
$app_dir = '/var/www/rails/[project]' 
$listen  = File.expand_path '/tmp/unicorn.sock', $app_dir
$pid     = File.expand_path '/var/www/rails/shared/tmp/pids/unicorn.pid', $app_dir
$std_log = File.expand_path '/var/www/rails/shared/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

MySql設定

sudo vi /etc/my.cnf

以下を追記

character-set-server=utf8

起動

sudo service mysqld start

ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock

nginxをいれる

sudo yum install nginx
sudo vi /etc/nginx/conf.d/[project].conf
# log directory
error_log  /var/www/rails/shared/log/nginx.error.log; 
access_log /var/www/rails/shared/log/nginx.access.log; 

# max body size
client_max_body_size 2G;
upstream app_server {
  # for UNIX domain socket setups
  server unix:/tmp/unicorn.sock fail_timeout=0; 
}
server {
  listen 80;
  server_name 52.197.122.210;
  # nginx so increasing this is generally safe...
  keepalive_timeout 5;
  # path for static files
  root /var/www/rails/[project]/public; 
  # page cache loading
  try_files $uri/index.html $uri.html $uri @app;
  location @app {
    # HTTP headers
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://app_server;
  }
  # Rails error pages
  error_page 500 502 503 504 /500.html;
  location = /500.html {
    root /var/www/rails/[project]/public; 
  }
}

Github

GithubのDeploy Keysを登録してパスワードなしでアクセスするをやる。

cd ~
git clone git@github.com:yourrepo/[project].git 
cd [project]

bundle install 
npm install

シンボリックリンク

ln -s ~/[project] /var/www/rails/[project] 

起動

unicorn起動

unicorn_rails -c /var/www/rails/[project]/config/unicorn.conf.rb -D -E staging

nginx起動

sudo service nginx start

まとめ

  • 地味にやることが多い
  • 自動化したい
  • 殴り書きなのでちゃんとまとめたい

以上。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?