使用Capistrano

时间:2016-09-24 18:01:00

标签: ruby-on-rails amazon-ec2 ubuntu-14.04 capistrano puma

我有一个非常简单的应用程序,只有一个标题测试要在主页上呈现,应该可以在我的服务器公共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" = $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

这是我的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

不知道这里有什么不对,为什么我无法使用服务公共IP和端口3000访问我的应用程序?

1 个答案:

答案 0 :(得分:1)

首先,通过在服务器上运行此命令来验证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,请参阅此相关答案:Capistrano doesn't create puma.pid file