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