Rails部署上线 #
一、部署准备 #
1.1 环境配置 #
ruby
# config/environments/production.rb
config.force_ssl = true
config.assets.compile = false
config.log_level = :info
1.2 密钥管理 #
bash
rails credentials:edit
二、Capistrano部署 #
2.1 安装 #
ruby
gem 'capistrano-rails', group: :development
2.2 配置 #
ruby
# config/deploy.rb
lock '3.17.0'
set :application, 'myapp'
set :repo_url, 'git@github.com:user/myapp.git'
set :deploy_to, '/var/www/myapp'
三、Docker部署 #
3.1 Dockerfile #
dockerfile
FROM ruby:3.2
WORKDIR /app
COPY Gemfile Gemfile.lock ./
RUN bundle install
COPY . .
RUN rails assets:precompile
CMD ["rails", "server", "-b", "0.0.0.0"]
3.2 docker-compose #
yaml
version: '3'
services:
web:
build: .
ports:
- "3000:3000"
depends_on:
- db
db:
image: postgres:15
四、总结 #
4.1 核心要点 #
| 要点 | 说明 |
|---|---|
| 生产环境 | production配置 |
| Capistrano | 自动部署 |
| Docker | 容器化部署 |
4.2 下一步 #
现在你已经掌握了部署上线,接下来让我们学习 博客系统,深入了解Rails的实战项目!
最后更新:2026-03-28