Graham Stevens
⟵ Home

My Pelican Adventures, Powered by Dropbox

This is a legacy post referring to how this blog was originally setup, using Pelican and Dropbox. It no longer reflects the current state of affairs, which can be read about here.


This website is powered solely by the wonderful static blogging engine known as Pelican. A static blogging engine means that each time a change is made i.e. a new post is created, the entire site is rebuilt as static HTML & CSS pages. This may sound laborious, but does mean that everything is extremely simple once setup correctly. For example, my work flow is as follows:

  1. Write a post in Mou or any plain text editor, using Markdown syntax
  2. Save it to a blog folder within Dropbox, either as a completed post, or with a status of draft
  3. Refresh Grh.am

It is as simple as that once everything is set up.

Setup

I only have experience of installing Pelican on a server, or more specifically, a low end VPS box – I imagine being Python based that it may not be possible to install on a standard shared hosting plan.

If I am honest, VPS’s are so cheap these days, there is no excuse, plus it’s a great way to learn about Linux! For offers, see LowEndBox.com, which is where I found my UK & US servers… I don’t really need two but I couldn’t resist, they are just so cheap.

This guide assumes your server is running and has a web server installed, such as Apache, Lighttpd or Nginx.

I had recently updated my Debian installation to Wheezy (7), which comes with Python 2.7 unlike Squeeze (6) which comes with 2.6. Pelican 3.2 and above requires at least Python 2.7, so it may be necessary to install the correct version of Python in order to use Pelican, however the vast majority of Linux distributions meet this requirement. If for whatever reason you require more than one Python version installed on your system, it may be wise to utilise Virtualenv & Virtualenvwrapper.

To install Pelican, we must first ensure that Pip, the Python package manager, is installed:

curl -O http://python-distribute.org/distribute_setup.py

python distribute_setup.py

easy_install pip

Next, we can actually install Pelican using the following command:

pip install pelican

We can also install some additional packages here, to allow us to use the Markdown language and some useful typography tweaks1:

pip install markdown typogrify

And that is all it takes to install Pelican – very simple and over within a few minutes. Next up, configuration.

Configuration

The easiest way to create a new blog with pelican is to execute the following command, within the folder that you wish everything to be stored. Let’s install it to the home directory within a folder called ‘blog’:

cd ~
mkdir blog
cd blog
pelican-quickstart

So first we are changing the directory to “~”, which is short for the home directory of the current user. Mkdir creates the directory or folder, and then we change directory to ‘blog’. Pelican-quickstart will guide you through a number of different options which you can tailor to your tastes.

We now have all the necessary files to start our static blog. Next we must install Dropbox:

cd ~ && wget -O - "https://www.dropbox.com/download?plat=lnx.x86" | tar xzf -
wget -O usr/bin/dropbox "http://www.dropbox.com/download?dl=packages/dropbox.py"
chmod +x usr/bin/dropbox

This installs the Dropbox client, and the command line interface for the client. So we are going to start Dropbox and link it to our account:

~/.dropbox-dist/dropboxd

You’ll be asked to copy and paste a link in a browser to create a new account or add your server to an existing account. Once you do, your Dropbox folder will be created in your home directory.

Once all this is configured, we can check on Dropbox with the following:

dropbox status

You should be told it is either Idle, or actively trying to sync your entire Dropbox contents to your server - not ideal. So lets setup a selective sync:

dropbox exclude add *

This should add all the top-level folders to the exclusion list, from which we can remove the single folder that will contain our blog posts and pages. If you find this does not work for any reason, try the following script2 while in your home folder:

cd ~
dropbox exclude add `ls Dropbox/ -1D \
| awk '{ { gsub(" ","\\ ") }; \
{gsub("\\(","\\(")};\
{gsub("\\)","\\)")};\
print "~/Dropbox/" $_ }' \
| tr '\n' ' '`

Now we need to remove our blog folder from the exclusion list, so lets run the following command:

dropbox exclude remove blog

Your folder structure should be as follows:

blog/
├── content
│   ├── posts
│   ├── pages
│   ├── media
│   └── extras

To explain, posts will contain your blog posts or articles in a format of your choice (personally, Markdown). Pages contains static pages, such as About or Contact pages. Media can contain files for your pages, i.e. images for your blog posts. Extras can contain static files, such as ‘favicon.ico’ and ‘robots.txt’.

For the files within the extras folder to be used within the build, we need to add a couple of lines to the pelican configuration file. You may need to install nano (apt-get install nano)

nano ~/blog/pelicanconf.py

And add the following:

FILES_TO_COPY = (('extra/robots.txt', 'robots.txt'),
				('extra/favicon.ico', 'favicon.ico'),)

Exit nano using ctrl+x, Y, then enter.

Building

Time to build our blog, using a simple command:

pelican /path/to/your/content/ [-s path/to/your/settings.py -o path/to/your/output]

Anything in square brackets is optional, however these parameters are needed for using our Dropbox folders.

pelican ~/Dropbox/blog/content [-s ~/blog/pelicanconf.py -o /var/www/]

This will build our blog using the files within the content folder, using the settings within the blog configuration folder, and output it to the web server folder.

Automation

So now that our blog is building via command line, lets automate the process so that when a file is changed within the Dropbox folder, the blog update automatically.

First, we will create a bash script to execute:

cd ~/blog
nano update_blog.sh

#!/bin/bash
/usr/local/bin/pelican ~/Dropbox/blog/content -o /var/www/ -s ~/blog/pelicanconf.py

Now that we have a single script to call, we can install a program to run this everything a change occurs within the Dropbox folder/

apt-get install incron

incrontab -e

By adding the following line to incrontab, any file that is modified within the posts folder will execute the script.

~/Dropbox/Blog/content/Posts IN_MODIFY,IN_CREATE,IN_DELETE,IN_MOVE ~/blog/update_blog.sh

Notes

This may need tweaking to your specific situation, depending on file locations and such, but you should be able to get the general gist of things by following this guide. Hopefully you will be up and running with an automated static blog in no time at all.


$ whoami I am Graham Stevens, a Cyber Security Specialist based in the South West of the UK. If you're lucky, I very occasionally post updates on Twitter/Mastodon.