Skip to content

Instantly share code, notes, and snippets.

@jooeycheng
Last active December 31, 2022 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jooeycheng/36e256f9e43bc260c24101f8c865b513 to your computer and use it in GitHub Desktop.
Save jooeycheng/36e256f9e43bc260c24101f8c865b513 to your computer and use it in GitHub Desktop.
Ubuntu 16.04 - Rails + Capistrano + Passenger + Nginx

Add ruby-version

# .ruby-version

2.5.1

Set up Capistrano

https://www.digitalocean.com/community/tutorials/deploying-a-rails-app-on-ubuntu-14-04-with-capistrano-nginx-and-puma

# Gemfile

group :development do
  gem 'capistrano',           require: false
  gem 'capistrano-rbenv',     require: false
  gem 'capistrano-rails',     require: false
  gem 'capistrano-bundler',   require: false
  gem 'capistrano-passenger', require: false
end
bundle exec cap install
# Capfile

require "capistrano/rbenv"
require "capistrano/bundler"
require "capistrano/rails/assets"
require "capistrano/rails/migrations"
require "capistrano/passenger"

Use ENV variables

# config/database.yml

production:
  <<: *default
  database: unibored_production
  username: <%= ENV['MYAPP_DATABASE_USERNAME'] %>
  password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>

Setup Rails secret key

Generate key with

bundle exec rake secret

Setup & Deploy

bundle exec cap production setup
bundle exec cap production deploy

Create user deploy

https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-16-04

Setup NodeJS

https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04

Setup Ruby + Rails

https://www.digitalocean.com/community/tutorials/how-to-install-ruby-on-rails-with-rbenv-on-ubuntu-16-04

Setup PostgreSQL

https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-16-04

Setup Passenger + Nginx

https://www.phusionpassenger.com/library/walkthroughs/deploy/ruby/digital_ocean/nginx/oss/xenial/install_passenger.html

Configure Nginx

Remove default config at /etc/nginx/sites-available/default or comment out the default_server lines. Create new config file for site:

# /etc/nginx/sites-available/myapp.conf

server {
    listen 80 default_server;
    server_name myapp.com;

    root /var/www/myapp/current/public;

    passenger_enabled on;
    passenger_app_env production;
    passenger_ruby /home/deploy/.rbenv/versions/2.5.1/bin/ruby;
     
    passenger_env_var LANG en_US.UTF-8;
    passenger_env_var MYAPP_DATABASE_USERNAME myapp;
    passenger_env_var MYAPP_DATABASE_PASSWORD super-strong-password;
}

Symlink config to sites-enabled

Symlink to /etc/nginx/sites-enabled/myapp.conf to enable the site

ln -s /etc/nginx/sites-available/myapp.conf /etc/nginx/sites-enabled/myapp.conf

Update app directory permission

Set user deploy as owner of /var/www/myapp so Capistrano can mkdir to it https://stackoverflow.com/questions/24470520/capistrano-mkdir-permission-denied

sudo chown deploy:deploy /var/www/myapp

Setup Let's Encrypt HTTPS

https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04


Additional useful information

Nginx folder structure https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-16-04

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment