4
4

More than 3 years have passed since last update.

初学者がハマったAWS(EC2)へのデプロイ We're sorry, but something went wrong.

Last updated at Posted at 2020-05-09

はじめに

初投稿であり、体裁や伝わりにくい部分があるかと思いますがハマった部分をメモとして、また、同じように困る方の解決の1つとして提供できたら嬉しいです。

環境

  • AWS【EC2(Amazon Linux2)、RDS(Mysql)】
  • Ruby 2.5.1
  • Rails 6.0.2.2
  • nginx 1.12.2
  • unicorn v5.5.5

We're sorry, but something went wrong.の表示

サーバー環境でデプロイしてさあ、ブラウザで確認!・・・おや?
We're sorry, but something went wrong.
調べてみるとエラーログを確認しなさいという記事が見つかる。なのでとりあえず、エラーログの確認

サーバー環境(/var/www/rails/アプリ名/)
$ cd log
$ tail -n 30 production.log

ActionView::Template::Error (Webpacker can't find application in /var/www/rails/アプリ名/public/packs/manifest.json. Possible causes:
1. You want to set webpacker.yml value of compile to true for your environment
   unless you are using the `webpack -w` or the webpack-dev-server.
2. webpack has not yet re-run to reflect updates.
3. You have misconfigured Webpacker's config/webpacker.yml file.
4. Your webpack configuration is not creating a manifest.
Your manifest contains:
{
}
):

7:     <%= csp_meta_tag %>
8:
9:     <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
10:    <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
11:    <link href="https://fonts.googleapis.com/css?family=Lato:400,700|Noto+Sans+JP:400,700" rel="stylesheet">
12:    <link href="https://use.fontawesome.com/releases/v5.6.1/css/all.css" rel="stylesheet">
13:    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
app/views/layouts/application.html.erb:10

エラーの記載があればデプロイは出来ているが、アプリ内でエラーが発生しているそうです。
なければ、デプロイがそもそも出来ていないそうです。
今回のエラーの場合は、プリコンパイルが出来ていない可能性が高い模様。。。

$ bundle exec rake assets:precompile RAILS_ENV=production
このコマンドでプリコンパイルの実行となります。

このコマンドを打つ前にやることがあります。
私は、あることをしていなかったため、長い長い沼にハマりました。
それでは見ていきましょう!

サーバー環境(/var/www/rails/アプリ名/)
1.nginxを止める
$ sudo systemctl stop nginx
2.止まっているか確認
$ systemctl status nginx.service
3.unicornのプロセスIDを調べる
$ ps aux | grep unicorn
4.killする
$ kill プロセスID
5.一応切れているか確認
$ ps aux | grep unicorn
6.プリコンパイルを実行
$ bundle exec rake assets:precompile RAILS_ENV=production
7.nginxの起動
$ sudo systemctl start nginx
$ sudo systemctl enable nginx
$ sudo systemctl status nginx.service
8.unicornの起動
$ bundle exec unicorn_rails -c /var/www/rails/アプリ名/config/unicorn.conf.rb -D -E production

これで私は解決しました!
大事なのは最初にnginxとunicornを止める事です!
これ、多分当たり前だろと思われると思います。が、知識の乏しい私はにとっては当たり前ではありませんでした。。笑
ずっとこれをやっていなかったがために、ひたすらに調べて出てこない、なんなんだとだいぶ考え込みいろんな方法を試していました。

こちらの記事に沿って作業しています。
【画像付きで丁寧に解説】AWS(EC2)にRailsアプリをイチから上げる方法【その4〜Railsアプリの公開編〜】

まとめ

初歩的な所で躓くような私ですが、プログラミングは楽しいという思いがあるので、これからも尽きることのない学びを続けたいと思います。前を向いて精進あるのみです。
役に立ったよという方がいればLGTMをください!
初投稿でしたが、書き下ろすことで整理がつくのでこれからも書きたいと思いました。

4
4
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
4
4