Skip to content

Instantly share code, notes, and snippets.

@oliveira-andre
Last active July 24, 2020 15:26
Show Gist options
  • Save oliveira-andre/a83199e512a8513d19e3b0016a41d51f to your computer and use it in GitHub Desktop.
Save oliveira-andre/a83199e512a8513d19e3b0016a41d51f to your computer and use it in GitHub Desktop.
deploy production rails
#enter on server
ssh [email protected]
# adding a user to deploy
adduser deploy
adduser deploy sudo
exit
# copy your ssh file to not need to put your pass
ssh-copy-id [email protected]
ssh-copy-id [email protected]
ssh [email protected]
# Adding Node.js 10 repository
curl -sL https://deb.nodesource.com/setup_10.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 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev dirmngr gnupg apt-transport-https ca-certificates redis-server redis-tools imagemagick nodejs yarn
# configuring rbenv
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
# 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 nginx
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
# change the line passenger_ruby to the code above:
# passenger_ruby /home/deploy/.rbenv/shims/ruby;
sudo vim /etc/nginx/conf.d/mod-http-passenger.conf
sudo service nginx start
sudo rm /etc/nginx/sites-enabled/default
sudo vim /etc/nginx/sites-enabled/myapp
# put this code
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;
}
}
sudo service nginx reload
# installing and setting up postgres
sudo apt-get install postgresql postgresql-contrib libpq-dev
sudo su - postgres
createuser --pwprompt deploy
createdb -O deploy myapp_production
exit
# go to your localmachine and put it in your gemfille
gem 'capistrano', '~> 3.11'
gem 'capistrano-rails', '~> 1.4'
gem 'capistrano-passenger', '~> 0.2.0'
gem 'capistrano-rbenv', '~> 2.1', '>= 2.1.4'
# run on terminal
bundle
bundle exec cap install STAGES=production
# on Capfile put the code above
require 'capistrano/rails'
require 'capistrano/passenger'
require 'capistrano/rbenv'
set :rbenv_type, :user
set :rbenv_ruby, '2.7.1'
# on config/deploy.rb put the code above
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}"
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
# put on config/deploy/production.rb
server '1.2.3.4', user: 'deploy', roles: %w{app db web}
# now run
ssh [email protected]
mkdir /home/deploy/myapp
vim /home/deploy/myapp/.rbenv-vars
# put the code above on your rbenv-vars
# For Postgres
DATABASE_URL=postgresql://deploy:[email protected]/myapp
# For MySQL
DATABASE_URL=mysql2://deploy:$omeFancyPassword123@localhost/myapp
RAILS_MASTER_KEY=ohai
SECRET_KEY_BASE=1234567890 # to get this code only run: bundle exec rails secret
STRIPE_PUBLIC_KEY=x
STRIPE_PRIVATE_KEY=y
# etc...
# exit from server and run on you project
cap production deploy
# now lets configure the lets encrypt
ssh [email protected]
cd ~
git clone https://github.com/letsencrypt/letsencrypt
cd letsencrypt/
./letsencrypt-auto
sudo ls /etc/letsencrypt/live/oliveira-andre.dev/
sudo apt-get install -y ruby-mini-magick
sudo service nginx restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment