gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:41:56 25 4
gpt4 key购买 nike

我有一个 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 -彪马

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

enter image description here

要使用 Puma 创建新环境,请使用 eb cli .不错的演练here .但是,在运行 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

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

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

这是多容器配置,其中 Nginx 绑定(bind)到端口 80 并通过套接字将请求流式传输到 puma。在您的情况下,它将是: "unix:///var/run/puma/my_app.sock"
要上传 Docker,您可以使用 AWS ECR 存储 Docker 镜像。您必须创建 Dockerrun.aws.json 文件(与 docker-compose.yml 文件非常相似),然后您可以通过 AWS 控制台(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 服务器绑定(bind)到 unix 套接字并与 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;
}

关于ruby-on-rails - 将 Rails + Puma + Postgres 应用程序部署到 Elastic beanstalk 的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40938155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com