我有一个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容器来做到这一点?
答案 0 :(得分:3)
您可以将Rails应用程序部署为Rails - Puma应用程序到Elastic Beanstalk或Docker。答案将更加通用,而是指出从哪里开始而不是提供完整的解决方案。
Ruby - Puma
这可能非常棘手:如果您通过控制台(在Web浏览器中)为Ruby创建新的Elastic Beanstalk环境,它可以默认设置Passenger平台,而不是Puma平台。也许你无法在控制台中更改它:
要使用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
您可以使用控制台(网络浏览器)或eb cli
(docs)来设置其他配置。
<强>泊坞强>
以下文章可能对如何设置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;
}