将Rails + Puma + Postgres应用程序部署到Elastic beanstalk的正确方法?

我有一个Rails 5 API,我试图在Elastic Beanstalk上正确部署。

这是我使用的初始config/puma.rb文件:

 threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i threads threads_count, threads_count # Specifies the `port` that Puma will listen on to receive requests, default is 3000. port ENV.fetch("PORT") { 3000 } # Specifies the `environment` that Puma will run in. environment ENV.fetch("RAILS_ENV") { "development" } # Allow puma to be restarted by `rails restart` command. plugin :tmp_restart 

我收到以下套接字错误:

 2015/11/24 06:44:12 [crit] 2689#0: *4719 connect() to unix:///var/run/puma/my_app.sock failed (2: No such file or directory) while connecting to upstream 

为了解决这个问题,我尝试添加以下行并使其工作:

 rails_env = ENV['RAILS_ENV'] || "production" if rails_env == "production" bind "unix:///var/run/puma/my_app.sock" pidfile "/var/run/puma/my_app.sock" end 

我真正的问题是,这是正确的方法吗? 如果有人做过,你可以指点我吗? 有没有办法通过docker容器来做到这一点?

您可以将Rails应用程序部署为Rails – Puma应用程序到Elastic Beanstalk或Docker。 答案将更加一般,而不是指出从哪里开始提供完整的解决方案。

Ruby – Puma

这可能非常棘手:如果您通过控制台(在Web浏览器中)为Ruby创建新的Elastic Beanstalk环境,它可以默认设置Passenger平台,而不是Puma平台。 也许你不能在控制台中改变它:

在此处输入图像描述

要使用Puma创建新环境,请使用eb cli 。 这里有很好的演练。 但是,在运行eb create之前,你eb create要做一件事 – 选择平台:

 $ eb platform select It appears you are using Python. Is this correct? (y/n): n Select a platform. 1) Go 2) Node.js 3) PHP 4) Python 5) Ruby 6) Tomcat 7) IIS 8) Docker 9) Multi-container Docker 10) GlassFish 11) Java (default is 1): 5 Select a platform version. 1) Ruby 2.3 (Puma) 2) Ruby 2.2 (Puma) 3) Ruby 2.1 (Puma) 4) Ruby 2.0 (Puma) 5) Ruby 2.3 (Passenger Standalone) 6) Ruby 2.2 (Passenger Standalone) 7) Ruby 2.1 (Passenger Standalone) 8) Ruby 2.0 (Passenger Standalone) 9) Ruby 1.9.3 (default is 1): 

如果要创建Elastic Beanstalk的Worker而不是Web Server,请运行:

  $ eb create -t worker 

您可以使用控制台(Web浏览器)或eb cli ( docs )来设置其他配置。

docker

以下post可能有用如何设置Rails + Puma + Nginx + Docker:

http://codepany.com/blog/rails-5-and-docker-puma-nginx/

这是多容器配置,其中Nginx绑定到端口80并通过套接字将请求流发送到puma。 在你的情况下它将是: "unix:///var/run/puma/my_app.sock"

要上传Dockers,您可以使用AWS ECR存储Docker镜像。 您必须创建Dockerrun.aws.json文件(与docker-compose.yml文件非常相似),您可以通过AWS Console(Web浏览器)将其部署到您的环境中。

编辑

这是puma.rb配置文件:

 threads_count = ENV.fetch('RAILS_MAX_THREADS') { 5 } threads threads_count, threads_count bind "unix:///var/run/puma.sock?umask=0000" stdout_redirect "/var/log/puma.stdout.log", "/var/log/puma.stderr.log", true # Specifies the `environment` that Puma will run in. # environment ENV.fetch('RAILS_ENV') { 'development' } # Allow puma to be restarted by `rails restart` command. plugin :tmp_restart 

一些设置可能会有所不同,但关键是我将puma服务器绑定到unix socket并且它与NGINX连接。 NGINX配置文件:

 user root; error_log /var/log/app-nginx-error.log; pid /var/run/app-nginx.pid; events { worker_connections 8096; multi_accept on; use epoll; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/app-nginx-access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 10; upstream appserver { server unix:///var/run/puma.sock; } server { listen 80 default_server; root /var/www/public; client_max_body_size 16m; location ^~ /assets/ { gzip_static on; expires max; add_header Cache-Control public; } try_files $uri/index.html $uri @appserver; location @appserver { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Host $server_name; proxy_set_header Client-IP $remote_addr; proxy_pass http://appserver; } access_log /var/log/app-nginx-access.log; error_log /var/log/app-nginx-error.log debug; error_page 500 502 503 504 /500.html; } } 

NGINX配置文件中最重要的部分是:

 upstream appserver { server unix:///var/run/puma.sock; }