Rails 7应用基于Nginx与Puma的EC2服务器部署配置求助
Rails应用部署到EC2(Nginx+Puma)公网访问全流程
一、基础环境确认
登录EC2服务器,先确保核心依赖已安装(以Ubuntu为例,CentOS请替换为yum/dnf命令):
sudo apt update && sudo apt install nginx ruby-full build-essential nodejs
确认Puma已安装:
gem list puma
如果未安装,执行gem install puma。
二、修复Puma Socket文件缺失错误(你遇到的核心问题)
这个错误是因为Puma未生成socket文件,或路径/权限配置错误,按以下步骤解决:
1. 配置Puma运行参数
进入应用current目录,创建/修改config/puma.rb:
# config/puma.rb application_path = '/home/user/apps/myapp/current' directory application_path environment 'production' # 绑定到shared目录下的socket(和你报错路径一致) bind "unix:///home/user/apps/myapp/shared/tmp/sockets/puma.myapp.sock" pidfile "/home/user/apps/myapp/shared/tmp/pids/puma.pid" state_path "/home/user/apps/myapp/shared/tmp/pids/puma.state" # 日志输出到shared目录 stdout_redirect "/home/user/apps/myapp/shared/log/puma.stdout.log", "/home/user/apps/myapp/shared/log/puma.stderr.log", true # 进程/线程数根据EC2配置调整,t2.micro建议设2个worker workers 2 threads 1, 6 # 后台运行 daemonize true
2. 创建必要目录并设置权限
shared目录下的临时文件夹可能未创建,或权限不足导致Puma无法写入:
cd /home/user/apps/myapp/shared mkdir -p tmp/sockets tmp/pids log # 给当前用户读写权限 sudo chown -R $USER:$USER /home/user/apps/myapp/shared sudo chmod -R 755 /home/user/apps/myapp/shared/tmp
3. 启动Puma并验证socket文件
cd /home/user/apps/myapp/current # 先终止可能残留的Puma进程 pumactl -P /home/user/apps/myapp/shared/tmp/pids/puma.pid stop || true # 启动Puma bundle exec puma -C config/puma.rb # 检查socket文件是否生成 ls /home/user/apps/myapp/shared/tmp/sockets/
如果输出里有puma.myapp.sock,说明Puma配置生效。
三、配置Nginx反向代理
1. 创建站点配置文件
sudo nano /etc/nginx/sites-available/myapp
写入以下内容(替换your-domain.com为你的实际域名):
server { listen 80; server_name your-domain.com; root /home/user/apps/myapp/current/public; # 静态资源直接由Nginx处理,提升速度 location ~ ^/assets/ { expires 1y; add_header Cache-Control public; add_header ETag ""; break; } # 动态请求转发给Puma的socket location / { proxy_pass http://unix:/home/user/apps/myapp/shared/tmp/sockets/puma.myapp.sock; 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-Proto $scheme; } # 自定义错误页面 error_page 404 /404.html; error_page 500 502 503 504 /500.html; }
2. 启用站点并重启Nginx
# 软链接到启用目录 sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/ # 测试配置是否合法 sudo nginx -t # 重启Nginx生效 sudo systemctl restart nginx
四、域名解析配置
- 登录你的域名服务商后台(如阿里云、Godaddy)
- 添加A记录,将域名指向EC2实例的公网IP
- 等待DNS生效(通常10-30分钟)
五、可选优化配置
- 配置HTTPS:用Let's Encrypt免费证书,执行:
工具会自动修改Nginx配置并启用HTTPS。sudo apt install certbot python3-certbot-nginx sudo certbot --nginx -d your-domain.com - Puma开机自启:创建systemd服务文件,避免服务器重启后Puma停止:
写入内容(替换路径为你的实际路径):sudo nano /etc/systemd/system/puma.service
然后启用服务:[Unit] Description=Puma HTTP Server After=network.target [Service] Type=forking User=your-ec2-username WorkingDirectory=/home/user/apps/myapp/current ExecStart=/usr/local/bin/bundle exec puma -C /home/user/apps/myapp/current/config/puma.rb ExecReload=/usr/local/bin/bundle exec pumactl -P /home/user/apps/myapp/shared/tmp/pids/puma.pid restart ExecStop=/usr/local/bin/bundle exec pumactl -P /home/user/apps/myapp/shared/tmp/pids/puma.pid stop Restart=always [Install] WantedBy=multi-user.targetsudo systemctl daemon-reload sudo systemctl enable puma sudo systemctl start puma
常见排查点
- 若仍报socket不存在:查看Puma日志
/home/user/apps/myapp/shared/log/puma.stderr.log找具体错误,或用ps aux | grep puma确认Puma是否在运行 - Nginx报错:查看日志
/var/log/nginx/error.log - 权限问题:确保Nginx用户(www-data)能访问socket文件,执行
sudo chown -R www-data:$USER /home/user/apps/myapp/shared/tmp/sockets
内容的提问来源于stack exchange,提问作者Akil Tahsin
相关产品推荐
相关产品推荐

