On this page
- What does the finished setup look like?
- How do you prepare Ruby, PostgreSQL and Nginx?
- How do you create the database role?
- How do you get the code and its secrets onto the server?
- How do you prepare the database and assets?
- How do you run Puma as a systemd service?
- How do you configure Nginx and HTTPS?
- How do you deploy updates without surprises?
- How do you troubleshoot a broken deploy?
- Questions
What does the finished setup look like?
Requests arrive at Nginx on ports 80 and 443, which answers static files from the app public folder and hands everything else to Puma on the loopback interface. Puma runs the Rails code as the deploy user under systemd, which restarts it on failure and at boot. PostgreSQL listens only locally. Rails 8 also keeps its cache, background jobs and Action Cable state in the database by default (Solid Cache, Solid Queue, Solid Cable), so no separate Redis server is needed to get started.
This page targets Ubuntu 24.04 and 22.04 and Debian 12 and 13. Plan on at least 2 GB of memory: Puma with a couple of workers, PostgreSQL and asset compilation together do not fit comfortably in 1 GB.
How do you prepare Ruby, PostgreSQL and Nginx?
Install Ruby for the deploy user with rbenv first, following installing Ruby with rbenv, and choose the version your app lists in .ruby-version. Then add the system services and the client headers the pg gem compiles against:
sudo apt update
sudo apt install -y postgresql libpq-dev nginx certbot python3-certbot-nginx git
psql --version
systemctl status postgresql --no-pagerThe distribution PostgreSQL is fine for most apps; if you need a newer major, the PostgreSQL guide shows the project repository. Rails 8 uses import maps by default, so Node.js is only needed if your app bundles JavaScript with esbuild, Vite or similar tools.
How do you create the database role?
Create a login role named after the app with permission to create databases, because db:prepare creates the primary database plus the cache, queue and cable databases on first run. Pick a long random password.
sudo -u postgres createuser --createdb --pwprompt blog
sudo -u postgres psql -c "\du blog"Rails reads the password from an environment variable. In config/database.yml, check that the production entries use username: blog and password: <%= ENV["BLOG_DATABASE_PASSWORD"] %>, which is what rails new blog -d postgresql generates, and add host: 127.0.0.1 to the default block so the connection uses password authentication over TCP instead of peer authentication on the socket.
How do you get the code and its secrets onto the server?
Clone the repository as deploy, configure Bundler for production and install gems. Then store secrets in an environment file that only root and the deploy user can read.
cd ~
git clone https://git.example.net/team/blog.git
cd blog
bundle config set --local deployment true
bundle config set --local without 'development test'
bundle installsudo mkdir -p /etc/blog
sudo nano /etc/blog/blog.envRAILS_ENV=production
RAILS_MASTER_KEY=paste-the-contents-of-config-master-key
BLOG_DATABASE_PASSWORD=the-password-you-chose
SOLID_QUEUE_IN_PUMA=1
RAILS_MAX_THREADS=3
WEB_CONCURRENCY=2sudo chown root:deploy /etc/blog/blog.env
sudo chmod 640 /etc/blog/blog.envconfig/master.key is never committed, so copy its value from your workstation; without it Rails cannot decrypt credentials.yml.enc. SOLID_QUEUE_IN_PUMA tells the generated config/puma.rb to run background jobs inside Puma, which suits a single server.
How do you prepare the database and assets?
Load the environment file into your shell, then run the two production tasks. db:prepare creates missing databases, loads the schema and runs pending migrations; assets:precompile writes fingerprinted files into public/assets.
set -a; source /etc/blog/blog.env; set +a
bin/rails db:prepare
bin/rails assets:precompile
bin/rails runner 'puts Rails.env; puts ActiveRecord::Base.connection.select_value("select version()")'The last line proves Rails can reach PostgreSQL in production mode. If it fails with an authentication error, check the password and the host line; if it complains about decryption, the master key is wrong.
How do you run Puma as a systemd service?
A unit file starts Puma through the rbenv shim, so the version in .ruby-version is used automatically. The -b option keeps Puma on localhost no matter what the config file says.
sudo nano /etc/systemd/system/blog.service[Unit]
Description=Blog Rails app (Puma)
After=network.target postgresql.service
[Service]
Type=simple
User=deploy
WorkingDirectory=/home/deploy/blog
EnvironmentFile=/etc/blog/blog.env
ExecStart=/home/deploy/.rbenv/shims/bundle exec puma -C config/puma.rb -b tcp://127.0.0.1:3000
ExecReload=/bin/kill -USR1 $MAINPID
Restart=on-failure
RestartSec=3
[Install]
WantedBy=multi-user.targetsudo systemctl daemon-reload
sudo systemctl enable --now blog
sudo ss -ltnp | grep 3000
curl -sI http://127.0.0.1:3000/up -H "X-Forwarded-Proto: https"Rails 8 apps include a /up health route that returns 200 when the app boots. systemctl reload blog sends USR1 for a phased restart that replaces workers one at a time.
How do you configure Nginx and HTTPS?
Nginx serves compiled assets straight from disk with long cache headers and passes all other requests to Puma. It needs to traverse the deploy home directory, which is private by default on Ubuntu 24.04, so allow execute-only access first.
sudo chmod 711 /home/deploy
sudo nano /etc/nginx/sites-available/blogupstream blog_puma {
server 127.0.0.1:3000;
}
server {
listen 80;
listen [::]:80;
server_name blog.example.net;
root /home/deploy/blog/public;
client_max_body_size 20m;
location /assets/ {
expires max;
add_header Cache-Control "public, immutable";
try_files $uri =404;
}
location / {
try_files $uri @puma;
}
location @puma {
proxy_pass http://blog_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;
}
}sudo ln -s /etc/nginx/sites-available/blog /etc/nginx/sites-enabled/
sudo rm -f /etc/nginx/sites-enabled/default
sudo nginx -t && sudo systemctl reload nginx
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
sudo certbot --nginx -d blog.example.netPoint the DNS records at 198.51.100.24 and 2001:db8:4f2a::24 before running Certbot. Production Rails 8 sets force_ssl, so plain HTTP requests redirect to HTTPS once the certificate is in place. More on certificate renewal is in the Certbot guide.
How do you deploy updates without surprises?
Keep one routine and run it every time: fetch code, install gems, migrate, compile assets, restart. A small script avoids forgotten steps.
#!/usr/bin/env bash
set -euo pipefail
cd /home/deploy/blog
set -a; source /etc/blog/blog.env; set +a
git pull --ff-only
bundle install
bin/rails db:migrate
bin/rails assets:precompile
sudo systemctl restart blog
curl -fsS -o /dev/null -H "X-Forwarded-Proto: https" http://127.0.0.1:3000/up && echo upTake a database dump before migrations that change or drop columns; pg_dump -Fc blog_production > ~/blog-$(date +%F).dump is enough for a quick rollback, and the PostgreSQL backup guide covers schedules. The optional daily backups on BitcoinVPS protect the whole disk as an extra layer.
How do you troubleshoot a broken deploy?
Start with the service journal, then work outward.
- 502 Bad Gateway: Puma is not running or not on port 3000; read
journalctl -u blog -n 50. - InvalidMessage or missing secret_key_base:
RAILS_MASTER_KEYis wrong or missing in the environment file. - PG::ConnectionBad: wrong password, missing
hostline, or PostgreSQL stopped. - Assets return 403: Nginx cannot traverse the home directory; recheck
chmod 711. - Blocked hosts error:
config.hostsinproduction.rbdoes not include your domain.
Rails logs to standard output in production by default, so all application errors show up in the journal next to Puma messages.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about Deploy Ruby on Rails
Do I need Redis for Rails 8?
Not by default. Solid Cache, Solid Queue and Solid Cable store their data in the database. Add Redis later only if you choose Sidekiq or another tool that requires it.
Can I use Kamal instead of this manual setup?
Yes. Kamal deploys Rails in Docker containers over SSH and works on any server with root access. The manual route on this page has fewer moving parts and suits a single app.
How many Puma workers should I run?
Start with one worker per vCPU and three to five threads each, then watch memory. Each worker holds a full copy of the app, often 150 to 300 MB.
Where do the Rails logs go?
To standard output, which systemd stores in the journal. Read them with journalctl -u blog -f.
Can I use MySQL or SQLite instead of PostgreSQL?
Yes. Rails supports both; change the adapter in database.yml and install libmysqlclient-dev or keep SQLite files on local disk with regular backups.