搬瓦工部署 Ruby on Rails 应用教程

Ruby on Rails(简称 Rails)是一款经典的全栈 Web 框架,以约定优于配置的理念著称。许多知名应用都基于 Rails 构建。本文将介绍如何在搬瓦工 VPS 上搭建完整的 Rails 运行环境并部署应用。购买搬瓦工 VPS 请参考 全部方案,使用优惠码 NODESEEK2026 可享受 6.77% 的折扣。

一、安装 Ruby

apt update && apt upgrade -y
apt install build-essential libssl-dev libreadline-dev zlib1g-dev libsqlite3-dev libyaml-dev libffi-dev curl git -y

# 使用 rbenv 安装 Ruby
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/HEAD/bin/rbenv-installer | bash
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc

rbenv install 3.3.0
rbenv global 3.3.0
gem install bundler

二、安装 Node.js 和 Yarn

curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt install -y nodejs
npm install -g yarn

三、安装 PostgreSQL

apt install postgresql postgresql-contrib libpq-dev -y
systemctl enable postgresql

sudo -u postgres psql << 'EOF'
CREATE USER rails WITH PASSWORD 'your_db_password' CREATEDB;
CREATE DATABASE myapp_production OWNER rails;
EOF

四、部署 Rails 应用

cd /var/www
git clone https://your-repo.git myapp
cd myapp

# 安装依赖
bundle install --deployment --without development test
yarn install

# 配置环境变量
cat > /var/www/myapp/.env << 'EOF'
RAILS_ENV=production
DATABASE_URL=postgresql://rails:your_db_password@localhost/myapp_production
SECRET_KEY_BASE=$(bin/rails secret)
RAILS_SERVE_STATIC_FILES=true
EOF

# 数据库迁移和资源编译
RAILS_ENV=production bin/rails db:migrate
RAILS_ENV=production bin/rails assets:precompile

五、配置 Puma 服务器

cat > /var/www/myapp/config/puma.rb << 'EOF'
workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['RAILS_MAX_THREADS'] || 5)
threads threads_count, threads_count

environment ENV.fetch("RAILS_ENV") { "production" }

bind "unix:///var/www/myapp/tmp/sockets/puma.sock"
pidfile "/var/www/myapp/tmp/pids/puma.pid"
state_path "/var/www/myapp/tmp/pids/puma.state"

preload_app!
EOF

mkdir -p /var/www/myapp/tmp/{sockets,pids}

六、Systemd 服务

cat > /etc/systemd/system/puma.service << 'EOF'
[Unit]
Description=Puma Rails Server
After=network.target

[Service]
Type=simple
User=www-data
WorkingDirectory=/var/www/myapp
ExecStart=/root/.rbenv/shims/bundle exec puma -C config/puma.rb
Restart=always
RestartSec=5
Environment=RAILS_ENV=production

[Install]
WantedBy=multi-user.target
EOF

chown -R www-data:www-data /var/www/myapp
systemctl daemon-reload
systemctl enable puma
systemctl start puma

七、Nginx 反向代理

apt install nginx -y

cat > /etc/nginx/sites-available/rails.conf << 'EOF'
upstream puma {
    server unix:///var/www/myapp/tmp/sockets/puma.sock fail_timeout=0;
}

server {
    listen 80;
    server_name yourdomain.com;
    root /var/www/myapp/public;

    location / {
        try_files $uri @puma;
    }

    location @puma {
        proxy_pass http://puma;
        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;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
    }
}
EOF

ln -sf /etc/nginx/sites-available/rails.conf /etc/nginx/sites-enabled/
rm -f /etc/nginx/sites-enabled/default
nginx -t && systemctl restart nginx

八、SSL 与 Sidekiq

# SSL 证书
apt install certbot python3-certbot-nginx -y
certbot --nginx -d yourdomain.com

# Sidekiq 后台任务(如使用)
cat > /etc/systemd/system/sidekiq.service << 'EOF'
[Unit]
Description=Sidekiq
After=network.target

[Service]
Type=simple
User=www-data
WorkingDirectory=/var/www/myapp
ExecStart=/root/.rbenv/shims/bundle exec sidekiq -e production
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

九、常见问题

资源预编译失败

确保 Node.js 和 Yarn 已安装。内存不足时预编译可能失败,添加 Swap 空间可以解决。

总结

Ruby on Rails 是经典且强大的 Web 框架,在搬瓦工 VPS 上通过 Puma + Nginx 部署可以获得优秀的性能。其他后端框架教程包括 Laravel PHPPhoenix ElixirActix Rust 等。购买搬瓦工 VPS 请访问 bwh81.net,使用优惠码 NODESEEK2026 可享受 6.77% 的折扣。

关于本站

搬瓦工VPS中文网(bwgvps.com)是非官方中文信息站,整理搬瓦工的方案、优惠和教程。我们不销售主机,不提供技术服务。

新手必读
搬瓦工优惠码

NODESEEK2026(优惠 6.77%)

购买时填入即可抵扣。