Try deploying Rails app to EC2-Part 2 (Deploy)-

About this article

We will proceed on the premise of the contents of the first part. Please create a GitHub repository in advance. This time, we will deploy a Rails application using EC2.

(Part 1) https://qiita.com/kotobuki562/items/92827bcf99d89b350445 (Part 2-Database construction-) https://qiita.com/kotobuki562/items/92827bcf99d89b350445

Register SSH key on GitHub

First, let's register the SSH key to clone the GitHub repository to EC2. If you don't register your SSH key, EC2 won't be able to connect to GitHub.

Execute the following command in EC2. When you execute it, you may be asked for various inputs. In that case, press "Enter". If successful, something like a figure such as "RSA 4096" will be output.

[ec2-user@ip-111-11-11-111 ~]$ ssh-keygen -t rsa -b 4096

Let's take a look at the contents of the SSH key with the cat command. Execute the following command.

[ec2-user@ip-111-11-11-111 ~]$ cat ~/.ssh/id_rsa.pub

Then go to GirHub and register your SSH key Access from the URL below and copy the SSH key confirmed by the cat command earlier. Please give any name you like to the title of the SSH key on GitHub side. https://github.com/settings/keys

After registering the key, execute the following command. After execution, you will be asked yes / no, so please wash yes. After that, access is completed.

[ec2-user@ip-111-11-11-111 ~]$ ssh -T [email protected]

Application server

This time, we will create an application server using "unicorn". If you haven't installed the unicorn Gem yet, install it first.

This time we will build in a production environment.

.Gemfile


group :production do
  gem unicorn
end

Next, create unicorn.rb in config and write it as follows. I will describe it as a sample, but you can set it yourself.

config/unicorn.rb



app_path = File.expand_path('../../', __FILE__)

worker_processes 1

working_directory app_path

pid "#{app_path}/tmp/pids/unicorn.pid"

listen 3000

stderr_path "#{app_path}/log/unicorn.stderr.log"

stdout_path "#{app_path}/log/unicorn.stdout.log"

timeout 60

preload_app true
GC.respond_to?(:copy_on_write_friendly=) && GC.copy_on_write_friendly = true

check_client_connection false

run_once = true

