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'文件?它會改善這個答案的用處 –

相關問題