maddison joyce
  • about me
  • blog
  • get in touch
Sep052014

My Very Own Deployments Guide

For about the 5th time in the past 3 months, I’m in the process of setting up another deployment server to host my web apps and it’s starting to get that repetitive feeling. Every time, I think I should take notes and then everytime I forget to, so here goes.

In case you’re wondering, I’m certainly not the first to do this, so I turned to the steadfast wisdom of Lord Google ™ and read through a few articles. This post is my own concoction, but many chunks of it will feel familiar having read through the resources below.

My Resources:

  • Jon McCartie’s Blog
  • Vladi Gleba’s 6-Parter
  • The Flying Developer

My Stack:

  • Ubuntu
  • Nginx
  • Rbenv
  • Unicorn
  • Mina

Step 1- Get Yourself A Server

Hopefully you were expecting that. There’s plenty of choices, but I’ve used Amazon, Digital Ocean and now Rackspace. If you’re really keen, you could always build it yourself, but I don’t have that time on my hands.

Spec-wise, it’s really up to you, and you’ll obviously need to think about who’s using the server, but here’s my preference:

  • Something with at least 1GB of RAM, for my unicorn workers.
  • Storage can be pretty minimal, it only stores the rails app. I hate file uploads, so I tend to use carrierwave + some service (Imgur, CloudFiles, Dropbox, etc.).
  • My preferred Server OS is Ubuntu. I’ve been using it since I was 7, it’s easy and I’m yet to find a reason to change. Generally, I’ll pick the LTS version, because, you know, it’s the LTS version.

I also have a couple of commands I run every time I setup a server, because nano is the bane of my existence:

apt-get update
apt-get install -y vim
update-alternatives --config editor
# Then hit the number for vim

Step 2- Get Yourself A User (And A Group)

Now the server’s up and running- or you’re still fiddling with a graphics card- let’s get a user for our deployments. You can use the root user, but that’s a bit dodgy. So let’s setup that user:

groupadd deployers
adduser deploy -ingroup deployers

Now edit /etc/sudoers:

%deployers  ALL=(ALL) ALL

And add your ssh key:

su deploy
cd ~
mkdir .ssh
cd .ssh
vim authorized_keys
# Paste your public keyfile (ssh-keygen -t rsa)

From here on, I’m just going to assume you’re logged in as the new deploy user.

Finally, make sure your server is up to date, and sort out time zones so you don’t have to work out what was happening at 3am later on:

sudo apt-get -y update
sudo apt-get -y upgrade
sudo dpkg-reconfigure tzdata

Step 3- Get Yourself A Proxy

In case you didn’t know- and because I didn’t- Nginx is actually a proxy server (which makes it a bit different to Apache). This is what makes it perfect for our use, we’ll setup Nginx to proxy requests to our unicorn workers.

sudo add-apt-repository ppa:nginx/stable
sudo apt-get -y update
sudo apt-get -y install nginx
sudo service nginx start

You can edit /etc/nginx/nginx.conf if you want, but mine tends to stay pretty standard:

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
  worker_connections 768;
}

http {
  # Basic Settings

  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 65;
  types_hash_max_size 2048;

  server_name_in_redirect off;

  include /etc/nginx/mime.types;
  default_type application/octet-stream;

  # Logging Settings

  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;

  # Gzip Settings

  gzip on;
  gzip_disable "msie6";

  # Virtual Host Configs

  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
}

Step 4- Get Yourself A Ruby

There’s a whole thing out there about http://rvm.io/ and http://rbenv.org/ and which runs faster, jumps higher and all the rest, but I prefer to stay out of inane internet arguments for my own sanity. Right now, I’m preferring Rbenv, because it feels easier and lighter, but both are awesome, so go with your gut.

First we install rbenv and a bunch of dependencies:

sudo apt-get -y install git
sudo curl -L https://raw.github.com/fesplugas/rbenv-installer/master/bin/rbenv-installer | bash
sudo apt-get -y install zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libxml2-dev libxslt1-dev autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev nodejs libcurl3 libcurl4-openssl-dev

There’s a step in there that rbenv will tell you to do, but I seem to forget it all the time. Edit ~/.bash_profile:

export RBENV_ROOT="${HOME}/.rbenv"

if [ -d "${RBENV_ROOT}" ]; then
  export PATH="${RBENV_ROOT}/bin:${PATH}"
  eval "$(rbenv init -)"
fi

Then reload your environment:

source ~/.bash_profile

Now that rbenv is ready to go, install a ruby version (2.2.0 for me as of now) and bundler, because it’s awesome:

rbenv install 2.2.0
rbenv global 2.2.0
gem install bundler

Step 5- Get Yourself A Unicorn

It appears I got a little distracted here and forgot to update. This will be updated- soon, I hope.

Edit (2015-07-31): Ok, so it looks like this isn’t getting updated. I’ll guess we’ll find out how to do it next time I have to setup a Ruby-Rails-Unicorn-Nginx-Mina stack. That’s my bad.

Step 6- Get Yourself A Mina

I’ve always used Capistrano as my deployment tool (other than the dark ages of FTP) but I really like the look of Mina, so I’m giving it a try.

First, add the mina gems:

gem 'mina'
gem 'mina-unicorn', :require => false

Then set it up:

bundle install
bundle exec mina init

Then all it takes is a single command:

bundle exec mina deploy

Maddison Joyce

+61 420 360 967

[email protected]

Who am I? I'm Maddison Joyce. I've worked in all areas of software development and before that I was in tech support. I love writing code, learning new technologies, reading books and talking to people. I can help you with your next software project.

Twitter

GitHub

LinkedIn

© Maddison Joyce, 2014.