5

我有一个Rails 5 API,我试图在Elastic Beanstalk上正确地部署它。将Rails + Puma + Postgres应用程序部署到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 

我真正的问题是,这是正确的做法吗?如果有人已经做到了,你能指点我吗?有没有办法通过码头集装箱做到这一点?

+0

你尝试只安装在Gemfile中的PUMA宝石与彪马选择Ruby平台,当你走通过Elastic Beanstalk设置?我会先看看你是否可以在没有任何特殊的Puma配置文件的情况下运行它。 – littleforest

+0

@littleforest彪马宝石已经在我的gemfile中。除了添加这两行外,我没有做任何其他配置。我在轨道上5 – aks

+0

您是否确认您使用Puma启动了Ruby平台,而不使用Passenger? – littleforest

回答

3

您也可以将Rails应用程序作为Rails-Puma应用程序部署到Elastic Beanstalk或Docker。答案会更一般,而且要点从哪里开始比提供完整的解决方案。

红宝石 - 彪马

这可能是一个相当棘手:如果您创建通过控制台新的弹性魔豆环境红宝石(网络浏览器),它可以通过默认设置乘客的平台,而不是Puma平台。大概你不能在控制台进行更改:

enter image description here

要与彪马创造新的环境中,使用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): 

如果你想创建弹性青苗的工人,而不是Web服务器,运行:

$ eb create -t worker 

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

泊坞

继后也许有用如何设置的Rails +彪马+ Nginx的+泊坞窗:

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

这是多包装配置,其中Nginx的是绑定到端口80和流请求通过套接字到美洲狮。在你的情况下,它将是:"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 

某些设置可能会有所不同,但问题是,我结合有美洲狮服务器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; 
} 
+0

你能否粘贴你的'puma.config'文件?它会改善这个答案的用处 –

相关问题