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.
"How to create an application server on an EC2 instance of AWS" (https://qiita.com/daisuke30x/items/4292f96d817805f93d62) It will be explained from the continuation of. In other words, it is assumed that the application is installed on the instance.
This is a typical web server.
① Install Nginx in your own application
Terminal
Log in to your instance
% ssh -i key pair name.pem ec2-user@<Public IP>
Nginx installation
[instance]$ sudo amazon-linux-extras install nginx1
=> Is this ok [y/d/N]: y
Nginx settings
[instance]$ sudo vim /etc/nginx/conf.d/rails.conf
Press "i" and enter the following (two places<Application name>And one place<Elastic IP>(Enter your own)
:/etc/nginx/conf.d/rails.conf
upstream app_server {
#Settings for linking with Unicorn
server unix:/var/www/<Application name>/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 <Elastic IP>;
#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/<Application name>/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;
}
Terminal
After entering the above, press the esc key and press ":Enter wq and save
Change Nginx permissions
[instance]$ cd /var/lib
[Instance lib]$ sudo chmod -R 775 nginx
(-R: Option to change all permissions in the directory)
Start Nginx
[Instance lib]]$ cd ~
[instance]$ sudo systemctl start nginx
[instance]$ sudo systemctl reload nginx
② Change application settings
This time, I will explain assuming that you are using Unicorn.
config/unicorn.rb
Fixed listen 3000=> listen "#{app_path}/tmp/sockets/unicorn.sock"
Go to the application directory in your instance
[instance]$ cd /var/www/<Application name>
[instance<Application name>]$ git pull origin master
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
We hope that this post will help beginners review.
Recommended Posts