使用 Capistrano 部署后,无法在 AWS EC2 实例 运行 Puma 3.6 上访问生产中的 Rails 5 应用程序

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

我有一个非常简单的应用程序,只有一个 header 测试要在主页上呈现,应该可以在我的服务器 public IP 上访问,像这样:

http://54.88.181.57:3000

开发中的应用程序正在运行:

http://localhost:3000/

呈现非常简单的消息:

Tasks home page

生产中的 Puma 可在此处购买:

$ 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]                                 

该实例是 AWS EC2 Ubuntu 14.04 作为 AWS RDS Postgresql 数据库的数据库没有问题(根据 production.log),我可以随时使用以下方式访问它:

$ 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

日志文件中没有错误消息报告,以下是日志文件的内容。

$ 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" =  LIMIT   [["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" =  LIMIT   [["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" =  LIMIT   [["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" =  LIMIT   [["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" =  LIMIT   [["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" =  LIMIT   [["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

这是我的 puma.rb 配置文件:

$ 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

不知道这里有什么问题以及为什么我无法使用服务 public IP 和端口 3000 访问生产中的应用程序?

首先,在服务器上通过运行此命令验证 Puma 确实在端口 3000 上侦听:

netstat -an | grep 3000

你应该看到这样的东西:

tcp        0      0 0.0.0.0:3000    0.0.0.0:*    LISTEN

如果 Puma 正在侦听端口 3000,则可能是防火墙或其他安全策略阻止了访问。

如果 Puma 没有在端口 3000 上侦听,那么我怀疑您在问题中粘贴的 config/puma.rb 文件实际上并未被加载。作为证据,我在日志中看到 Puma 绑定到 unix:///home/deploy/tasks/shared/tmp/sockets/puma.sock,但在 puma.rb 中你指定了 shared/sockets/puma.sock(没有 tmp)。

您没有解释您是如何启动 Puma 的,但是无论您使用什么方法,请确保向 Puma 发送 -C 标志以及您想要的 puma.rb 的路径它使用。

如果你使用的是 capistrano3-puma gem,那么也可以参考这个相关的答案: