Migrating from Wordpress to Pelican on PaaS - Part 3

Posted by Dave on 12 February 2014

The final installment in this three part series. This covers installing Dokku and publishing your pelican blog to you new Docker-powere mini-Heroku.

Part 3: Publishing to PaaS with Dokku

The Plan

If you haven't read Part 1 or Part 2 yet, this should give you some background as to what I'm doing, why I'm doing it and how I built it. In this installment I'll focuse on the publishing side of things.

Hosting

My former blog was hosted on a Linode 1024 VPS, which had a healthy 1GB RAM. I've been very happy with Linode and would recommend them to anybody who needs hosting, but for the convenience of having prebuild Ubuntu images with Dokku installed, I opted to host my blog with DigitalOcean. They have a full tutorial on their website that makes this very easy to set up.

One of the big benefits of using a static site generator is that the memory requirement is a lot less than Apache+PHP or Nginx+PHP. I'm hosting my site now on a $5/month VM from DigitalOcean which is a $15/month saving on my Wordpress site.

Before publishing...

Once you have your Dokku installation set up, you can push your application to Dokku with git. Dokku will take this, use a buildpack to build your application, and provision a 'slug' at the domain you provided during the setup.The 'slug' used appears after the : in the commands below, which adds your dokku installation as a remote git repository

:::bash
git remote add dokku dokku@<your_domain>:blog

Before we can push the application, we need to do a few things. First we need to create a .env file in our repository. This file tell Dokku to use these environment variables when buiding the app. We use BUILDPACK_URL to specify a custom buildpack for Pelican. Fortunately such a buildpack is provided by the Pelican project on GitHub. Unfortunately this doesn't work with Dokku. For now, I am using my own fork until I can contribute the fix back upstream. You can find my fork here. The PELICAN_SITEURL is used to allow my blog to be accessible from the url above or from 'blog.davetucker.co.uk' which is the domain I'm using for my Dokku install:

:::bash
export BUILDPACK_URL=https://github.com/dave-tucker/heroku-buildpack-pelican
export PELICAN_SITEURL=http://dtucker.co.uk

As I was migrating from Wordpress, I didn't want my old links to break (as when links break, Google kills a kitten). To fix this, I created a custom nginx.conf for may application by including a nginx.conf.erb file. In my fork of the buildpack, I configure nginx with PCRE support so it can do URL rewriting. I also took this opportunity to clear up my categories. If you aren't familiar with nginx, you can read the documentation on the HTTP Rewrite Module here

:::bash
daemon off;
worker_processes 4;

events {
  use epoll;
  worker_connections 1024;
}

http {
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;

  gzip_static on;

  include mime.types;

 <% if ENV["PELICAN_SITEURL"] %>
  server {
    listen <%= ENV["PORT"] %>;
    rewrite ^(.*) <%= ENV["PELICAN_SITEURL"] %>$1 permanent;
  }
 <% end %>

  server {
    listen <%= ENV["PORT"] %>;
    port_in_redirect off;
    root /app/public;

    error_page 500 502 503 504 /pages/50x.html;
    error_page 404 /pages/404.html;

    if ($http_user_agent !~ (FeedValidator|FeedPress)){
        rewrite ^/feed/all.rss.xml$ http://feedpress.me/davetucker redirect;
    }

    rewrite ^/networking/(using-the-latest-open-vswitch-with-devstack-and-opendaylight)$ /hack/$1.html permanent;
    rewrite ^/virtualization/(bypassing-the-memory-check-on-a-vmware-esxi-5)$ /hack/$1.html permanent;
    rewrite ^/programming/(.*)$ /hack/$1.html permanent;
    rewrite ^/productivity/(.*)$ /lifehack/$1.html permanent;
    rewrite ^/networking/(.*)$ /work/$1.html permanent;
    rewrite ^/misc/electronics/(.*)$ /make/$1.html permanent;
    rewrite ^/misc/rants/(.*)$ /rants/$1.html permanent;
    rewrite ^/uncategorized/(.*)$ /work/$1.html permanent;
    rewrite ^(/about-me)$ /pages/about-me.html permanent;

    location / {
      expires 10s;
    }
  }
}

Publishing

Now we have set everything up, we can publish using a single git command

:::bash
git push dokku master

If you are lazy like me, you might want to create a git alias

:::bash
git config --local alias.publish 'push dokku master' 
git publish

Your app should now get built and you can browse to it at <slug>.<yourdomain>

Workflow

My workflow is now as follows:

  1. Open up Sublime Text
  2. Write a blog post
  3. git add --all
  4. git commit -a -m "New Post: <Title>"
  5. git publish

Closing Thoughts

Pelican is an awesome Python-powered static site generator which provides everything you need to host a great blog. This combined with a Dokku gives me a really great writing/publishing workflow... One of the things I'm missing from Wordpress is the ability to schedule posts, but I am learning to live without it. Overall, I'm very happy since the move and I hope that documenting this will help or encourage others to take the plunge.

@dave_tucker