[RUBY] [Personal memo] Summary of stumbling blocks when deploying Rails apps to AWS

index As shown in the article below, I have summarized as a personal reminder what I stumbled upon when deploying a Rails app to AWS.

(Preparation) The world's most polite AWS commentary. Until you bring your Rails app to AWS using EC2 https://qiita.com/naoki_mochizuki/items/f795fe3e661a3349a7ce https://qiita.com/naoki_mochizuki/items/22cfbf4bf7ec95f6ac1c https://qiita.com/naoki_mochizuki/items/814e0979217b1a25aa3e https://qiita.com/naoki_mochizuki/items/5a1757d222806cbe0cd1

Unable to generate RDS instance

When I set up RDS and click "Create Database", this error occurs.

DB Subnet Group doesn't meet availability zone coverage requirement. Please add subnets to cover at least 2 availability zones. Current coverage: 1 (Service: AmazonRDS; Status Code: 400; Error Code: DBSubnetGroupDoesNotCoverEnoughAZs; Request ID: 3e87202c-e6b3-46dc-8396-47c64a2f0dd6)

I referred to this article. https://www.wantanblog.com/entry/2019/09/24/225020 Apparently, it seems that the problem was that only one subnet group was created, so if you create another subnet with the following settings from "VPC" → "Subnet" → "Create subnet", you can create an RDS instance. It became so. ・ VPC ・ ・ ・ Select the created VPC ・ Availability zone ・ ・ ・ Specify a location different from the subnet you have already created. ・ IPv4 CIDR block ・ ・ ・ 10.0.1.0/24

I can't log in to EC2 with SSH

*[ .ssh ] $: ssh -i mumu.pem [email protected]

When I tried to log in to EC2 with, I got a connection error after waiting for a while. I referred to this article. https://xn--o9j8h1c9hb5756dt0ua226amc1a.com/?p=3583 When I checked the route table of the subnet where EC2 is located, the route to the outside (destination 0.0.0.0/0, target igw -...) was not set (inadvertently). After adding the following in "VPC"-> "Route table"-> "Check the corresponding route table"-> "Route table"-> "Edit route table"-> "Add route", SSH connection to EC2 becomes possible. I did. ・ Destination ・ ・ ・ 0.0.0.0/0 ・ Target ・ ・ ・ igw -... (created internet gateway)

Ssh-key gem not found error when creating public key

It was a typo. It was ssh-keygen instead of ssh-key gem.

I can't make a secret key with rake secret

The following error occurs

$ rake secret
You must use Bundler 2 or greater with this lockfile.

I referred to this article. https://programming-beginner-zeroichi.jp/articles/169

$ gem install bundler
$ bundle install
$ bundle exec rake secret

