Skip to content

Instantly share code, notes, and snippets.

@nathancolgate
Created March 11, 2019 14:23
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 nathancolgate/36c9a1b58ffc7db54cd0fe5eb3fd5319 to your computer and use it in GitHub Desktop.
Save nathancolgate/36c9a1b58ffc7db54cd0fe5eb3fd5319 to your computer and use it in GitHub Desktop.
How we setup servers from 2014 - 2016
# Digital Ocean / Rails 4.1.6 / Ruby 2.1.3 / nginx 1.4.6 / Ubuntu 14.04x64 / Capistrano 3.2.1
# https://gorails.com/deploy/ubuntu/14.04
# https://coderwall.com/p/yz8cha/deploying-rails-app-using-nginx-unicorn-postgres-and-capistrano-to-digital-ocean
# http://www.cubicleapps.com/articles/ubuntu-rails-ready-with-nginx-unicorn
# 1) SETUP DEPLOY USER AND GET ROOT ACCESS OUT OF THE WAY
you@local:~$ ssh root@IPADDRESS
root@remote:~$ adduser USERNAME
root@remote:~$ visudo
# # User privilege specification
# root ALL=(ALL:ALL) ALL
# USERNAME ALL=(ALL:ALL) ALL # Add
root@remote:~$ vi /etc/ssh/sshd_config
# Port PORT # change this to whatever port you wish to use: 1025..65536
# UseDNS no # Add to bottom
# AllowUsers USERNAME # Add to bottom
# AllowAgentForwarding yes # Add to bottom
root@remote:~$ reload ssh
root@remote:~$ exit
# Hand way to automagically upload your ssh key(s) to the server
you@local:~$ ssh-copy-id USERNAME@IPADDRESS -p PORT
# 2) INSTALL TIME
you@local:~$ ssh deploy@IPADDRESS -p PORT
deploy@remote:~$ sudo apt-get update
deploy@remote:~$ sudo apt-get install curl
deploy@remote:~$ gpg --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3 # RVM 1.26.0 introduces signed releases and automated check of signatures when GPG software found.
deploy@remote:~$ curl -L get.rvm.io | bash -s stable
deploy@remote:~$ source ~/.rvm/scripts/rvm
deploy@remote:~$ rvm requirements
deploy@remote:~$ rvm install 2.1.3
deploy@remote:~$ rvm use 2.1.3 --default
deploy@remote:~$ rvm rubygems current
deploy@remote:~$ gem install rails --no-ri --no-rdoc
deploy@remote:~$ sudo apt-get install nodejs
# 3) LOCAL POSTGRESQL DATABASE (OPTIONAL)
deploy@remote:~$ sudo apt-get install postgresql postgresql-server-dev-9.3
deploy@remote:~$ gem install pg -- --with-pg-config=/usr/bin/pg_config
deploy@remote:~$ sudo -u postgres psql
postgres@psql:~$ create user deploy with password 'l2gVdgDDZmFOAGw';
postgres@psql:~$ alter role deploy superuser createrole createdb replication;
postgres@psql:~$ create database ksdetasn_production owner deploy;
postgres@psql:~$ \q # Quits
# 3) LOCAL MYSQL DATABASE (OPTIONAL)
deploy@remote:~$ sudo apt-get install mysql-server
deploy@remote:~$ mysql -u root -p
mysql> CREATE USER 'deploy'@'localhost' IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON * . * TO 'deploy'@'localhost';
mysql> FLUSH PRIVILEGES;
mysql> quit
# 3) REMOTE MYSQL DATABASE (OPTIONAL)
deploy@remote:~$ sudo apt-get install libmysqlclient-dev
# 3.0) IMAGEMAGICK (OPTIONAL)
deploy@remote:~$ sudo apt-get install imagemagick
# 3.1) CONTINUED INSTALLATION
deploy@remote:~$ sudo apt-get install git-core
deploy@remote:~$ rvm use 2.1.3@PROJECTNAME --create
deploy@remote:~$ gem install bundler
deploy@remote:~$ sudo apt-get install nginx
deploy@remote:~$ sudo service nginx start
# At this point you should be able to see the nginx welcome page at your IPADDRESS
# 3.2) MOHR INSTALLATION
deploy@remote:~$ sudo apt-get install memcached # << Crazy, I know... but that's all it takes. No setup. No config.
# 3.2) LOCAL CAPISTRANO
# In your Gemfile
gem 'capistrano', '3.4.0'
gem 'capistrano-rvm'
gem 'capistrano-bundler'
gem 'capistrano-rails'
you@local:~$ bundle
you@local:~$ cap install
# Copy over "Capfile" from this gist. No substitutions needed.
# Copy over "production.rb" from this gist. Substitutions: SPECIAL_PORT, USERNAME, IPADDRESS
# Copy over "deploy.rb" from this gist. Substitutions: APPLICATION, RUBY, USERNAME
# 4) LOCAL SERVER CONFIG FILES
# In your gemfile
gem 'unicorn'
# Copy over "config/nginx.conf" from this gist. Substitutions: USERNAME, PROJECTNAME
# Copy over "config/unicorn.rb" from this gist. Substitutions: USERNAME, PROJECTNAME
# Copy over "config/unicorn_init.sh" from this gist. Substitutions: USERNAME, PROJECTNAME, RUBY
you@local:~$ chmod +x config/unicorn_init.sh
# Commit all that hard work you did! Good job, you!
you@local:~$ git add .
you@local:~$ git commit -m "Oh yeah"
you@local:~$ git push
# 5) Setup and Deploy Capistrano
# Setup ~/apps/APPLICATION/shared/config/secrets.yml
# I had to install bundler gem... not sure how that got stepped over
# I think it was becdause when I first installed the bundler gem, I was not inside my application gemset. I've changed that above
# Debug as necessary
you@local:~$ cap production deploy
# 6) LINK NGINX AND UNICORN
deploy@remote:~$ sudo ln -nfs CURRENTPATH/config/nginx.conf /etc/nginx/sites-enabled/projectname
deploy@remote:~$ sudo rm /etc/nginx/sites-enabled/default
deploy@remote:~$ sudo service nginx restart
deploy@remote:~$ sudo ln -nfs CURRENTPATH/config/unicorn_init.sh /etc/init.d/unicorn_projectname
deploy@remote:~$ sudo update-rc.d -f unicorn_projectname defaults
# 6) Get into Prodution Console
# A good first step. Means you're on the right path.
# Debug as necessary
deploy@remote:~$ bundle exec rails c production
# 7) RVM WRAPPER FOR UNICORN
# http://rvm.io/deployment/init-d
USERNAME@remote:~$ cd ~/apps/PROJECTNAME/shared/bin
USERNAME@remote:~$ rvm wrapper 2.1.3@PROJECTNAME PROJECTNAME unicorn_rails
# This will create:
# /home/USERNAME/.rvm/wrappers/ruby-2.1.3@PROJECTNAME/unicorn_rails
# Use this fully qualified path in your unicorn_init.sh
# 8) Lock Down
USERNAME@remote:~$ sudo vi /etc/ssh/sshd_config
# PermitRootLogin yes # change this to no
USERNAME@remote:~$ sudo reload ssh
# 9) TAKEAWAYS
--------------------------------------------------------------------
Unicorn
Log: /home/deploy/apps/projectname/current/log/unicorn.log
Config: /home/deploy/apps/projectname/config/unicorn.rb
Control: /etc/init.d/unicorn_projectname start|stop|restart
--------------------------------------------------------------------
Nginx
Generic Nginx Log: /var/log/nginx/*
Application Specific Log: /home/deploy/apps/projectname/current/log/nginx.*
Config: /etc/nginx/sites-enabled/projectname
Control: sudo service nginx start|stop|restart
--------------------------------------------------------------------
To get a list of available gems: gem list
To get a list of Rubies: rvm list
To get Gem environment: gem env
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment