I will describe the procedure to publish the application using AWS. This article introduces Nginx, a web server.
Nginx is a type of web server. Performs static content retrieval processing in response to user requests, and requests application server to generate dynamic content.
Execute the following command and change to the ".ssh" directory.
cd ~/.ssh/
Execute the following command to access the EC2 instance with ssh. (When the downloaded pem file name is "xxx.pem" and the Elastic IP is 12.345.67.890)
ssh -i xxx.pem [email protected]
Execute the following command to install Nginx.
sudo yum -y install nginx
Execute the following command and edit the Nginx configuration file using vim. Files under the / etc directory cannot be read, written, or saved without permission, so execute with sudo.
sudo vim /etc/nginx/conf.d/rails.conf
Edit rails.conf as follows. This time, we will use the case where the application name is "testapp" and the Elastic IP is "12.345.67.890" as an example.
rails.conf
upstream app_server {
#Settings for linking with Unicorn.
server unix:/var/www/testapp/tmp/sockets/unicorn.sock;
}
# {}The part surrounded by is called a block. Can set the server
server {
#The port number on which this program accepts connections
listen 80;
#Request URL to accept connection Cannot be accessed with URL not written here
server_name 12.345.67.890;
#Set the maximum size of files uploaded from the client to 2 giga. The default is 1 mega, so keep it large
client_max_body_size 2g;
#The root directory when the connection came
root /var/www/testapp/public;
#assets file(CSS and JavaScript files, etc.)Settings applied when access comes to
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri @unicorn;
location @unicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
error_page 500 502 503 504 /500.html;
}
Execute the following command to change Nginx permissions. By changing the authority here, the error will not appear in the POST method.
cd /var/lib
sudo chmod -R 775 nginx
The "-R" option is an option for recursive changes. That is, change the permissions of that directory and all files in that directory.
One of the requests sent from the client to the Web server by HTTP communication, for sending data from the client to the program specified by the URL. Used to send large data and files to the server.
Execute the following command, restart Nginx, and reload the configuration file.
cd ~
sudo service nginx restart
Modify the unicorn settings again to process via Nginx.
Edit config / unicorn.rb in the development environment as follows. After editing, don't forget to commit and push.
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 "#{app_path}/tmp/sockets/unicorn.sock"
#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
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
Execute the following command to reflect the edited contents in the production environment.
cd /var/www/testapp(When the repository name of the application is "testapp")
git pull origin master
Execute the following command to check the process ID of unicorn master.
ps aux | grep unicorn
Execute the following command to kill the confirmed process ID. (Here, the process ID of unicorn master is 17877)
kill 17877
Execute the following command to start unicorn.
RAILS_SERVE_STATIC_FILES=1 unicorn_rails -c config/unicorn.rb -E production -D
Access the application with Elastic IP from your browser. (This time: You don't have to add 3000) At this time, unicorn must be running.
--If you get an error saying 502 bad gateway, check /var/log/nginx/error.log --Check /var/www/testapp/log/unicorn.stderr.log for any errors (if the repository name is testapp) --Is rails running? --Restart the EC2 instance (requires a restart of MySQL and Nginx in the production environment)
Receives a request sent from the outside and adds processing. Nginx is a type of web server. It has the following roles.
--Return static content as a response to the client --Request the application server to generate dynamic content --Return the processing result returned from the application server to the client as a response
Static content is a file whose content does not change with each request. Css and image files that have a fixed display. Dynamic content is a file whose content changes with each request. Files that acquire and display data that meets the search conditions from the database.
Generate dynamic content and return the processing result to the Web server. Unicorn is a type of application server. It has the following roles.
--Generate dynamic content based on the information requested by the web server --Return the processing result to the Web server
Procedure to publish application using AWS (1) Create AWS account Procedure to publish application using AWS (2) Create EC2 instance [How to publish an application using AWS (3) EC2 instance environment construction] (https://qiita.com/osawa4017/items/8dc09203f84e04bf0e66) [Procedure to publish application using AWS (4) Create database] (https://qiita.com/osawa4017/items/7dba25f4fa30ab0b1246) [Procedure to publish application using AWS (5) Publish application] (https://qiita.com/osawa4017/items/6f3125fcc21f73024311)
Recommended Posts