I solved it with. (* By the way, since I set the database of the application to sqlite, an error occurred in "bundle install", and after following the steps in the next article, I did "bundle install".

Error installing sqlite3 when bundle install

I'm planning to use MySQL as a database, so I'll change the rails app database from sqlite to MySQL. I referred to this article. https://note.com/itoa06/n/n31fe4f9cd6b9

Click here for the difference /Gemfile

-gem 'sqlite3', '~> 1.4'
+gem 'mysql2', '>= 0.4.4'

/config/database.yml

 default: &default
-  adapter: sqlite3
+  adapter: mysql2
+  encoding: utf8mb4
   pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
-  timeout: 5000
+  username: root
+  password:
+  host: localhost
 
 development:
   <<: *default
-  database: db/development.sqlite3
+  database: hello_rails_development

 test:
   <<: *default
-  database: db/test.sqlite3
+  database: hello_rails_test
 
 production:
   <<: *default
-  database: db/production.sqlite3
+  database: hello_rails_production
+  username: hello_rails
+  password: <%= ENV['HELLO_RAILS_DATABASE_PASSWORD'] %>

Failed to start mysqld.service: Unit not found. I got an error when I tried to launch MySQL

sudo service mysqld start
Redirecting to /bin/systemctl start mysqld.service
Failed to start mysqld.service: Unit not found.

I referred to this article. https://qiita.com/hamham/items/fd77bb0bb167a150dc8e#mysql57%E3%81%AE%E5%B0%8E%E5%85%A5

As @MurakamiKazutaka wrote in the comment, it seems that Amazon Linux 2 will try to install MariaDB when trying to install mysql with yum.

$ yum -y install http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
$ yum -y install mysql mysql-community-server
$ mysqld --version
mysqld  Ver 5.7.23 for Linux on x86_64 (MySQL Community Server (GPL))
$ cd /var/www/rails/app name
$ sudo service mysqld start

It was solved safely.

I thought it wasn't safe. In this case, MySQL will create the root user password on its own, so you need to put the password in database.yml, as @hat_log said. https://qiita.com/Dough/items/7493ad374a51b24abb58

$ sudo cat /var/log/mysqld.log | grep 'temporary password'
[Note] A temporary password is generated for root@localhost: XXXXXX
$ mysql -u root -p
Enter password: XXXXXX
mysql> set password for root@localhost=password('passwordPASSWORD@999');

Next, enter the password in database.yml

production:
  <<: *default
  database: mumu_production
  username: root
  password: passwordPASSWORD@999

NoMethodError (undefined method `deep_symbolize_keys' for error

An error occurred while creating the database.

$ rake db:create RAILS_ENV=production
...
NoMethodError (undefined method `deep_symbolize_keys' for...

I wondered if the .yml file was malformed and probably didn't have two indented spaces. Then I didn't write the key name secret_key_base: in config / secrets.yml (inadvertently).

Wrong ↓

production:
(Generated secret key)

Correct one ↓

production:
  secret_key_base:(Generated secret key)

Job for nginx.service failed because the control process exited with error code error

I get an error when trying to start Nginx

$ sudo service nginx start
Redirecting to /bin/systemctl start nginx.service
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

I referred to this article. https://qiita.com/shota0701nemoto/items/a6929ef6f396cf3bede4

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] open() "/var/www/rails/[Wrong app name]/log/nginx.error.log" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed

I think the content of the error varies from person to person. I was told that "/var/www/rails/[wrong app name] /log/nginx.error.log" does not exist, so when I moved to the file,

$ cd /var/www/rails/[Wrong app name]/log
-bash: cd: /var/www/rails/[Wrong app name]/log: No such file or directory

When I looked closely, I noticed that I made a mistake in writing the hyphen (-) and underscore (_) in the app name.

$ vim config/unicorn.conf.rb
$ cd /etc/nginx/conf.d/
$ sudo vim mumu.conf

Corrected the wrong part in.

$ cd /var/www/rails/[app name]/
$ sudo service nginx start

It started up safely.

We're sorry, but something went wrong. Part 1

After starting Nginx, when I try to access the IP address of EC2 with Chrome,

We're sorry, but something went wrong.

It became. I thought it was because I stopped RDS once,

$ sudo service mysqld start

I started MySQL, but there is no change.

$ less log/production.log

I checked the Rails log in (the bottom is the latest information),

ActionView::Template::Error (The asset "application.css" is not present in the asset pipeline.

Since the error was displayed, rewrite config / envitonments / production.rb referring to this article. https://kanoe.studio/archives/791

  # Do not fallback to assets pipeline if a precompiled asset is missed.
  config.assets.compile = true

Restart the application server, (I referred to this article https://qiita.com/takuyanagai0213/items/259ca105e35f6eb066d6 )

$ ps -ef | grep unicorn | grep -v grep
takuya 2460 1 0 March 11?      00:00:04 unicorn_rails master -c /var/www/rails/myapp/config/unicorn.conf.rb -D -E production
takuya 2465 2460 0 March 11?      00:00:05 unicorn_rails worker[0] -c /var/www/rails/myapp/config/unicorn.conf.rb -D -E production
takuya 2467 2460 0 March 11?      00:00:04 unicorn_rails worker[1] -c /var/www/rails/myapp/config/unicorn.conf.rb -D -E production
$ kill 2460
$ unicorn_rails -c /var/www/rails/myapp(My app name)/config/unicorn.conf.rb -D -E production

I accessed the EC2 IP address again, but as usual, "We're sorry, but something went wrong." Continued (Is it still an error ...)

We're sorry, but something went wrong. Part 2

When I checked the log again,

$ less log/production.log

ActionView::Template::Error (Webpacker can't find application in /var/www/rails/hello-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.

Error was occurring.

I don't know what you're talking about

I referred to this article. https://qiita.com/natecotus/items/a2bd9f3ebd5b1866d48e

$ rm -rf bin/webpack*
$ rails webpacker:install
Webpacker requires Node.js >= 8.16.0 and you are using 6.17.1
Please upgrade Node.js https://nodejs.org/en/download/

An error occurred again. It seems that the version of Node.js is old. I referred to this article. https://qiita.com/paranishian/items/bddaed7c3aacedb11967

$ git clone git://github.com/creationix/nvm.git .nvm
$ . ~/.nvm/nvm.sh
$ nvm install
$ curl -o- -L https://yarnpkg.com/install.sh | bash
$ source ~/.bashrc
$ yarn -v

Well then, let's change it ~

$ rails webpacker:install
[Ynaqdhm] Y
[Ynaqdhm] Y
$ RAILS_ENV=production bundle exec rails webpacker:compile

It looks like it's compiled, so restart Unicorn and Nginx.

$ ps -ef | grep unicorn | grep -v grep
$ kill [Process ID]
$ unicorn_rails -c /var/www/rails/[app name]/config/unicorn.conf.rb -D -E production
$ sudo nginx -s reload

Now, try connecting with an IP address. .. .. It was displayed!

Recommended Posts

[Personal memo] Summary of stumbling blocks when deploying Rails apps to AWS
Summary of stumbling blocks related to form_with
SCSS doesn't work when deploying Rails6 AWS
Knowledge required to bring Rails apps to AWS
[Rails] [Memo] When to add = to <%%> and when not
Summary of moss when updating from JMockit 1.4 to 1.30
[Ruby on rails + Mysql] Data migration procedure memo when switching from heroku to AWS
Summary of initial work when creating an app with Rails
How to deploy a Rails application on AWS (article summary)
Method definition location Summary of how to check When defined in the project and Rails / Gem
Introduction to RSpec-Everyday Rails Summary-
[Webpacker] Summary of how to install Bootstrap and jQuery in Rails 6.0
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator