AWS is an abbreviation for Amazon Web Servises, which is a cloud server service provided by Amazon. We will output what we have learned in order to improve our understanding of AWS.
It is assumed that you have created an instance.
① Install Unicorn in your own application
Add the following description to the Gemfile in the application.
Gemfile
group :production do
gem 'unicorn', '5.4.1'
end
Terminal
% bundle install
② Unicorn settings
Create unicorn.rb in your application's config. Then, make the following settings.
config/unicorn.rb
#Put the directory where the application code on the server is installed in a variable
app_path = File.expand_path('../../', __FILE__)
#Determine application server performance
worker_processes 1
#Specify the directory where the application is installed
working_directory app_path
#Specify the location of the files required to start Unicorn
pid "#{app_path}/tmp/pids/unicorn.pid"
#Specify the port number
listen 3000
#Specify a file to log errors
stderr_path "#{app_path}/log/unicorn.stderr.log"
#Specify the file to record the normal log
stdout_path "#{app_path}/log/unicorn.stdout.log"
#Set maximum time to wait for Rails application response
timeout 60
#The following is an applied setting, so the explanation is omitted.
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 # prevent from firing again
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
Push it to the remote repository and reflect it.
③ Install the application on the instance
Terminal
Log in to your instance
% ssh -i key pair name.pem ec2-user@<Public IP>
In the instance/var/www/Creating a directory
[instance]$ sudo mkdir /var/www/
Ec2 permissions on the created www directory-Granted to user
[instance]$ sudo chown ec2-user /var/www/
Go to www directory
[instance]$ cd /var/www/
#Clone application from GitHub
[Instance www]$ git clone <HTTPS URL obtained from the Code button of the application you want to deploy on GitHub>
④ Install Gem
Terminal
Move to your home directory in your instance
[Instance www]$ cd ~
Swap file settings
[instance]$ sudo dd if=/dev/zero of=/swapfile1 bs=1M count=512
[instance]$ sudo chmod 600 /swapfile1
[instance]$ sudo mkswap /swapfile1
[instance]$ sudo swapon /swapfile1
[instance]$ sudo sh - -c 'echo "/swapfile1 none swap sw 0 0" >> /etc/fstab'
Terminal
Go to the application directory in your instance
[instance]$ cd /var/www/<Application name>
Install bundler(The version of bundler is "command" in the terminal+Create another tab with "T" and "bundler" in the newly created tab-You can check it by executing "v")
[instance<Application name>]$ gem install bundler -v <bundler version>
Install Gem
[instance<Application name>]$ bundle install
⑤ Environment variable settings
For security reasons, database passwords etc. are not uploaded to GitHub. Such information is set using "environment variables".
A character string used to encrypt cookies. Required for running Rails applications in production.
Terminal
[instance<Application name>]$ rake secret
=> secret_key_The character string that becomes the base is displayed
Move to your home directory within the instance
[instance<Application name>]$ cd ~
/etc/Edit environment
[instance]$ sudo vim /etc/environment
Type "i" to enter input mode
Enter the password of the root user of the configured database
DATABASE_PASSWORD='Database root user password'
SECRET_KEY_BASE='The secret created earlier_key_base'
* If you are using AWS S3, enter the following (enter the value referring to the CSV file downloaded when setting S3)
AWS_ACCESS_KEY_ID='Copy the value of Access key ID in the CSV file here'
AWS_SECRET_ACCESS_KEY='Copy the value of Secret access key in the CSV file here'
* If you have introduced Basic authentication, enter the following (enter the set user name and password)
BASIC_AUTH_USER='Set user name'
BASIC_AUTH_PASSWORD='Password set'
「:Save as "wq"
Log out of the instance and log in again for the environment variables to take effect
[instance]$ exit
% ssh -i key pair name.pem ec2-user@<Public IP>
Checking environment variables
[Instance re]$ env
=>OK when the settings set above are displayed.
⑥ Release the port to the security group
In the AWS Management Console, add "Port 3000" and "Source 0.0.0.0/0" of "Custom TCP" to the inbound rule of the security group of the instance.
⑦ Change Rails application settings
You must also set the environment variables you set in your instance above in your application.
:config/database.yml
production:
<<: *default
database:(* Do not edit here)
username: root
password: <%= ENV['DATABASE_PASSWORD'] %>
socket: /var/lib/mysql/mysql.sock
After changing the settings, push to GitHub.
Then let the instance reflect the changes in your application.
Terminal
Move to the application directory within the instance
[instance]$ cd /var/www/<Application name>
Reflect application setting changes
[instance<Application name>]$ git pull origin master
⑧ Create database (if database server exists in instance)
Please refer to here for how to create a database server https://qiita.com/daisuke30x/items/7501d724a0727ad9f2e4
Terminal
Creating a database
[instance<Application name>]$ rails db:create RAILS_ENV=production
[instance<Application name>]$ rails db:migrate RAILS_ENV=production
Start database
[instance<Application name>] $ sudo systemctl start maradb
RAILS_ENV = production is an option to specify the production environment
⑨ Start application
Terminal
[instance<Application name>]$ bundle exec unicorn_rails -c config/unicorn.rb -E production -D
Compile your application's asset files (CSS, JavaScript, images, etc.). This is a necessary task in a production environment.
Terminal
[instance<Application name>]$ rails assets:precompile RAILS_ENV=production
Terminal
Check the process
[instance<Application name>]$ ps aux | grep unicorn
=>The second number from the left is the process ID
Process stop
[instance<Application name>]$ kill <unicorn_Rails master process ID>
Launch the application again
[instance<Application name>]$ RAILS_SERVE_STATIC_FILES=1 unicorn_rails -c config/unicorn.rb -E production -D
"RAILS_SERVE_STATIC_FILES = 1" is the role that specifies that the compiled asset file can be found.
** Enter "http: //
We hope that this post will help beginners review.
Recommended Posts