I am trying to deploy my first Ruby on Rails API app in amazon ec2. For that I have copied my myapp
to my home directory /home/user
. In my server I have installed Rails using rbenv
, also installed mysql-client
.
In the app Gemfile I have added the following gems:
# Use Puma as the app server
gem 'puma', '~> 3.11'
#for server deployment
group :production, :staging do
gem 'capistrano'
gem 'capistrano3-puma'
gem 'capistrano-rails', require: false
gem 'capistrano-bundler', require: false
gem 'capistrano-rbenv'
end
puma.rb:
# Change to match your CPU core count
workers 1
# Min and Max threads per worker
threads 1, 6
app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/shared"
# Default to production
rails_env = ENV['RAILS_ENV'] || "production"
environment rails_env
# Set up socket location
bind "unix://#{shared_dir}/sockets/puma.sock"
# Logging
stdout_redirect "#{shared_dir}/log/puma.stdout.log", "#{shared_dir}/log/puma.stderr.log", true
# Set master PID and state locations
pidfile "#{shared_dir}/pids/puma.pid"
state_path "#{shared_dir}/pids/puma.state"
activate_control_app
on_worker_boot do
require "active_record"
ActiveRecord::Base.connection.disconnect! rescue ActiveRecord::ConnectionNotEstablished
ActiveRecord::Base.establish_connection(YAML.load_file("#{app_dir}/config/database.yml")[rails_env])
end
Initially, I am not looking for capistrano deployment. So I am not listing deploy.rb
file here.
I have install nginx in the server and in the configuration file I have added the following
/etc/nginx/conf.d/virtual.conf:
upstream app {
server unix:///home/user/myapp/shared/sockets/puma.sock fail_timeout=0;
}
server {
listen 80;
server_name localhost;
root /home/user/myapp/current/public;
try_files $uri/index.html $uri @app;
location / {
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Connection '';
proxy_pass http://app;
}
location ~ ^/(assets|fonts|system)/|favicon.ico|robots.txt {
gzip_static on;
expires max;
add_header Cache-Control public;
}
}
After adding the configuration I have restarted the server, I tired to get the server by providing the IP in the browser but timout happened. Then I started puma server using the command
bundle exec puma -e production -p 3000 -b unix:/home/user/myapp/shared/sockets/puma.sock
Do I have to run server like this?