Deploy Rails
This guide will help you deploy a Ruby on Rails application to Deployxa.
Overview
Ruby on Rails is a full-stack web framework for building database-backed applications. Deployxa provides optimized deployments for Rails with automatic configuration.
Features:
- Automatic framework detection
- Ruby 3.0+ support
- PostgreSQL and MySQL support
- Asset pipeline
- Database migrations
- Puma web server
- Automatic HTTPS
Prerequisites
- A Rails application (version 7 or higher recommended)
- A GitHub repository with your code
- A Deployxa account
- PostgreSQL or MySQL database
Quick Start
1. Create Your Rails App
If you don't have a Rails app yet:
gem install rails
rails new my-rails-app --database=postgresql
cd my-rails-app
2. Configure for Production
Update config/environments/production.rb:
Rails.application.configure do
config.force_ssl = true
config.log_level = :info
config.assets.compile = false
end
Update config/database.yml:
production:
<<: *default
url: <%= ENV['DATABASE_URL'] %>
3. Push to GitHub
git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/yourusername/my-rails-app.git
git push -u origin main
4. Deploy to Deployxa
- Go to Deployxa Dashboard
- Click "New Project"
- Select your GitHub repository
- Deployxa will automatically detect Rails
- Click "Deploy"
Your app will be live in under 60 seconds!
Configuration
Detected Settings
Deployxa automatically detects these settings:
| Setting | Value |
|---|---|
| Framework | Rails |
| Build Command | bundle install --without development test |
| Output Directory | public |
| Start Command | bundle exec puma -C config/puma.rb |
| Port | 3000 |
Custom Configuration
If you need to customize settings:
- Go to Project → Settings → Build
- Modify settings as needed
- Save changes
Example Custom Settings:
{
"buildCommand": "bundle install --without development test && bundle exec rake assets:precompile",
"outputDirectory": "public",
"startCommand": "bundle exec puma -C config/puma.rb"
}
Environment Variables
Adding Environment Variables
- Go to Project → Environment
- Click "Add Variable"
- Enter key and value
- Click "Save"
Required Rails Environment Variables:
RAILS_ENV=production
RAILS_LOG_TO_STDOUT=true
RAILS_SERVE_STATIC_FILES=true
SECRET_KEY_BASE=your-secret-key-base-here
# Database
DATABASE_URL=postgresql://user:pass@host:5432/dbname
# Redis (optional)
REDIS_URL=redis://user:pass@host:6379/0
Generating SECRET_KEY_BASE
Generate a new secret key base:
rails secret
Or use Ruby:
ruby -rsecurerandom -e 'puts SecureRandom.hex(64)'
Database Configuration
Using Managed Database
- Go to Project → Settings → Database
- Click "Create Database"
- Choose database type (PostgreSQL recommended)
- Configure database settings
- Copy connection details to environment variables
Database Migrations
Run migrations automatically on deployment:
Option 1: Post-Deploy Script
Create a deployment script:
#!/bin/bash
bundle exec rake db:migrate
bundle exec rake assets:precompile
Option 2: Manual Migration
Run migrations via SSH or console:
bundle exec rake db:migrate
Database Seeding
Seed your database after migration:
bundle exec rake db:seed
Asset Pipeline
Precompiling Assets
Compile assets automatically:
bundle exec rake assets:precompile
This command should be run during deployment.
Using Propshaft (Rails 7+)
For modern asset management:
# Gemfile
gem "propshaft"
Using Webpacker / Shakapacker
For advanced asset management:
bundle add webpacker
rails webpacker:install
Build assets:
bundle exec rake assets:precompile
Custom Domains
Adding a Custom Domain
- Go to Project → Domains
- Click "Add Domain"
- Enter your domain (e.g.,
example.com) - Configure DNS records
- Wait for verification
DNS Configuration
For Apex Domain (example.com):
Type: A
Name: @
Value: 76.76.21.21
For Subdomain (www.example.com):
Type: CNAME
Name: www
Value: cname.deployxa.com
Performance Optimization
Enable Puma Workers
Configure Puma workers based on CPU cores:
# config/puma.rb
workers ENV.fetch("WEB_CONCURRENCY") { 2 }
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
threads threads_count, threads_count
Enable Caching
Configure Rails caching:
# config/environments/production.rb
config.action_controller.perform_caching = true
config.cache_store = :memory_store
Database Indexing
Add indexes to frequently queried columns:
# db/migrate/xxxx_add_index_to_users.rb
class AddIndexToUsers < ActiveRecord::Migration[7.0]
def change
add_index :users, :email
end
end
Monitoring
Viewing Logs
- Go to Project → Logs
- Select log type:
- Build Logs: Deployment process
- Runtime Logs: Application output
- Error Logs: Application errors
Rails Logging
Configure logging in config/environments/production.rb:
config.log_level = :info
config.log_tags = [ :request_id ]
View logs:
log/production.log
Monitoring Metrics
- Go to Project → Analytics
- View metrics:
- Request count
- Response times
- Error rates
- Bandwidth usage
- CPU/RAM usage
Health Checks
Deployxa automatically monitors your application:
- HTTP health checks every 30 seconds
- Automatic restart on failure
- Alerts on consecutive failures
Troubleshooting
500 Internal Server Error
Problem: Application returns 500 error
Solution:
- Check runtime logs for errors
- Verify environment variables
- Check database connection
- Ensure SECRET_KEY_BASE is set
- Check file permissions
Database Connection Failed
Problem: ActiveRecord::ConnectionNotEstablished
Solution:
- Verify DATABASE_URL is correct
- Check database host and port
- Ensure database exists
- Check firewall rules
- Verify database user permissions
Migration Failed
Problem: rake db:migrate fails
Solution:
- Check database connection
- Verify migration files
- Check for syntax errors
- Run migrations locally first
- Check database permissions
Assets Not Loading
Problem: CSS/JS files return 404
Solution:
- Run
rake assets:precompile - Check public/assets directory exists
- Verify asset paths are correct
- Check file permissions
Memory Errors
Problem: Killed or out of memory
Solution:
- Reduce Puma workers
- Optimize application code
- Upgrade plan for more RAM
- Use background jobs for heavy tasks
Best Practices
Project Structure
my-rails-app/
├── app/
│ ├── controllers/
│ ├── models/
│ ├── views/
│ └── assets/
├── config/
│ ├── environments/
│ ├── initializers/
│ └── database.yml
├── db/
│ ├── migrate/
│ └── schema.rb
├── public/
├── log/
├── Gemfile
├── Gemfile.lock
├── Rakefile
└── README.md
Environment Variables
- Never commit
.envfile - Use
.env.exampleas template - Rotate secrets regularly
- Use different values per environment
Security
- Keep Rails updated
- Use HTTPS only
- Validate all inputs
- Use CSRF protection
- Sanitize user data
- Use parameterized queries
Performance
- Use Puma with multiple workers
- Enable caching
- Optimize database queries
- Use background jobs
- Use CDN for static assets
- Enable compression
Database
- Use migrations for schema changes
- Index frequently queried columns
- Use transactions for complex operations
- Backup database regularly
- Use connection pooling
Advanced Configuration
Custom Puma Configuration
# config/puma.rb
max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }
threads min_threads_count, max_threads_count
worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development"
port ENV.fetch("PORT") { 3000 }
environment ENV.fetch("RAILS_ENV") { "development" }
pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }
workers ENV.fetch("WEB_CONCURRENCY") { 2 }
preload_app!
plugin :tmp_restart
Background Jobs with Sidekiq
For background jobs:
# Gemfile
gem "sidekiq"
# config/initializers/sidekiq.rb
Sidekiq.configure_server do |config|
config.redis = { url: ENV.fetch("REDIS_URL") { "redis://localhost:6379/0" } }
end
Sidekiq.configure_client do |config|
config.redis = { url: ENV.fetch("REDIS_URL") { "redis://localhost:6379/0" } }
end
Active Storage
For file uploads:
# config/storage.yml
amazon:
service: S3
access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
region: <%= ENV['AWS_REGION'] %>
bucket: <%= ENV['AWS_BUCKET'] %>
Examples
Basic Rails App
# app/controllers/welcome_controller.rb
class WelcomeController < ApplicationController
def index
render plain: "Hello, Deployxa!"
end
end
# config/routes.rb
Rails.application.routes.draw do
root "welcome#index"
end
With Database Model
# app/models/user.rb
class User < ApplicationRecord
validates :name, presence: true
validates :email, presence: true, uniqueness: true
end
# app/controllers/users_controller.rb
class UsersController < ApplicationController
def index
@users = User.all
render json: @users
end
def create
@user = User.new(user_params)
if @user.save
render json: @user, status: :created
else
render json: @user.errors, status: :unprocessable_entity
end
end
private
def user_params
params.require(:user).permit(:name, :email)
end
end
API Endpoint
# app/controllers/api/v1/users_controller.rb
module Api
module V1
class UsersController < ApplicationController
def index
render json: User.all
end
end
end
end
# config/routes.rb
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
resources :users, only: [:index, :show, :create]
end
end
end
Related Topics
Resources
Need Help? Contact support@deployxa.com or join our community Discord.