Skip to content

Instantly share code, notes, and snippets.

@Netznarkose
Created March 7, 2018 10:44
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Netznarkose/93a65affc9d928136adf504916fa44d2 to your computer and use it in GitHub Desktop.
the whole journey #DevOps

Rails 4, Apache2, Passenger, RVM, Capistrano3

Sources:

Passenger: https://www.phusionpassenger.com/library/deploy/apache/automating_app_updates/ruby/
DigitalOcean/Apache: https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-passenger-and-apache-on-ubuntu-14-04

Setup the Server

digitalocean.com: create Droplet (ubuntu 14.04.5 x32)

Create deploy-User

local: ssh root@139.59.185.172

https://gist.github.com/Netznarkose/098295c54b8b8bf402564fc7491e3eab

<script src="https://gist.github.com/Netznarkose/098295c54b8b8bf402564fc7491e3eab.js"></script>

Add public-ssh-key of local machine to the Server

deploy@ubuntu: mkdir .ssh deploy@ubuntu: chmod 700 .ssh
deploy@ubuntu: vi .ssh/authorized_keys
local: pbcopy < ~/.ssh/id_rsa.pub
deploy@ubuntu: sudo chmod 600 .ssh/authorized_keys

Create a public-ssh-key and add to to Github

deploy@ubuntu: ssh -T git@github.com
deploy@ubuntu: ssh-keygen -t rsa
deploy@ubuntu: cat .ssh/id_rsa.pub
github: add ssh-key
deploy@ubuntu: sudo apt-get install git
deploy@ubuntu: git clone test_repository_to_check_connection
deploy@ubuntu: exit

test that deploy user has access

local: ssh deploy@139.59.185.172

restrict access of root user

root@ubuntu: vi /etc/ssh/sshd_config
change PermitRootLogin yes => no
root@ubuntu: service ssh restart

test that root user does not have access

local: ssh root@139.59.185.172

Install RVM & Ruby

deploy@ubuntu: sudo apt-get update
deploy@ubuntu: gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
deploy@ubuntu: curl -sSL https://get.rvm.io | bash -s stable
deploy@ubuntu: source ~/.rvm/scripts/rvm
deploy@ubuntu: rvm install 2.2.6
deploy@ubuntu: rvm use 2.2.6 --default

Create a Gemset and Install Bundler

deploy@ubuntu: rvm gemset create my_app
deploy@ubuntu: rvm 2.2.6@my_app do gem install bundler

Install Apache

deploy@ubuntu: sudo apt-get update
deploy@ubuntu: sudo apt-get install apache2

Install Passenger

deploy@ubuntu: sudo apt-get install -y dirmngr gnupg
deploy@ubuntu: sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7
deploy@ubuntu: sudo apt-get install -y apt-transport-https ca-certificates
deploy@ubuntu: sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger trusty main > /etc/apt/sources.list.d/passenger.list'
deploy@ubuntu: sudo apt-get update
deploy@ubuntu: sudo apt-get install -y libapache2-mod-passenger
deploy@ubuntu: sudo a2enmod passenger
deploy@ubuntu: sudo apache2ctl restart

Check Installation

deploy@ubuntu: sudo /usr/bin/passenger-config validate-install
deploy@ubuntu: sudo /usr/sbin/passenger-memory-stats check for Apache-Processes

Configure Passenger

deploy@ubuntu: sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/my_app.conf
deploy@ubuntu: sudo vi /etc/apache2/sites-available/my_app.conf

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/my_app/current/public
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    <Directory "/var/www/my_app/current/public">
        RailsEnv production
        Options FollowSymLinks
        Require all granted
    </Directory>
</VirtualHost>

Capistrano-friendly directory structure

deploy@ubuntu: sudo mkdir -p /var/www/my_app/shared
deploy@ubuntu: sudo chown deploy: /var/www/my_app /var/www/my_app/shared

Create initial configuration files

deploy@ubuntu: sudo mkdir -p /var/www/my_app/shared/config
deploy@ubuntu: sudo touch /var/www/my_app/shared/config/database.yml
deploy@ubuntu: sudo touch /var/www/my_app/shared/config/secrets.yml
deploy@ubuntu: sudo chown -R deploy: /var/www/my_app/shared/config
deploy@ubuntu: chmod 600 /var/www/my_app/shared/config/database.yml
deploy@ubuntu: chmod 600 /var/www/my_app/shared/config/secrets.yml

create my_app

check if rails 4.2.10 is installed locally

local: gem list | grep rails

if not, install it!

local: gem install rails -v 4.2.10
local: rails _4.2.10_ new my_app -T
Github: create repository my_app
local/my_app: git remote add origin git@github.com:Netznarkose/my_app.git
local/my_app: vi Gemfile

group :development do
  gem 'capistrano'
  gem 'capistrano-passenger', '>= 0.1.1'
  gem 'capistrano-bundler'
  gem 'capistrano-rails'
  gem 'capistrano-rvm'
end

local/my_app: bundle install
local/my_app: cap install
local/my_app: vi Capfile

require 'capistrano/bundler'
require 'capistrano/passenger'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
require 'capistrano/rvm'

local/my_app: vi config/deploy.rb

set :application, "my_app"
set :repo_url, "git@github.com:Netznarkose/my_app.git"
set :deploy_to, "/var/www/my_app"

local/my_app: vi config/deploy/production.rb

server "139.59.185.172", user: "deploy", roles: %w{app db web}
set :rails_env, 'production'
set :rvm_ruby_version, 'ruby-2.2.6@my_app'
set :branch, 'master'

local/my_app: cap production deploy

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