Search code examples
ruby-on-railsamazon-ec2ubuntu-14.04capistranopuma

Can't access the Rails 5 App in production on AWS EC2 instance running Puma 3.6 after deployment with Capistrano


I have a very simple app with only a header test to be rendered on a home page that should be accessible at my server public IP, something like this:

http://54.88.181.57:3000

In development the app is working:

http://localhost:3000/

rendering the very simple message:

Tasks home page

In production Puma is available here:

$ ps -ef | grep puma
deploy    5653     1  0 16:23 ?        00:00:01 puma 3.6.0 (unix:///home/deploy/tasks/shared/tmp/sockets/puma.sock) [20160924162316]                                 

The instance is a AWS EC2 Ubuntu 14.04 There are no issues with the DB which is a AWS RDS Postgresql database (accordingly with the production.log) and I can access it anytime using:

$ psql --host tasks.cbb3snhbk4wo.us-east-1.rds.amazonaws.com --port 5432 --username tasks --dbname tasks
Password for user tasks: 
psql (9.3.14, server 9.5.2)
WARNING: psql major version 9.3, server major version 9.5.
         Some psql features might not work.
SSL connection (cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256)
Type "help" for help.

tasks=> \dt
               List of relations
 Schema |         Name         | Type  | Owner 
--------+----------------------+-------+-------
 public | ar_internal_metadata | table | tasks
 public | schema_migrations    | table | tasks
(2 rows)

tasks=> \q

There are no error messages reports in the log files, below is the content of the log files.

$ pwd
/home/deploy/tasks/current/log

$ cat production.log 
D, [2016-09-24T12:16:07.190313 #30239] DEBUG -- :    (2.5ms)  SELECT pg_try_advisory_lock(2768007245500244475);
D, [2016-09-24T12:16:07.200361 #30239] DEBUG -- :   ActiveRecord::SchemaMigration Load (2.7ms)  SELECT "schema_migrations".* FROM "schema_migrations"
D, [2016-09-24T12:16:07.220410 #30239] DEBUG -- :   ActiveRecord::InternalMetadata Load (1.5ms)  SELECT  "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2  [["key", :environment], ["LIMIT", 1]]
D, [2016-09-24T12:16:07.226223 #30239] DEBUG -- :    (1.3ms)  BEGIN
D, [2016-09-24T12:16:07.228361 #30239] DEBUG -- :    (1.4ms)  COMMIT
D, [2016-09-24T12:16:07.229899 #30239] DEBUG -- :    (1.4ms)  SELECT pg_advisory_unlock(2768007245500244475)
D, [2016-09-24T14:10:42.585283 #1857] DEBUG -- :    (2.6ms)  SELECT pg_try_advisory_lock(2768007245500244475);
D, [2016-09-24T14:10:42.603894 #1857] DEBUG -- :   ActiveRecord::SchemaMigration Load (5.2ms)  SELECT "schema_migrations".* FROM "schema_migrations"
D, [2016-09-24T14:10:42.626674 #1857] DEBUG -- :   ActiveRecord::InternalMetadata Load (1.6ms)  SELECT  "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2  [["key", :environment], ["LIMIT", 1]]
D, [2016-09-24T14:10:42.639144 #1857] DEBUG -- :    (1.6ms)  BEGIN
D, [2016-09-24T14:10:42.641200 #1857] DEBUG -- :    (1.2ms)  COMMIT
D, [2016-09-24T14:10:42.642593 #1857] DEBUG -- :    (1.3ms)  SELECT pg_advisory_unlock(2768007245500244475)
D, [2016-09-24T15:32:06.662915 #2622] DEBUG -- :    (2.6ms)  SELECT pg_try_advisory_lock(2768007245500244475);
D, [2016-09-24T15:32:06.672595 #2622] DEBUG -- :   ActiveRecord::SchemaMigration Load (2.4ms)  SELECT "schema_migrations".* FROM "schema_migrations"
D, [2016-09-24T15:32:06.702690 #2622] DEBUG -- :   ActiveRecord::InternalMetadata Load (1.6ms)  SELECT  "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2  [["key", :environment], ["LIMIT", 1]]
D, [2016-09-24T15:32:06.708799 #2622] DEBUG -- :    (1.3ms)  BEGIN
D, [2016-09-24T15:32:06.710904 #2622] DEBUG -- :    (1.3ms)  COMMIT
D, [2016-09-24T15:32:06.712580 #2622] DEBUG -- :    (1.5ms)  SELECT pg_advisory_unlock(2768007245500244475)
D, [2016-09-24T15:44:15.659955 #3517] DEBUG -- :    (1.3ms)  SELECT pg_try_advisory_lock(2768007245500244475);
D, [2016-09-24T15:44:15.669138 #3517] DEBUG -- :   ActiveRecord::SchemaMigration Load (1.8ms)  SELECT "schema_migrations".* FROM "schema_migrations"
D, [2016-09-24T15:44:15.682996 #3517] DEBUG -- :   ActiveRecord::InternalMetadata Load (1.4ms)  SELECT  "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2  [["key", :environment], ["LIMIT", 1]]
D, [2016-09-24T15:44:15.694242 #3517] DEBUG -- :    (1.2ms)  BEGIN
D, [2016-09-24T15:44:15.696341 #3517] DEBUG -- :    (1.2ms)  COMMIT
D, [2016-09-24T15:44:15.697750 #3517] DEBUG -- :    (1.3ms)  SELECT pg_advisory_unlock(2768007245500244475)
D, [2016-09-24T16:03:29.398817 #4802] DEBUG -- :    (1.3ms)  SELECT pg_try_advisory_lock(2768007245500244475);
D, [2016-09-24T16:03:29.407495 #4802] DEBUG -- :   ActiveRecord::SchemaMigration Load (1.6ms)  SELECT "schema_migrations".* FROM "schema_migrations"
D, [2016-09-24T16:03:29.421703 #4802] DEBUG -- :   ActiveRecord::InternalMetadata Load (1.6ms)  SELECT  "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2  [["key", :environment], ["LIMIT", 1]]
D, [2016-09-24T16:03:29.432893 #4802] DEBUG -- :    (1.6ms)  BEGIN
D, [2016-09-24T16:03:29.435183 #4802] DEBUG -- :    (1.5ms)  COMMIT
D, [2016-09-24T16:03:29.436803 #4802] DEBUG -- :    (1.5ms)  SELECT pg_advisory_unlock(2768007245500244475)
D, [2016-09-24T16:23:25.345080 #5561] DEBUG -- :    (1.4ms)  SELECT pg_try_advisory_lock(2768007245500244475);
D, [2016-09-24T16:23:25.353737 #5561] DEBUG -- :   ActiveRecord::SchemaMigration Load (1.4ms)  SELECT "schema_migrations".* FROM "schema_migrations"
D, [2016-09-24T16:23:25.368330 #5561] DEBUG -- :   ActiveRecord::InternalMetadata Load (1.6ms)  SELECT  "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2  [["key", :environment], ["LIMIT", 1]]
D, [2016-09-24T16:23:25.379551 #5561] DEBUG -- :    (1.5ms)  BEGIN
D, [2016-09-24T16:23:25.381611 #5561] DEBUG -- :    (1.3ms)  COMMIT
D, [2016-09-24T16:23:25.383051 #5561] DEBUG -- :    (1.3ms)  SELECT pg_advisory_unlock(2768007245500244475)

$ cat puma_access.log 
=== puma startup: 2016-09-24 12:16:08 +0000 ===
=== puma startup: 2016-09-24 14:10:43 +0000 ===
=== puma startup: 2016-09-24 15:32:08 +0000 ===
=== puma startup: 2016-09-24 15:44:17 +0000 ===
=== puma startup: 2016-09-24 16:03:30 +0000 ===
=== puma startup: 2016-09-24 16:23:26 +0000 ===

$ cat puma_error.log 
=== puma startup: 2016-09-24 12:16:08 +0000 ===
* Listening on unix:///home/deploy/tasks/shared/tmp/sockets/puma.sock
- Gracefully stopping, waiting for requests to finish
=== puma shutdown: 2016-09-24 13:45:35 +0000 ===
- Goodbye!
=== puma startup: 2016-09-24 14:10:43 +0000 ===
* Listening on unix:///home/deploy/tasks/shared/tmp/sockets/puma.sock
* Restarting...
Refreshing Gemfile
Puma starting in single mode...
* Version 3.6.0 (ruby 2.3.1-p112), codename: Sleepy Sunday Serenity
* Min threads: 0, max threads: 8
* Environment: production
* Daemonizing...
=== puma startup: 2016-09-24 15:32:08 +0000 ===
* Inherited unix:///home/deploy/tasks/shared/tmp/sockets/puma.sock
* Restarting...
Refreshing Gemfile
Puma starting in single mode...
* Version 3.6.0 (ruby 2.3.1-p112), codename: Sleepy Sunday Serenity
* Min threads: 0, max threads: 8
* Environment: production
* Daemonizing...
=== puma startup: 2016-09-24 15:44:17 +0000 ===
* Inherited unix:///home/deploy/tasks/shared/tmp/sockets/puma.sock
* Restarting...
Refreshing Gemfile
Puma starting in single mode...
* Version 3.6.0 (ruby 2.3.1-p112), codename: Sleepy Sunday Serenity
* Min threads: 0, max threads: 8
* Environment: production
* Daemonizing...
=== puma startup: 2016-09-24 16:03:30 +0000 ===
* Inherited unix:///home/deploy/tasks/shared/tmp/sockets/puma.sock
* Restarting...
Refreshing Gemfile
Puma starting in single mode...
* Version 3.6.0 (ruby 2.3.1-p112), codename: Sleepy Sunday Serenity
* Min threads: 0, max threads: 8
* Environment: production
* Daemonizing...
=== puma startup: 2016-09-24 16:23:26 +0000 ===
* Inherited unix:///home/deploy/tasks/shared/tmp/sockets/puma.sock

And this is my puma.rb config file:

$ cat /home/deploy/tasks/current/config/puma.rb 

app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/shared"

threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i
threads threads_count, threads_count

port        ENV.fetch("PORT") { 3000 }
environment ENV.fetch("RAILS_ENV") { "development" }

workers ENV.fetch("WEB_CONCURRENCY") { 2 }
preload_app!

# 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
  ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
end

plugin :tmp_restart

Don't know what is not right here and why I cannot access my app in production using the serve public IP and port 3000?


Solution

  • First, verify that Puma is indeed listening on port 3000 by running this command on the server:

    netstat -an | grep 3000
    

    You should see something like this:

    tcp        0      0 0.0.0.0:3000    0.0.0.0:*    LISTEN
    

    If Puma is listening on port 3000, then perhaps a firewall or other security policy is blocking access.

    If Puma is not listening on port 3000, then I suspect the config/puma.rb file that you pasted in your question is not actually being loaded. As evidence, I see in the logs that Puma is binding to unix:///home/deploy/tasks/shared/tmp/sockets/puma.sock, but in the puma.rb you've specified shared/sockets/puma.sock (without the tmp).

    You haven't explained how exactly you started Puma, but whatever method you are using, make sure Puma is being sent the -C flag with the path to the puma.rb that you would like it to use.

    If you are using the capistrano3-puma gem, then refer also to this related answer: Capistrano doesn't create puma.pid file