How to Host Ruby on Rails Applications: A Complete Server Setup Guide
Ruby on Rails is a powerful web application framework, but hosting Rails applications requires different server setup and understanding compared to traditional PHP websites. This guide walks you through choosing a suitable VPS, installing the essential tools, configuring your web server, and preparing your environment for production deployment. Whether you are deploying your first Rails app or managing multiple services, understanding the hosting fundamentals will help you build a reliable and performant infrastructure.
Contents
- How Ruby on Rails Differs from PHP Websites
- Minimum VPS Requirements for Rails Applications
- Installing Ruby, Rails, and Bundler on a Fresh Server
- Choosing an Application Server: Puma vs Unicorn
- Setting Up Nginx as a Reverse Proxy for Rails
- Database Setup for Rails: PostgreSQL vs MySQL
- Asset Pipeline: Precompilation and Production Serving
- Background Jobs: Sidekiq and Hosting Considerations
- Deployment Approaches: Capistrano, Docker, and CI/CD
- FAQ
How Ruby on Rails Differs from PHP Websites
PHP websites typically execute script files on disk, with the web server processing each file on demand. Ruby on Rails, by contrast, requires an application instance to run continuously on your server. Rails relies on a persistent application process (such as Puma or Unicorn) that maintains HTTP connections, and a web server like Nginx acts as a reverse proxy, forwarding requests to these processes. Additionally, Rails applications require extra preparation steps, including asset compilation and dependency management through a Gemfile, making the hosting setup more involved than a simple PHP installation.
- PHP executes scripts per request; Rails needs persistent application processes
- Ruby and Rails must be installed with language-specific tools
- Assets (JavaScript, CSS, images) require preprocessing and compilation
- Rails typically uses PostgreSQL or MySQL databases
- Dependency management relies on Bundler and the Gemfile system
Minimum VPS Requirements for Rails Applications
For small to medium Rails applications, a VPS with at least 2 CPU cores, 2 GB of RAM, and 20-30 GB of disk space is a reasonable starting point. However, the actual requirements depend on your application size and expected concurrent users. Applications with heavy processing or large user bases may need more RAM. Nowadays, SSD storage is the industry standard and delivers significantly faster read-write performance than traditional HDD. Additionally, consider choosing a provider that guarantees at least 99% uptime, as application reliability depends on consistent server availability.
- 2-4 CPU cores sufficient for small to medium applications
- 2 GB RAM minimum (4-8 GB for resource-intensive apps)
- 20-50 GB SSD storage depending on data volume
- Adequate bandwidth for expected user traffic
- 99% uptime SLA as a baseline quality standard
Installing Ruby, Rails, and Bundler on a Fresh Server
Begin by preparing your Linux environment. Start with system updates (apt update && apt upgrade), then install dependencies needed to compile Ruby, such as build-essential, libssl-dev, libreadline-dev, and zlib1g-dev. The popular way to install Ruby is through rbenv, which lets you manage multiple Ruby versions on the same server. After Ruby is installed, add the Rails gem and Bundler, which handles dependency management for your Rails application. Using a version manager like rbenv is especially useful if you need to run multiple Ruby versions or upgrade Ruby without breaking existing applications.
- Update system packages with apt update && apt upgrade
- Install build tools and libraries required to compile Ruby
- Use rbenv to install and manage Ruby versions
- Install Rails gem after Ruby is installed
- Install Bundler to manage application dependencies
- Consider using gemsets for isolated gem environments per project
Choosing an Application Server: Puma vs Unicorn
The application server is the process that maintains HTTP connections and responds to user requests. Puma is the most popular choice today because it handles concurrency efficiently through threading, allowing it to manage multiple requests on a single process. This makes Puma more resource-efficient in memory-constrained environments. Unicorn is an older server that uses separate processes for each request; while it still works, Puma's superior performance and lower resource footprint make it the recommended choice for new Rails applications. Puma also supports features like hot restarts and built-in SSL termination, providing more flexibility for modern deployment scenarios.
- Puma uses threading to handle multiple requests within one process
- Unicorn spawns separate processes, consuming more memory
- Puma has lower memory overhead than Unicorn
- Puma supports built-in SSL termination
- Puma configuration is simpler and more maintainable
- Puma is the default application server in modern Rails versions
Setting Up Nginx as a Reverse Proxy for Rails
Nginx is a lightweight, fast web server that serves perfectly as a reverse proxy for Rails applications. It sits in front of your Puma server, accepting requests from the internet and forwarding them to your application processes. To configure Nginx, you create a configuration file that specifies the location of your Puma server (usually localhost:3000 or localhost:8080). Nginx also handles caching of static files, compresses responses to reduce bandwidth usage, and normalizes URLs. This separation between Nginx and Puma allows you to restart your application without bringing down the web server, and makes your infrastructure more flexible and secure.
- Configure Nginx upstream block pointing to Puma
- Define server block for your application domain
- Enable gzip compression to reduce response size
- Set proxy headers to forward client information to Puma
- Disable the default server block for security
- Use try_files to serve precompiled assets efficiently
Database Setup for Rails: PostgreSQL vs MySQL
The database is central to storing your application data. Rails works seamlessly with both PostgreSQL and MySQL. PostgreSQL offers advanced features such as native JSON support, full-text search, and array data types, making it excellent for applications needing sophisticated querying and data processing. MySQL, particularly its open-source fork MariaDB, is lightweight and fast, suitable for smaller applications where advanced features are not critical. Modern Rails applications typically default to PostgreSQL due to its rich feature set and excellent Rails integration. Whichever you choose, install the appropriate Ruby database gem (pg for PostgreSQL, mysql2 for MySQL), configure your database.yml file, and set up secure database credentials on your server.
- PostgreSQL excels with advanced features and complex queries
- MySQL/MariaDB is lightweight and suitable for smaller apps
- Install the appropriate Ruby database adapter gem
- Configure Rails database.yml with correct connection details
- Use strong, unique passwords for database accounts
- Consider automated backups for your database
- Set up read replicas for high-traffic applications
Asset Pipeline: Precompilation and Production Serving
The Asset Pipeline is a Rails feature that compiles and minifies JavaScript, CSS, and images for production deployment. While development environments often serve uncompressed assets, production requires running rake assets:precompile to generate minified, fingerprinted files optimized for caching. These compiled assets are stored in public/assets and should be served directly by Nginx for maximum performance, bypassing the Rails application server. Fingerprinting ensures that when you update an asset, clients fetch the new version rather than serving a cached outdated file. This approach greatly reduces bandwidth and improves page load times.
- Run rake assets:precompile during deployment to generate minified assets
- Fingerprinting ensures cache-busting for updated files
- Serve public/assets directly from Nginx for performance
- Set Cache-Control headers for long-term caching
- Consider a CDN for global asset distribution
- Remove old precompiled assets to save disk space
Background Jobs: Sidekiq and Hosting Considerations
Background jobs are essential for applications that need to handle long-running tasks, such as sending emails, generating reports, or processing images. Sidekiq is a popular job processing library for Rails that uses Redis to queue and manage tasks. Instead of making users wait for these operations to complete, Sidekiq lets you queue the job in Redis and have separate worker processes handle it in the background. To host a Rails app using Sidekiq, you must install and run Redis and configure Sidekiq workers as separate system processes, typically managed by systemd or supervisor. Properly monitoring Sidekiq ensures that jobs complete successfully and that your background processes do not consume excessive resources.
- Install Redis to back Sidekiq's job queue
- Configure Sidekiq worker processes to run as services
- Use systemd or supervisor to manage worker processes
- Set appropriate concurrency and memory limits
- Monitor Sidekiq dashboard for job success and failure rates
- Implement Dead Letter Queue handling for failed jobs
Deployment Approaches: Capistrano, Docker, and CI/CD
Deployment is the process of moving your code from development to production servers. Capistrano is a traditional deployment tool that scripts the release process, automating tasks like pulling the latest code, installing gems, running migrations, and restarting the application server. Docker offers an alternative: deploying containerized versions of your application with all dependencies and runtime versions bundled together, ensuring consistency across environments. Modern teams also use CI/CD pipelines (such as GitHub Actions or GitLab CI) to automate testing and deployment whenever code is pushed to your repository. Choosing a deployment strategy depends on your application complexity, team size, and infrastructure preferences. Each approach has trade-offs between simplicity, flexibility, and operational overhead.
- Capistrano automates remote execution via SSH for seamless deployments
- Docker containers ensure deployment consistency across environments
- CI/CD pipelines automatically test and deploy on code pushes
- Use zero-downtime deployment techniques to maintain availability
- Implement automated rollback mechanisms for failed deployments
- Monitor deployment metrics to detect and respond to issues
Frequently Asked Questions
Can Ruby on Rails applications run on shared hosting?
Generally, no. Rails applications require a persistent application process running at all times, which shared hosting platforms (typically designed for PHP) cannot provide. Rails requires either a VPS or dedicated server where you have full control to install Ruby, Bundler, and necessary tools. Some hosting providers offer specialized Rails hosting, but these come at a higher cost than basic shared hosting.
How much RAM does a typical medium-sized Rails app need?
Memory usage depends on your application's size and complexity. Each Puma process typically consumes 100-300 MB of RAM. If you run 4 Puma workers, expect around 400-1,200 MB just for the application. Add overhead for Nginx, Redis, your database, and the operating system, and you can see why 4 GB of RAM or more is recommended for production Rails applications. Larger applications may require 8 GB or more.
What is the difference between Puma and Unicorn?
The key difference is that Puma uses threads (multiple threads within a single process) while Unicorn uses multiple separate processes (each single-threaded). This makes Puma more memory-efficient. Puma is also more modern, actively maintained, and well-integrated with current Rails versions. Unless you have a specific reason to use Unicorn, Puma is the better choice for new Rails applications.
How do I safely deploy updates to a live Rails application?
The safest approach is using zero-downtime deployment techniques, such as rolling deployments supported by Capistrano. A proper deployment process should run database migrations first, gracefully close existing connections, and start new application processes. Automated testing via CI/CD pipelines catches issues before they reach production. It is also wise to keep database backups and have a rollback plan in case something goes wrong.
Which operating system works best for hosting Rails applications?
Linux, particularly distributions like Ubuntu, Debian, or CentOS, is the standard choice for hosting Ruby on Rails. Linux is cost-effective, stable, well-documented, and has a large community of Rails developers providing support and sharing knowledge. macOS is suitable for local development but not recommended for production servers. Windows is not a good fit for Rails hosting due to limited tooling and community support.