AWSに入って手動でデプロイしたときのコマンドです。
前提
- EC2インスタンス作成済
- RDS DB(mysql)作成済
# updateして必要なものを入れる
sudo yum -y update
sudo yum install -y git curl unzip gcc openssl-devel readline-devel mysql-devel
# git config
git config --global user.name "xxxxxx"
git config --global user.email "xxxxxxxx@email.com"
# timezone
sudo timedatectl set-timezone Asia/Tokyo
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
~/.rbenv/bin/rbenv init
mkdir -p "$(rbenv root)"/plugins
git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
rbenv install 2.6.6
rbenv global 2.6.6
gem install bundler
sudo curl -sL https://rpm.nodesource.com/setup_12.x | sudo bash -
sudo yum install -y nodejs
git clone https://github.com/branch_name/xxx.git
bundle install --without test development
bin/rails db:migrate RAILS_ENV=production
bundle exec rake assets:precompile RAILS_ENV=production
bin/rails s -e production
bin/rails db -e production
vi Gemfile # uniron gem 追加
bundle install
vi config/unicorn.conf.rb
# ==========================================
# set lets
$worker = 2
$timeout = 30
$app_dir = "/home/ec2-user/branch_name" <= edit
$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
# ==========================================
sudo amazon-linux-extras install nginx1.12
nginx -v
cd /etc/nginx/conf.d/
cd /var/lib
sudo chmod -R 775 nginx
sudo service nginx start
cd /etc/nginx/conf.d/
sudo vi default.conf
# ==========================================
error_log /home/ec2-user/branch_name/log/nginx.error.log; <= edit
access_log /home/ec2-user/branch_name/log/nginx.access.log; <= edit
# max body size
client_max_body_size 2G;
upstream app_server {
# for UNIX domain socket setups
server unix:/home/ec2-user/branch_name/tmp/sockets/.unicorn.sock fail_timeout=0; <= edit
}
server {
listen 80;
server_name IPアドレス; <= edit
# nginx so increasing this is generally safe...
keepalive_timeout 5;
# path for static files
root /home/ec2-user/branch_name/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 /home/ec2-user/branch_name/public; <= edit
}
}
# ==========================================
# 構文検査
nginx -t
sudo service nginx start
sudo nginx -s reload
sudo chmod -R +r /home/ec2-user/branch_name/public
sudo chmod -R +rwx /home/ec2-user/branch_name/tmp
sudo nginx -s reload
bundle exec unicorn_rails -c /home/ec2-user/circleci_study/config/unicorn.conf.rb -D -E production
ps -ef | grep unicorn | grep -v grep
chmod 701 /home/ec2-user
最後の方でずっと「forbidden 403」権限エラーが出ていて、すぐにはわかりませんでした。
権限つけたら見ることができるようになりました。
参考文献
個人開発のための Webサービス公開マニュアル - 秀和システム あなたの学びをサポート
難波 聖一 著
秀和システム (2019/12/25)
415ページ
参考ページ
(デプロイ編②)世界一丁寧なAWS解説。EC2を利用して、RailsアプリをAWSにあげるまで - Qiita