Multiple Rails Apps On One Digital Ocean Droplet

How to have multiple rails app on one digital ocean droplet

  1. Create a new nginx server block on your droplet with the name of your app
sudo nano /etc/nginx/sites-enabled/NAMEOFYOURAPPHERE
  1. Add the nginx server code
server {
  listen 80;
  listen [::]:80;

  server_name WEBSITEURL www.WEBSITEURL;
  root /home/deploy/APPNAME/current/public;

  passenger_enabled on;
  passenger_app_env production;

  location /cable {
    passenger_app_group_name APPNAME_websocket;
    passenger_force_max_concurrent_requests_per_process 0;
  }

  # Allow uploads up to 100MB in size
  client_max_body_size 100m;

  location ~ ^/(assets|packs) {
    expires max;
    gzip_static on;
  }
}
  1. Create the production database for your app

3.1 change your user: sudo su - postgres 3.2 create the db: createdb -O db-deploy APP_DB_NAME

  1. Add capistrano to your rails app Gemfile
gem 'capistrano', '~> 3.11'
gem 'capistrano-rails', '~> 1.4'
gem 'capistrano-passenger', '~> 0.2.0'
gem 'capistrano-rbenv', '~> 2.1', '>= 2.1.4'

4.1 Run bundle install

4.2 Run cap install STAGES=production

4.3 Add lines to Capfile:

require 'capistrano/rails'
require 'capistrano/passenger'
require 'capistrano/rbenv'

set :rbenv_type, :user
set :rbenv_ruby, '2.6.3'

4.4 Modify config/deploy with app git repo details:

set :application, "myapp"
set :repo_url, "git@github.com:username/myapp.git"

# Deploy to the user's home directory
set :deploy_to, "/home/deploy/#{fetch :application}"

append :linked_dirs, 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', '.bundle', 'public/system', 'public/uploads'

# Only keep the last 5 releases to save disk space
set :keep_releases, 5

4.5 Modify config/deploy/production.rb with your server id

server '1.2.3.4', user: 'deploy', roles: %w{app db web}

  1. Go into the server to add environment variables
     ssh deploy@1.2.3.4
     mkdir /home/deploy/myapp
     nano /home/deploy/myapp/.rbenv-vars
    

5.1 Add your secret key base and database url (check other apps for reference

# For Postgres
DATABASE_URL=postgresql://deploy:PASSWORD@127.0.0.1/myapp

# For MySQL
DATABASE_URL=mysql2://deploy:$omeFancyPassword123@localhost/myapp

RAILS_MASTER_KEY=ohai
SECRET_KEY_BASE=1234567890

STRIPE_PUBLIC_KEY=x
STRIPE_PRIVATE_KEY=y
# etc...

Join my newsletter

If you want to receive my latest essays and interesting finds subscribe to my list: