[RAILS] How to create a web server on an EC2 instance on AWS


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.

This flow

"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.

Install Nginx on your EC2 instance

What is Nginx

This is a typical web server.

① Install Nginx in your own application


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)


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;


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.


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


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 in the browser, and if the application is displayed, it is successful. ** **


We hope that this post will help beginners review.

Recommended Posts

How to create a web server on an EC2 instance on AWS
How to create an application server on an EC2 instance on AWS
How to install Ruby on an EC2 instance on AWS
How to publish an application using AWS (3) EC2 instance environment construction
How to deploy a container on AWS Lambda
Build a Laravel environment on an AWS instance
Volume of trying to create a Java Web application on Windows Server 2016
How to create an application
Install docker-compose on a Graviton 2 instance of AWS EC2
Deploy laravel using docker on EC2 on AWS ① (Create EC2 instance)
How to handle an instance
How to create a method
How to get inside a container running on AWS Fargate
How to deploy a kotlin (java) app on AWS fargate
How to deploy a Rails application on AWS (article summary)
Build a Minecraft server on AWS
[Java] How to create a folder
How to install and use Composer on an ECS instance on Ubuntu 16.04
How to run a mock server on Swagger-ui using stoplight/prism (using AWS/EC2/Docker)
How to publish an application on Heroku
How to create a Maven repository for 2020
[rails] How to create a partial template
How to send push notifications on AWS
How to create a query using variables in GraphQL [Using Ruby on Rails]
How to migrate a web application created in a local docker environment to AWS
[For beginners] Laravel Docker AWS (EC2) How to easily deploy a web application (PHP) from 0 (free) ①-Overview-
How to create a database for H2 Database anywhere
[Rails] How to create a graph using lazy_high_charts
How to display a web page in Java
How to create pagination for a "kaminari" array
How to save images on Heroku to S3 on AWS
How to create a class that inherits class information
How to create a theme in Liferay 7 / DXP
[1st] How to create a Spring-MVC framework project
How to easily create a pull-down in Rails
Rails6.0 ~ How to create an eco-friendly development environment
[Rails] How to create a Twitter share button
I tried installing docker on an EC2 instance
How to build a Pytorch environment on Ubuntu
Create a web api server with spring boot
Notes on how to create Burp Suite extensions
How to create an oleore certificate (SSL certificate, self-signed certificate)
How to check if an instance variable is defined in a Ruby class
Create a Docker container for your development web server in Ansible on MacOS
How to deploy a system created with Java (Wicket-Spring boot) to an on-campus server
How to create an Excel form using a template file with Spring MVC
[Introduction] Try to create a Ruby on Rails application
3. Create a database to access from the web module
How to create a Java environment in just 3 seconds
[Rails] How to create a signed URL for CloudFront
[Ruby On Rails / HTML] How to display the cursor (caret) at an appropriate place when displaying a web page
Create an EC site with Rails 5 ⑨ ~ Create a cart function ~
List how to learn from Docker to AKS on AWS
How to create a JDBC URL (Oracle Database, Thin)
How to create a Spring Boot project in IntelliJ
[Spring Boot] How to create a project (for beginners)
How to make JavaScript work on a specific page
How to create a header or footer once and use it on another page
How to create a data URI (base64) in Java
Create a user with an empty password on CentOS7
Steps to set up a VNC server on CentOS 8.3