Last active
August 8, 2016 04:36
-
-
Save SanoHiroshi/99e437d926f9f3e6297e to your computer and use it in GitHub Desktop.
Capistrano3 + Rails4 + Unicorn + Nginx + EC2でサーバー構築! ref: http://qiita.com/SanoHiroshi/items/d7942d66678f0d60f0ed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'capistrano/setup' | |
require 'capistrano/deploy' | |
require 'capistrano/rails' | |
require 'capistrano/rbenv' | |
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ここをEC2のサーバー側で作ったMysqlのユーザー名、パスワード、ホスト名を書く | |
production: | |
adapter: mysql2 | |
encoding: utf8 | |
pool: 5 | |
username: EC2で作ったmysqlユーザー | |
password: EC2で作ったユーザーのパスワード | |
host: EC2のElasticIP | |
database: あなたが作ったデータベース名 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set :application, "あなたのアプリ名" | |
set :repo_url, 'あなたのgitのレポジトリ名'#gitからコードをcloneする | |
set :branch, 'master' #マージ前なら他のブランチでも設定可能 | |
set :deploy_to, '/var/www/EC2で作ったディレクトリ名' | |
set :keep_releases, 5 #何個アプリを確保しておくか。この場合はデプロイした最新のアプリ5個をキープ | |
set :rbenv_type, :user | |
set :rbenv_ruby, '2.0.0-p576' #rubyのバージョン間違えないように! | |
set :rbenv_map_bins, %w{rake gem bundle ruby rails} | |
set :rbenv_roles, :all | |
set :linked_dirs, %w{bin log tmp/backup tmp/pids tmp/cache tmp/sockets vendor/bundle} | |
after 'deploy:publishing', 'deploy:restart' | |
namespace :deploy do | |
task :restart do | |
invoke 'unicorn:restart' | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config.secret_key = 'terminalのエラーで追加するように言われたキー' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Host インスタンスのIPアドレス | |
Hostname インスタンスのIPアドレス | |
User サーバーで作ったユーザー | |
Port 22 | |
PasswordAuthentication no | |
IdentityFile ~/.ssh/ダウンロードしたファイル名.pem | |
IdentitiesOnly yes | |
ForwardAgent yes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mysql> GRANT ALL PRIVILEGES ON *.* TO '任意のユーザー名'@'設定したElasticIP' IDENTIFIED BY 'パスワード決める' WITH GRANT OPTION; | |
mysql> FLUSH PRIVILEGES; | |
作れたか確認 | |
mysql> select Host, User, Password from mysql.user; | |
データベース作成 | |
mysql> CREATE DATABASE 任意のデータベース名 CHARACTER SET utf8; | |
作れたか確認 | |
mysql> show databases; | |
終了 | |
mysql> quit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
group :production, :staging do | |
gem 'unicorn' | |
end | |
group :development do | |
gem 'capistrano', '~> 3.2.1' | |
gem 'capistrano-rails', '~> 1.1', require: false | |
gem 'capistrano-bundler', '~> 1.1', require: false | |
gem 'capistrano-rbenv', '~> 2.0', require: false | |
gem 'capistrano3-unicorn' | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
events { | |
worker_connections 2048; | |
} | |
#通信情報 | |
http { | |
#最新のアプリはcapistranoがcurrentに最新のデプロイしたアプリのシンボリックリンクを貼るのでそこを指定すればOK | |
root /var/www/あなたのアプリ名/current; | |
#unicornに必要 | |
#http{}の中に記述しないと動かない | |
upstream unicorn-server { | |
server unix:/var/www/あなたのアプリ名/shared/tmp/sockets/unicorn.sock | |
fail_timeout=0; | |
} | |
#unicorn-serverという名前は任意でOK。プロキシで設定する名前と同じなら大丈夫 | |
#サーバー情報 | |
server { | |
listen 80; | |
client_max_body_size 4G; | |
server_name あなたのEC2のpublic IP; | |
keepalive_timeout 80; | |
#ログ関係は設定しないとデバッグが不可なので必須(下記はデフォルトのフォルダを指定しているので作成する必要はない) | |
access_log /var/log/nginx/access.log; | |
error_log /var/log/nginx/error.log; | |
root /var/www/あたなのアプリ名/current; | |
#本番環境ではrailsのpublic以下のassetが使用されるのでそこを指定 | |
location ~ ^/assets/ { | |
include /etc/nginx/mime.types; | |
root /var/www/あなたのアプリ名/current/public; | |
} | |
location / { | |
proxy_pass http://unicorn-server;#ここのunicorn-serverという名前をupstreamと合わせる必要がある | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
proxy_set_header Host $host; | |
} | |
#エラー画面の場所(railsのディレクトリを指定する) | |
error_page 500 502 503 504 /500.html; | |
location = /500.html { | |
root /var/www/あなたのアプリ名/current/public; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set :stage, :production | |
set :rails_env, 'production' | |
server 'EC2のElasticIP', user: 'EC2で作成したユーザー名(rootのユーザー。Mysqlとは違うよ)', | |
roles: %w{web app db} #何サーバーの処理を書くか。今回は同じサーバーで全部動かすのでweb app db全て指定 | |
#sshでEC2に入るのに必要 | |
set :ssh_options, { | |
keys: [File.expand_path('~/.ssh/EC2で任意でつけてダウンロードしたキー名.pem)')] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
application = 'reserve-hacker' | |
worker_processes 2 #EC2のインスタンスのCPU数より少し大きく | |
app_path = "/var/www/EC2で作ったディレクトリ名" | |
#標準だとsharedに作成される | |
#ここが一番重要 | |
#Nginxのupstreamで設定した「server unix:/var/www/あなたのアプリ名/shared/tmp/sockets/unicorn.sock」の場所と合わせる!! | |
listen "#{app_path}/shared/tmp/sockets/unicorn.sock" | |
pid "#{app_path}/current/tmp/unicorn.pid" | |
#何秒でタイムアウトするか | |
timeout 60 | |
#ダウンタムをなくす | |
preload_app true | |
stdout_path "#{app_path}/current/log/production.log"# 標準出力ログ出力先 | |
stderr_path "#{app_path}/current/log/production.log"# 標準エラー出力ログ出力先 | |
GC.respond_to?(:copy_on_write_friendly=) and GC.copy_on_write_friendly = true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment