LoginSignup
3
6

More than 3 years have passed since last update.

【AWS】デプロイ関連で詰まった箇所

Last updated at Posted at 2020-04-04

デプロイ

bundler: failed to load command: puma

解決法

参考記事

AssetsPrecompile関連エラー

Precompile = 1つのファイルにまとめる(圧縮する作業)

ターミナル
# アセッツプリコンパイル
$ bundle exec rake assets:precompile RAILS_ENV=production

解決法

yarnのインストール

ターミナル
$ curl -o- -L https://yarnpkg.com/install.sh | bash
$ source ~/.bashrc # 反映
$ yarn -V # yarnインストール
config/environments/production.rb
config.assets.js_compressor = :uglifier

# 変更

config.assets.js_compressor = Uglifier.new(harmony: true)
config/environments/production.rb
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?

# 変更

config.public_file_server.enabled = true

capistrano関連エラー

SSHKit::Runner::ExecuteError

config/deploy/production.rb
set :ssh_options, {
  keys: %w(~/.ssh/id_rsa)
}

# 変更(例)

set :ssh_options, {
  keys: %w(/Users/hogehogehoge/.ssh/hogehoge_key_rsa)
}

# ①ssh接続の秘密鍵にはデフォルトでid_rsaを探しにいくため、使用する秘密鍵の名前に変更
# ②使用する秘密鍵の場所までのpathを通す

参考記事

Gem::LoadError : "ed25519 is not part of the bundle. Add it to your Gemfile."

Gemfile
gem 'ed25519'
gem 'bcrypt_pbkdf'
ターミナル
$ bundle

使ってるパソコンの環境によってエラーが出たり出なかったりするらしい。

bundle exec cap production deploy(git:checkで落ちる)

Gemfile
# ssh形式で接続
set :repo_url, "git@github.com:hogehogehoge.git"

# 変更

# https形式で接続
set :repo_url, "https://github.com/hoge/hogehogehoge.git"
set :git_http_username, "gitに登録しているusername"
set :git_http_password, "gitに登録した公開鍵パスワード"

bundle exec cap production deploy(deploy:symlink:linked_filesで落ちる)

master.key database.yml settings.ymlの配置を変更

/var/www/rails/アプリケーション/shared/config配下に移動

bundle exec cap production deploy(yarn:installで落ちる)

nodeのバージョンが古かったらしい

参考記事

bundle exec cap production deploy(nginx:restartで落ちる)

ターミナル
$ ps aux | grep nginx
→プロセスを確認してnginxが起動していないか確認
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] open() "/var/www/rails/アプリケーション名/log/nginx.error.log" failed (2: No such file or directory)
→nginx.conf(nginx設定ファイル)は問題ない
→/var/www/rails.......にnginx.error.logがあるか確認してくださいと言われるが、nginx.error.logは/var/www/rails/アプリケーション名/sheard/log/nginx.error.logに配置したいため/etc/nginx/sites-enabled/アプリケーション名を書き換える
ターミナル
[~]$ cd /etc/nginx/sites-enabled
$ sudo vim アプリケーション名
sites-enabled/アプリケーション名
error_log /var/www/rails/アプリケーション名/sheard/log/nginx.error.log
access_log /var/www/rails/アプリケーション名/sheard/log/nginx.access.log
ターミナル
$ sudo service nginx start
→ 起動確認

bundle exec cap production deploy(全て通ったのにブラウザで画面表示されない)

nginx.error.log
puma.error.log
production.log
を確認してエラーが出ているか確認。

nginx.error.log場所確認

ターミナル
/etc/nginx/sites-enabled/アプリケーション

vimで開いて中身を確認
自分の場合は/var/www/rails/アプリケーション名/shared/log/nginx.error.logに配置

注意))あくまで読み込むのは/etc/nginx/nginx.confであるためnginx.confinclude /etc/nginx/sites-enabled/*のようにincludeされているかを確認。

nginx関連で見るファイルは基本的にnginx.confとsites-enabled配下のファイルの2つ

puma.error.log場所確認

ローカル環境の

config/deploy.rb
set :puma_error_log の後にパスが記述

自分の場合は
/var/www/rails/アプリケーション名/shared/log/puma.error.logに配置

error.logは他の開発者とも共有しやすいようにshared(シェアード)配下に置くのが一般的なのでそのように設定しておくのがベター

production.logshared配下に配置

今回のエラーは/etc/nginx/sites-enabled配下にcapistranoでデプロイ以前に作成した.confファイルが残っておりそちらのファイルがnginx.confinclude
されており、間違った設定が適用されていたことが原因だった。

sites-enabled/.confを削除し、nginx.confに記載のincludeを削除することで解決。

AWSアーキテクチャ関連

複数サーバーにcapistranoでデプロイしたい時

元々あるWebサーバーからイメージの作成、EC2インスタンスを起動し、ローカルの

config/deploy/production.rb
role app, 'ユーザー名@パブリックIP'
role web, 'ユーザー名@パブリックIP'
role db, 'ユーザー名@パブリックIP'

を追加して

ターミナル
$ bundle exec cap production deploy

で、OK

SSL証明書発行(albに付与)

参考記事の7まで進めたら、最下部の新しい ACM 証明書をリクエストをクリック。

*.ドメイン名ドメイン名を記入し、次へ
検証方法はDNS
証明書が認証されるまで待って、リスナー画面へ戻り、発行された証明書を選択して保存

※ UdymyAWS:ゼロから実践するAmazon Web Services。手を動かしながらインフラの基礎を習得のセクション8の62.CloudFrontを設定して高速化しようが参考になる。

ローカルのconfig/deploy/templates/nginx_conf.erb
# bundle exec rails g capistrano:nginx_puma:configをした時に自動生成されるファイル
# デプロイすると、このファイルを元にEC2インスタンスの/etc/nginx/nginx.confや/etc/nginx/sites-enabled/アプリケーションに設定が反映される

location @puma_<%= fetch(:nginx_config_name) %> {
...

<% if fetch(:nginx_use_ssl) -%>
    proxy_set_header X-Forwarded-Proto https;
<% else -%>
    proxy_set_header X-Forwarded-Proto http;
<% end -%>

↓ 変更

proxy_set_header X-Forwarded-Proto https;

この作業をしないとPOSTリクエストがhttpsの時はできないので要注意
https://www.cotegg.com/blog/?p=1850

Git

プッシュ済みコミット取り消し、ローカルの状態をプッシュ

プッシュ済みコミット取り消し

ターミナル
$ git log
戻したいコミットの地点のIDをコピー
$ git reset --hard ID

ローカルの状態をプッシュ
リモートの方がコミットが進んでるためrejectするので

ターミナル
$ git push -f origin master
ローカルの状態を最新としてリモートを更新

チーム開発時の -fは要注意

3
6
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
3
6