before_fork do |server, worker|
  defined?(ActiveRecord::Base) &&
    ActiveRecord::Base.connection.disconnect!

  if run_once
    run_once = false
  end

  old_pid = "#{server.config[:pid]}.oldbin"
  if File.exist?(old_pid) && server.pid != old_pid
    begin
      sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
      Process.kill(sig, File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH => e
      logger.error e
    end
  end
end

after_fork do |_server, _worker|
  defined?(ActiveRecord::Base) && ActiveRecord::Base.establish_connection
end

Clone your GitHub repository

Next, let's create a directory for cloning from GitHub. After creating the directory, we will pass the permissions to the EC2 user, so

[ec2-user@ip-111-11-11-111 ~]$ sudo mkdir /var/www/
[ec2-user@ip-111-11-11-111 ~]$ sudo chown ec2-user /var/www/

Clone GitHub to the directory you created. Go to the directory and run the git clone command. Finally, let's apply the gem with bundle install.

[ec2-user@ip-111-11-11-111 ~]$ cd /var/www/
[ec2-user@ip-111-11-11-111 www ~]$git clone paste the copied URL
[ec2-user@ip-111-11-11-111 app ~]$ bundle install

Next, set the environment variables. Execute the following command. Make a copy of it when it comes out.

[ec2-user@ip-111-11-11-111 <Repository name> ~]$ rake secret

Next, we will actually describe the environment variables. Let's execute the following command and describe it.

[ec2-user@ip-111-11-11-111 ~]$ sudo vim /etc/environment

Set the database environment variables set in the previous chapter. Let's describe the following two points

DATABASE_PASSWORD='Database root user password'
SECRET_KEY_BASE='The secret created earlier_key_base'

Now you are ready.

EC2 port open

Let's go back to EC2 on the web and open the port. Release the port number 3000 of unicorn.rb described earlier. The setting method is described in the first part, so please check there. Please set as shown in the image below.

スクリーンショット 2020-11-11 12.01.11.png

Deploy

First, edit the local database.yml. Please edit as follows. Do not edit the database.

config/database.yml


production:
  <<: *default
  database: app_production
  username: root
  password: <%= ENV['DATABASE_PASSWORD'] %>
  socket: /var/lib/mysql/mysql.sock

Let's go back to EC2 on the terminal and do a git pull.

[ec2-user@ip-111-11-11-111 <Repository name>] git pull origin master

Let's create and migrate a database in EC2. You are now ready.

[ec2-user@ip-111-11-11-111 <Repository name>]$ rails db:create RAILS_ENV=production
[ec2-user@ip-111-11-11-111 <Repository name>]$ rails db:migrate RAILS_ENV=production

Now let's reflect the assets directory with assets: orecompile. Finally, start the server with bundle exec unicorn_rails.

[ec2-user@ip-111-11-11-111 <Repository name>]$ rails assets:precompile RAILS_ENV=production
[ec2-user@ip-111-11-11-111 <Repository name>]$ bundle exec unicorn_rails -c config/unicorn.rb -E production -D

Execute the following command to confirm that the startup has been confirmed.

[ec2-user@ip-111-11-11-111 <Repository name>]$ ps aux | grep unicorn

#The following process is output.

ec2-user  5879  0.0 13.0 515244 131640 ?Sl November 09 0:02 unicorn_rails master -c config/unicorn.rb -E production -D
ec2-user  5887  0.0 12.6 525216 127600 ?Sl November 09 0:02 unicorn_rails worker[0] -c config/unicorn.rb -E production -D
ec2-user 15917  0.0  0.0 119436   976 pts/0    S+   03:26   0:00 grep --color=auto unicorn

To stop it, write the following command.

[ec2-user@ip-111-11-11-111 <Repository name>]$ kill 5879 #Stop process ID

After killing the process, execute the following command and check if it is actually reflected in the browser. You can access it at http: // : 3000 /.

[ec2-user@ip-111-11-11-111 <Repository name>]$ RAILS_SERVE_STATIC_FILES=1 unicorn_rails -c config/unicorn.rb -E production -D

Error log

Finally, if an error occurs after deployment, execute the following command and check the error log. Let's access the error log storage location described earlier in unicorn.rb.

[ec2-user@ip-111-11-11-111 <Repository name>]$ less log/unicorn.stderr.log

Recommended Posts

Try deploying Rails app to EC2-Part 2 (Deploy)-
Try deploying a Rails app on EC2-Part 1-
How to deploy jQuery in your Rails app using Webpacker
How to deploy jQuery on Rails
How to deploy Bootstrap on Rails
Try to create a server-client app
Deploy Rails on Docker to heroku
Deploy your Rails app on Heroku
Deploy Rails apps to Azure App Service using Docker & Continuous Deployment
How to change app name in rails
I tried to introduce CircleCI 2.0 to Rails app
Deploy to heroku with Docker (Rails 6, MySQL)
Upload Rails app image file to S3
Deploy to Heroku [Ruby on Rails] Beginner
Introduced Vuetify to an existing Rails app
Try App Clips
Downgrade an existing app created with rails 5.2.4 to 5.1.6
Android app: Try to summarize events and listeners
Rails Tutorial Chapter 1 From Zero to Deployment [Try]
How to deploy
Deploy Rails to ECS Fargate with AWS Copilot
Error deploying rails5 + Mysql to heroku with Docker-compose
Unify the Rails app time zone to Japan time
Try to summarize the common layout with rails
[Introduction] Try to create a Ruby on Rails application
How to push an app developed with Rails to Github
Deploy to Ruby on Rails Elastic beanstalk (Environment construction)
[Rails] Create sitemap using sitemap_generator and deploy to GAE
How to get started with creating a Rails app
Try to implement tagging function using rails and js
Try to release gem
Steps to deploy to Heroku
How to write Rails
Introducing CircleCI to Rails
Introducing Bootstrap to Rails 5
Introducing Bootstrap to Rails !!
Rails deploy with Docker
Introduce Vue.js to Rails
Steps to deploy Struts 2.5.8
Heroku app moving (rails)
Deploy RAILS on EC2
How to uninstall Rails
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
Deploy to Ruby on Rails Elastic beanstalk (IAM permission change)
[Rails] I tried to create a mini app with FullCalendar
I want to push an app made with Rails 6 to GitHub
How to deploy a simple Java Servlet app on Heroku
[Java] Deploy the Spring Boot application to Azure App Service
How to specify db when creating an app with rails
How to deploy a kotlin (java) app on AWS fargate
Confirm public key ~ Register key on Github ~ Push Rails app to Github
How to deploy a Rails application on AWS (article summary)