Jekyll Deployment with DigitalOcean

shared on May 07, 2015

For as long as I’ve been using Jekyll, I never quite figured out the automated deployment thing. Every time I updated this site (and others), I would then open Transmit and copy the folders/files that were changed. Sometimes, I would forget something and have to upload again. Terrible.

Late last night/this morning, I decided to take a crack at it.

DigitalOcean is where I host my sites and the only way you can access your servers is via SSH or SFTP. For this tutorial, I am using Ubuntu and Apache. Mina and Capistrano seem to be the most popular methods for deploying static sites to servers. Mina has proven to be confusing to my feeble mind so I looked for Capistrano tutorials. I found one(05.04.16 update, link’s dead), but it requires an older version of Capistrano for reasons I will detail below. That’s not a problem, just a heads-up.

# this is the latest version of Capistrano before 3.0, which removes a key feature
gem install capistrano -v 2.15.5

Assuming you are already at the base of your local project in Terminal…

capify .

This will automatically add two files to your Jekyll project:

You probably don’t need to edit anything in your Capfile, but I like to change single quotes to double quotes in all project files, for consistency. You do need to edit the deploy.rb file found in your new config folder. I have provided a copy of mine below.

# replace this with your site's name
set :application, "SampleApp"

# your autogenerated Jekyll site resides here, as you know
set :repository, "_site"

# !!! Capistrano 3 does away with this useful line
set :scm, :none

set :deploy_via, :copy
set :copy_compression, :gzip
set :use_sudo, false

# the name of the user that should be used for deployments on your VPS
set :user, "admin"

# the path to deploy to on your VPS
# this is the most common path on Apache configs, update your own accordingly
set :deploy_to, "/var/www/html"

# the ip address of your VPS
role :web, "123.456.78.900"

before "deploy:update", "deploy:update_jekyll"

# your images and CSS may not appear to work after the symlink process
# this rule and subsequent code fixes this issue
after "deploy:create_symlink", "deploy:fix_permissions"

namespace :deploy do
  [:start, :stop, :restart, :finalize_update].each do |t|
    desc "#{t} task is a no-op with jekyll"
    task t, :roles => :app do ; end

  desc "Run jekyll to update site before uploading"
  task :update_jekyll do
    # clear existing _site
    # build site using jekyll
    # remove Capistrano stuff from build
    %x(rm -rf _site/* && jekyll build && rm _site/Capfile && rm -rf _site/config)

  desc "Fix permissions"
  task :fix_permissions do
    # chmod files on the server
    run "chmod 775 -R #{current_path}"
    run "chmod 644 -R #{current_path}/.htaccess"

After you are done modifying the script above to your specifications, run…

cap deploy:setup

You will only need to run this setup command once, unless you change server parameters in the future. Finally, the code to deploy your Jekyll generated site to your server!

cap deploy

Now, what happens when you deploy is your site will be pushed to a symlinked folder on your server called current. So, if your regular server path for your site is /var/www/html, it will now be /var/www/html/current. You will need to update your Apache config to represent that so when visitors go to your site, they will actually see the well, current, version. This StackOverflow answer explains it best (emphasis mine):

In Ubuntu the Apache configuration is located at /etc/apache2. There should be two folders, sites-available and sites-enabled. Inside sites-enabled are symlinks to config files in sites-available.

You simply have to change the document root in your activated configuration. Thats probably /etc/apache2/sites-enabled/000-default

Have a look for DocumentRoot and change it to /var/www/new, then reload your apache.

To reload your cache, SSH into your server via Terminal and run:

service apache2 reload

And that’s it! Gone are the days when I left the Terminal to upload things via multiple clicks like a savage.

P.S: I noticed that the deploy script did not exclude the Capistrano files in the generated Jekyll site, so I added these lines to my _config.yml file:

  - Capfile
  - config
Updated 05.14.15 to update deploy.rb script with chmod rules
Updated 05.04.16 to remove link, add note to another link, and for grammar, wtf. How did I go so long without seeing this?!!
Jekyll Deployment with DigitalOcean


Home Me Projects Thoughts Resumé Imprint


Dribbble GitHub Miiverse Twitter