Skip to content

Instantly share code, notes, and snippets.

@danielmoralesp
Last active February 2, 2023 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielmoralesp/e83bc94bbf642506a56dccc05c1b5fe0 to your computer and use it in GitHub Desktop.
Save danielmoralesp/e83bc94bbf642506a56dccc05c1b5fe0 to your computer and use it in GitHub Desktop.
D.O + Ubuntu 18 + Ruby 2.7.1
# Inside Digital Ocean
create new project
name project (without resources)
get started with a droplet
settings: Ubuntu 16.04 + 10$ + USA-New York 3
# Now, instead of emailing you a one-time password, you can create a root password to access your droplet.
# So, create password there (see pass document)
create droplet
# Setting root and deploy users
ssh root@IPADDRESS
continue
password created in last step
current: again the same
# add user deploy
# inside root server
sudo adduser deploy
new UNIX password (same as last step)
retype
#enter to all data here and yes
sudo adduser deploy sudo
su deploy
# ssh-copy
#in your computer, not in your server run
ssh-copy-id deploy@IPADDRESS
type deploy password
exit from server
ssh deploy@IPADDRESS
## installing ruby
deploy@1.2.3.4
# 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
deploy@1.2.3.4
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
deploy@1.2.3.4
# 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
deploy@1.2.3.4
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
deploy@1.2.3.4
# 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;
deploy@1.2.3.4
sudo service nginx start
deploy@1.2.3.4
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;
}
}
deploy@1.2.3.4
sudo service nginx reload
## creating database
deploy@1.2.3.4
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, "git@github.com: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 deploy@1.2.3.4
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:PASSWORD@127.0.0.1/myapp
# For MySQL
DATABASE_URL=mysql2://deploy:$omeFancyPassword123@localhost/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.
deploy@1.2.3.4
# 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
## logs commands
#to see the Nginx process logs
sudo journalctl -u nginx
#to check the Nginx access logs
sudo vi /var/log/nginx/access.log
#to check Nginx error logs
sudo vi /var/log/nginx/error.log
#to check your Flask app’s uWSGI logs
sudo journalctl -u myproject
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
# If ngnix is shutting down unexpectly each day, sollow this instructions: https://serverfault.com/questions/1003361/how-to-automatically-restart-nginx-when-it-goes-down
Use monit which purpose is to take care of situations like this.
apt install monit
nano /etc/monit/conf.d/nginx.conf
Put content below inside this file and restart monit
check process nginx with pidfile /var/run/nginx.pid
start program = "/usr/sbin/service nginx start"
stop program = "/usr/sbin/service nginx stop"
service monit restart
### if it fails (ngnix hard kill)
sudo pkill -f nginx & wait $!
sudo systemctl start nginx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment