【AWS】EC2を利用してRailsアプリをAWSにデプロイしたい!デプロイ編の2

デプロイする2

1.準備する編
2.サーバー構築編
3.デプロイする編1

とやってきましたが、やっと最後になるようです。

参照:(デプロイ編②)世界一丁寧なAWS解説。EC2を利用して、RailsアプリをAWSにあげるまで - Qiita

Unicornの設定

Unicornとは

アプリケーションサーバーの一種

アプリ本体を格納するためのサーバー

[zerobit|website_app]$ vi Gemfile


group :production, :staging do
 gem 'unicorn'
end


[zerobit|website_app]$ gem install bundler
[zerobit|website_app]$ bundle install
[zerobit|website_app]$ vi config/unicorn.conf.rb


unicorn.conf.rb

# set lets
$worker = 2
$timeout = 30
$app_dir = "/var/www/rails/website_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の設定

[zerobit|~]$ sudo yum install nginx
[sudo] password for zerobit:
 
総ダウンロード容量: 1.2 M
インストール容量: 2.9 M
Is this ok [y/d/N]:y
 
完了しました!
 
[zerobit|~]$ cd /etc/nginx/conf.d/
[zerobit|conf.d]$ sudo vi website_app.conf #自分のアプリケーション名でファイル名変更


website_app.conf

# log directory error_log /var/www/rails/website_app/log/nginx.error.log;
#自分のアプリケーション名に変更
access_log /var/www/rails/website_app/log/nginx.access.log;
#自分のアプリケーション名に変更
# max body size
client_max_body_size 2G;
upstream app_server {
# for UNIX domain socket setups
 server unix:/var/www/rails/website_app/tmp/sockets/.unicorn.sock fail_timeout=0; #自分のアプリケーション名に変更
}
server {
 listen 80;
 server_name 13.231.47.97; #アプリのElastic IPに変更してください
 # nginx so increasing this is generally safe...
 keepalive_timeout 5;
 # path for static files
 root /var/www/rails/website_app/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/website_app/public; #自分のアプリケーション名に変更
 }
}


[zerobit|conf.d]$ cd /var/lib
[zerobit|lib]$ sudo chmod -R 775 nginx

MySQLの設定

アプリケーションのDBがMySQLで作成されている。

[zerobit|website_app]$ vi config/database.yml


production:
 <<: *default
 database: website_production
 username: root #ここをrootに変更する
 password: #ここを空欄にする


[zerobit|website_app]$ sudo service mysqld start #mysqldの起動
Starting mysqld: [ OK ]
 
[zerobit|website_app]$ ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
[zerobit|website_app]$ rake db:create RAILS_ENV=production
Created database 'website_production'
[zerobit|website_app]$ rake db:migrate RAILS_ENV=production

Nginxの起動

[zerobit|website_app]$ sudo service nginx start
Starting nginx: [ OK ]

ブラウザにIPアドレスを入力して起動確認。

f:id:kisokoji:20180404131906p:plain:w500

何か違う?

このページは、インストール後のnginx HTTPサーバーの適切な操作をテストするために使用されます。 このページを読むことができれば、このサイトにインストールされているWebサーバーが正常に動作していることを意味します。 by google先生

成功なのか?

うーんこれ以上がまだわからない。

で、どうするの?がCapistranoにあるのか?