簡體   English   中英

將Rails + Puma + Postgres應用程序部署到Elastic beanstalk的正確方法?

[英]Right way to deploy Rails + Puma + Postgres app to 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 clidocs )來設置其他配置。

搬運工人

以下帖子可能有用如何設置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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM