Back-end Engineering Articles

I write and talk about backend stuff like Ruby, Ruby On Rails, Databases, Testing, Architecture / Infrastructure / System Design, Cloud, DevOps, Backgroud Jobs, and more...

Twitter:
@daniel_moralesp

2020-01-17

Digital Ocean + Ubuntu 18 + Ruby 2.7.1

Original link: https://gist.github.com/danielmoralesp/e83bc94bbf642506a56dccc05c1b5fe0

## Create droplet in Digital ocen from $5
## Ubuntu 18.04

   Local Machine
ssh [email protected]


   [email protected]
adduser deploy
adduser deploy sudo
exit

   Local Machine
ssh-copy-id [email protected]
ssh-copy-id [email protected]
### solution to the problem with copy-ssh: 
https://www.digitalocean.com/community/questions/ssh-copy-id-not-working-permission-denied-publickey

   Local Machine
ssh [email protected]

## installing ruby

  [email protected]
# Adding Node.js repository
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
# Adding Yarn repository
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo add-apt-repository ppa:chris-lea/redis-server
# Refresh our packages list with the new repositories
sudo apt-get update
# Install our dependencies for compiiling Ruby along with Node.js and Yarn
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev dirmngr gnupg apt-transport-https ca-certificates redis-server redis-tools nodejs yarn

 [email protected]
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
git clone https://github.com/rbenv/rbenv-vars.git ~/.rbenv/plugins/rbenv-vars
exec $SHELL
rbenv install 2.7.1
rbenv global 2.7.1
ruby -v
# ruby 2.7.1

   [email protected]
# This installs the latest Bundler, currently 2.x.
gem install bundler
# For older apps that require Bundler 1.x, you can install it as well.
gem install bundler -v 1.17.3
# Test and make sure bundler is installed correctly, you should see a version number.
bundle -v
# Bundler version 2.0

## Installing NGnix
[email protected]
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7
sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger bionic main > /etc/apt/sources.list.d/passenger.list'
sudo apt-get update
sudo apt-get install -y nginx-extras libnginx-mod-http-passenger
if [ ! -f /etc/nginx/modules-enabled/50-mod-http-passenger.conf ]; then sudo ln -s /usr/share/nginx/modules-available/mod-http-passenger.load /etc/nginx/modules-enabled/50-mod-http-passenger.conf ; fi
sudo ls /etc/nginx/conf.d/mod-http-passenger.conf

  [email protected]
# If you want to use the Nano for editing
sudo nano /etc/nginx/conf.d/mod-http-passenger.conf
# If you want to use the Vim for editing
sudo vim /etc/nginx/conf.d/mod-http-passenger.conf

We simply want to change the passenger_ruby line to match the following:

passenger_ruby /home/deploy/.rbenv/shims/ruby;

  [email protected]
sudo service nginx start

  [email protected]
sudo rm /etc/nginx/sites-enabled/default
# If you want to use the Nano for editing
sudo nano /etc/nginx/sites-enabled/myapp
# If you want to use the Vim for editing
sudo vim /etc/nginx/sites-enabled/myapp

Change myapp to the name of your app. We'll use this same folder later on when we define our Capistrano deploy_to folder.

server {
  listen 80;
  listen [::]:80;

  server_name _;
  root /home/deploy/myapp/current/public;

  passenger_enabled on;
  passenger_app_env production;

  location /cable {
    passenger_app_group_name myapp_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;
  }
}

[email protected]
sudo service nginx reload

## creating database

[email protected]
sudo apt-get install postgresql postgresql-contrib libpq-dev
sudo su - postgres
createuser --pwprompt deploy
createdb -O deploy myapp
exit

### ## OJOO, UPGRADE DIGITAL OCEAN TO ASSETS:PRECOMPLIE
LOCAL ASSETS PRECOMPILE
CHANGE UGLIFIER
CHANGE SQLITE3 AND ADD POSTGRES

gem 'capistrano', '~> 3.11'
gem 'capistrano-rails', '~> 1.4'
gem 'capistrano-passenger', '~> 0.2.0'
gem 'capistrano-rbenv', '~> 2.1', '>= 2.1.4'

 Local Machine
bundle
cap install STAGES=production

We're need to edit the Capfile and add the following lines:

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

set :rbenv_type, :user
set :rbenv_ruby, '2.7.1'

Then we can modify config/deploy.rb to define our application and git repo details.

set :application, "myapp"
set :repo_url, "[email protected]:username/myapp.git"

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

### SEEMS LIKE I DONT NEED APPLICATION.YML, AND OTHERS HERE

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

Now we need to modify config/deploy/production.rb to point to our server's IP address for production deployments. Make sure to replace 1.2.3.4 with your server's public IP.

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

 Local Machine
ssh [email protected]
mkdir /home/deploy/myapp
nano /home/deploy/myapp/.rbenv-vars

#### in local, my credentiasl needs to be like this:
EDITOR="atom --wait" bin/rails credentials:edit

development:
  secret_key_base: c29c343acasdadad

production:
  secret_key_base: asdadasd


####
Add any environment variables you need for production to this file.

# For Postgres
DATABASE_URL=postgresql://deploy:[email protected]7.0.0.1/myapp

# For MySQL
DATABASE_URL=mysql2://deploy:[email protected]/myapp

RAILS_MASTER_KEY=  ### OJOO ADD MASTER.KEY
SECRET_KEY_BASE= ### OJOO ADD SECRET FROM LOCAL

STRIPE_PUBLIC_KEY=x
STRIPE_PRIVATE_KEY=y

#### I'LL NEED TO DO THIS
EDITOR="atom --wait" bin/rails credentials:edit

#### change sqlite3 in local to deb and pg to production
#### change UGlifier
#### run rails assets:precomplie in local

### UPGRADE DORPLET TO $10 USD WHILE FIRST DEPLOYMENT

 Local Machine
cap production deploy

If you see an error, you can SSH into the server and view the log files to see what's wrong.

   [email protected]
# To view the Rails logs
less /home/deploy/myapp/current/log/production.log
# To view the NGINX and Passenger logs
sudo less /var/log/nginx/error.log

Once you find your error (often times a missing environment variable or config for production), you can fix it and restart or redeploy your app.

### FINAL ERROR... RUBY VERSION IN SERVER... I HAD AN ERROR TELLING ME THAT A FILE HAVE RUBY 2.3 AND IM RUNNING 2.7. THE FILE WAS: THE ERROR IS IN LOCAL FILE .ruby-version, CHANGE IN LOCAL TO 2.7.1 AND RE-DEPLOY
AND SERVER IS FINALLYYYY RUNNING ON WEB SERVER


### OTHERS

GEMFILE
 # Capistrano rails console
gem 'capistrano-rails-console', require: false

CAPFILE
require 'capistrano/rails/console'

CAPISTRANO TASKS
lib/capistrano/tasks/seed.rake

namespace :deploy do
  desc "realod the database with seed data"
  task :seed => [:set_rails_env] do
    on primary fetch(:migration_role) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :rake, 'db:seed'
        end
      end
    end
  end

  after 'deploy:migrate', 'deploy:seed'
end

## config/deploy.rb
set :passenger_restart_with_touch, true
## load seeds
load 'lib/capistrano/tasks/seed.rake'

#Restarting The Site
touch my_app_name/current/tmp/restart.txt