質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
unicorn

Unicornは、汎用のRackアプリケーションサーバ。RackとWebサーバーの機能を併せ持ちます。レスポンス処理や、Nginx単体がRackの機能をサポートしていない事から、一般的にはNginx+Unicorn+Railsの構成を取って用います。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Q&A

解決済

1回答

1101閲覧

AWS、unicornを用いてrailsアプリを本番環境にデプロイしたいです。

anago1030

総合スコア1

unicorn

Unicornは、汎用のRackアプリケーションサーバ。RackとWebサーバーの機能を併せ持ちます。レスポンス処理や、Nginx単体がRackの機能をサポートしていない事から、一般的にはNginx+Unicorn+Railsの構成を取って用います。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

0グッド

0クリップ

投稿2020/09/04 20:20

前提・実現したいこと

Ruby on railsで作成しているアプリをAWSというクラウドサービスを用いて本番環境にデプロイしたいと考えています。

前提
・rails db:create RAILS_ENV=productionでデータベース作成済み
・rails db:migrate RAILS_ENV=productionでマイグレーション済み
・rails assets:precompile RAILS_ENV=productionでアセットコンパイル済み

発生している問題・エラーメッセージ

[ec2-user@ip-172-31-8-78 highlight]$ ps aux | grep unicorn ec2-user 1044 0.0 0.0 119420 920 pts/0 R+ 19:55 0:00 grep --color=auto unicorn [ec2-user@ip-172-31-8-78 highlight]$ RAILS_SERVE_STATIC_FILES=1 unicorn_rails -c config/unicorn.rb -E production -D master failed to start, check stderr log for details [ec2-user@ip-172-31-8-78 highlight]$

該当のソースコード

database.yml

1# MySQL. Versions 5.5.8 and up are supported. 2# 3# Install the MySQL driver 4# gem install mysql2 5# 6# Ensure the MySQL gem is defined in your Gemfile 7# gem 'mysql2' 8# 9# And be sure to use new-style password hashing: 10# https://dev.mysql.com/doc/refman/5.7/en/password-hashing.html 11# 12default: &default 13 adapter: mysql2 14 encoding: utf8 15 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 16 username: root 17 password: 18 socket: /tmp/mysql.sock 19 20development: 21 <<: *default 22 database: highlight_development 23 24# Warning: The database defined as "test" will be erased and 25# re-generated from your development database when you run "rake". 26# Do not set this db to the same as development or production. 27test: 28 <<: *default 29 database: highlight_test 30 31# As with config/credentials.yml, you never want to store sensitive information, 32# like your database password, in your source code. If your source code is 33# ever seen by anyone, they now have access to your database. 34# 35# Instead, provide the password as a unix environment variable when you boot 36# the app. Read https://guides.rubyonrails.org/configuring.html#configuring-a-database 37# for a full rundown on how to provide these environment variables in a 38# production deployment. 39# 40# On Heroku and other platform providers, you may have a full connection URL 41# available as an environment variable. For example: 42# 43# DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase" 44# 45# You can use this database configuration with: 46# 47# production: 48# url: <%= ENV['DATABASE_URL'] %> 49# 50production: 51 <<: *default 52 database: highlight_production 53 username: root 54 password: <%= ENV['DATABASE_PASSWORD'] %> 55 socket: /var/lib/mysql/mysql.sock 56

試したこと

master failed to start, check stderr log for detailsというエラー文より
unicornのエラーログを確認しました。

I, [2020-09-04T07:58:53.515723 #29314] INFO -- : Refreshing Gem list
I, [2020-09-04T07:58:54.395771 #29314] INFO -- : listening on addr=0.0.0.0:3000 fd=9
E, [2020-09-04T07:58:54.396194 #29314] ERROR -- : Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) (Mysql2::Error::ConnectionError)

上記のエラー内容を確認したので、本番環境でインストールするデータベースの設定がローカルとは異なるため、接続できなくなっている状態であると仮説を立てました。

database.ymlの中身を
production:
<<: *default
database: highlight_production
username: root
password: <%= ENV['DATABASE_PASSWORD'] %>
socket: /var/lib/mysql/mysql.sock

と記述し、次にps aux | grep unicornでunicornのプロセスを確認しても
ec2-user 17877 0.4 18.1 588472 182840 ? Sl 01:55 0:02 unicorn_rails master -c config/unicorn.rb -E production -D
ec2-user 17881 0.0 17.3 589088 175164 ? Sl 01:55 0:00 unicorn_rails worker[0] -c config/unicorn.rb -E production -D
ec2-user 17911 0.0 0.2 110532 2180 pts/0 S+ 02:05 0:00 grep --color=auto unicorn

のように表示されずに、「unicorn_rails master」と表示されているプロセスが確認できません。これはunicornが起動できていないのでしょうか?
起動しようとして、RAILS_SERVE_STATIC_FILES=1 unicorn_rails -c config/unicorn.rb -E production -D
コマンドをターミナルで打っても、master failed to start, check stderr log for detailsと表示されてしまいます。

対処法を教えていただきたいです。初の質問投稿なので記述が冗長に感じさせてしまったら申し訳ありません。よろしくお願いいたします。

補足情報(FW/ツールのバージョンなど)

開発環境
・macOS Catalina バージョン10.15.6
・VSCODE
・Rails 6.0.0
・Ruby 2.6.5
・mariaDB
・Application Server Unicorn
・Web Server Nginx
・capistrano

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

ps aux | grep unicornでunicornが起動できていないので、
bundle exec unicorn_rails -c /アプリのあるディレクトリ/config/unicorn.conf.rb -D -E production
での結果を教えてください。

また、DBのproduction環境のpasswordを直書きしてみてください。

投稿2020/09/05 15:26

murakmixi

総合スコア26

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

anago1030

2020/09/06 08:30

murakmixiさんご回答ありがとうございました! bundle exec unicorn_rails -c /アプリのあるディレクトリ/config/unicorn.conf.rb -D -E production を実行した結果unicornを起動させることができました! 本当に助かりました! ベストアンサーにさせていただきます。
murakmixi

2020/09/08 13:11

よかったです。 railsライフを楽しんでください。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問