Jekyll on a shared Dreamhost server

This blog started as a set of static pages, mostly updated in a plain text editor, as I have yet to find a blogging tool that does not get in a way of writing. There is ups and downs of running your own infrastructure for blogging. Fundamentally I’m a firm believer in the “own your data” philosophy so I gave Jekyll a try.

What is Jekyll?

Jekyll is a parsing engine used to build static websites from dynamic components such as templates, partials, liquid code, markdown etc. You can install it as a ruby gem on your local computer and use it to generate a static website, no databases, Wordpresses or other baggage. You can read more about Jekyll here.

What’s in it for me?

I got tired of manually updating the static HTML pages, for instance the “Next” and “Previous” post links. With Jekyll I just push a post markdown file via Git to the server and let Jekyll do the heavy lifting: update the homepage with new post, include the “Next” and “Previous” links, update previous post. With Jekyll making global updates, like changing the post template are a breeze. Just update the template in one place are Jekyll re-generates the appropriate pages to match the new template.

Jekyll on a local computer vs Jekyll on a server

Whether or not you want to install Jekyll on a server depends on your blogging workflow. You have two options:

  1. Local Jekyll - run Jekyll on a local machine and upload the generated, static website to the server
  2. Jekyll on the server + Git - run Jekyll on the server, use Git to push a post to the server and have Jekyll re-generate the website on the sever

I love geeking out so I tried option #2 - Jekyll on a server + Git. So whatever you see now is a result of geeking out :)

Dreamhost + Jekyll

  • LOCAL MACHINE - do this in a terminal on your local computer
  • SERVER - do thins in a terminal on the server

Step 1 SSH into your Dreamhost account

LOCAL MACHINE>ssh your-user-name@your-domain.com

Step 2 Install RVM

Install RVM. It’s the Ruby Version Manager you need to update the Dreamhost default Ruby version, because Jekyll needs Ruby >= 1.9.3.

SERVER> curl -L get.rvm.io | bash -s stable

You can get a “GPG signature verification failed” warning. To fix this import Michal Papis (RVM maintainer) GPG keys with:

SERVER>curl -sSL https://rvm.io/mpapis.asc | gpg --import -

Now set a temp environment variable

SERVER>foo='[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"'' 

Add that variable to the .profile and re-load it with updated rvm paths

SERVER> echo $foo >> ~/.bash_profile
SERVER> echo $foo >> ~/.bashrc

To start using RVM run its post install script.Look at the RVM post install comments for the path if you’re not sure where you installed RVM

SERVER>source /your-home/path/.rvm/scripts/rvm

Step 3

Before installing a modern Ruby version disable autolibs, since you do not have access to the root user

SERVER>rvm autolibs disable

Install Ruby 2.0.0 + set it as default + install therubyracer (need ExecJS to process coffeescript)

SERVER> rvm install 2.0.0
SERVER> rvm --default use 2.0.0
SERVER> gem install therubyracer

Step 4 Install Jekyll with Pygments (for syntax highllighting)

SERVER> gem install jekyll
SERVER> mkdir ~/soft && mkdir ~/packages && mkdir ~/packages/lib && mkdir ~/packages/lib/python && cd ~/soft
SERVER> echo 'export PYTHONPATH=\"$HOME/packages/lib/python:/usr/lib/python2.6\"' >> ~/.bash_profile
SERVER> source ~/.bash_profile
SERVER> wget http://pypi.python.org/packages/source/P/Pygments/Pygments-1.6.tar.gz
SERVER> tar -xvzf Pygments-1.6.tar.gz
SERVER> cd Pygments-1.6
SERVER> python setup.py install --home=$HOME/packages 

Step 5 Generating SSH keys for auto SSH login

Setup public key SSH login on Dreamhost to push Git updates over SSH with automatic, key-based login

First check if local machine SSH keys exist

LOCAL MACHINE>ls -al ~/.ssh

If the .ssh directory is not found or you do not see any files starting with id_ generate the local machine SSH keys with the command below:

LOCAL MACHINE>ssh-keygen -t rsa -C "user@yourSERVER.com"

It generates the SSH keys which then upload to the SERVER. If the .ssh directory does not exist on the SERVER create it first.

LOCAL MACHINE>scp ~/.ssh/id_rsa.pub "user@yourSERVER.com":~/

SERVER> cat id_rsa.pub >> .ssh/authorized_keys
SERVER> rm id_rsa.pub
SERVER> chmod go-w ~
SERVER> chmod 700 ~/.ssh
SERVER> chmod 600 ~/.ssh/authorized_keys

STEP 6 Setup Git repos for Jekyll

LOCAL MACHINE> mkdir jekyll-local-repo && cd jekyll-local-repo
LOCAL MACHINE> git init 

Git works best when there is files in the repo :) Add your Jekyll files to repo directory - create them with the ‘jekyll build’ command. More info here

LOCAL MACHINE> git add * 
LOCAL MACHINE> git commit -m "Initial commit" 

SERVER> mkdir jekyll-server-repo && cd jekyll-server-repo
SERVER> mkdir jekyll-blog.git && cd jekyll-blog.git
SERVER> git --bare init

LOCAL MACHINE> git remote add origin ssh://myUser@myDomain.com/home/myUser/jekyll-server-repo/jekyll-blog.git
LOCAL MACHINE> git push --all origin

STEP 7 Setup Git to run Jekyll when a post is pushed to the server

On the server navigate to /home/myUser/gitprojects/repo.git/hooks and edit the post-receive file. If there is no post-receive file create one.

SERVER>cd /home/myUser/gitprojects/repo.git/hooks
SERVER> touch post-receive

Here’s what you should put into the post-receive file, given you want to host your blog under www.yourDomain.com/jekyll

#!/bin/sh
GIT_REPO=$HOME/jekyll-server-repo/jekyll-blog.git
TMP_GIT_CLONE=$HOME/jekyll
PUBLIC_WWW=$HOME/www.yourDomain.com/jekyll

git clone $GIT_REPO $TMP_GIT_CLONE
jekyll build --source $TMP_GIT_CLONE --destination $PUBLIC_WWW
cd ~ 
rm -rf $TMP_GIT_CLONE
exit

Make post-receive file executable

SERVER> chmod ug+x post-receive

STEP 8 Does it work?

Navigate to the Jekyll git repo on the local machine. Edit _config.yml and change to title unter site setting to “It WORKS!”. Next push the local updates to the server

LOCAL MACHINE> cd jekyll-local-repo
LOCAL MACHINE> git add *
LOCAL MACHINE> git commit -m "Will this work?"
LOCAL MACHINE> git push --all origin

You should see something like this

Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 315 bytes, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Cloning into '/home/www.yourDomain.com/tmp/jekyll'...
remote: done.
remote: Configuration file: /home/www.yourDomain.com/tmp/jekyll/_config.yml
remote:             Source: /home/www.yourDomain.com/tmp/jekyll
remote:        Destination: /home/userName/www.yourDomain.com/jekyll
remote:       Generating...
remote:                     done.
remote:  Auto-regeneration: disabled. Use --watch to enable.

In the browser navigate to www.yourDomain.com/jekyll. You should see a plain Jekyll blog. Congratulations!

CREDITS

The guide about is based on an excellent Jekyll Dreamhost installation guide by Nicholas Sorrell.