Categories
Posts

Installing Pelican and Dropbox on CentOS

Along with my new site design, I moved from a mixture of static HTML and Scriptogr.am to a site that’s managed by Pelican.

If you don’t already know, Pelican is a Python static blog generator. It isn’t a content management system like WordPress though – there’s no database or even a web interface. Instead, all your content is flat-file based and managed by the CLI. Posts and pages are created using either reStructuredText or Markdown (my preferred writing method). Because all posts are stored flat-file, you’re able to manage your posts and if necessary, migrate them elsewhere. This was perfect since Scriptogr.am appears to be Pelican-based, I was able to simply move the files and all posts (including permalinks) were kept exactly as they were.

I write and save all my posts to Dropbox and although Pelican includes Dropbox functionality, it’s not the best way to utilise Dropbox. So, here’s a quick post on installing and running Pelican and Dropbox on CentOS.

Note: The following has only been tested on a minimal install of CentOS 6.2 on a VPS. If you’re on shared hosting, the below is likely not to work. You’ll also need root access so make sure to sudo su appropriately.

#####Step 1: Check your version of Python#####

Pelican requires Python 2.6+ (Pelican 3.2 requires Python 2.7+). My version of CentOS 6.2 shipped with Python 2.6.6. To see which version of Python you’re running, SSH into your server and enter Python at the CLI. You’ll receive the response along the lines of:

Python 2.6.6 (r266:84292, Sep 11 2012, 08:34:23).

#####Step 2: Install easy_install#####

This is a Python module that provides setup tools to quickly download and install Python packages. To do this, enter:

$ yum install -y python-setuptools

#####Step 3: Install Pelican#####

Once the setup tools are installed, you can then install Pelican. Simply enter:

$ easy_install pelican

#####Step 4: Setup Your Pelican Blog#####

Navigate to your www folder where you’ll want to save your site and content to (for example, /home/pelican/site), then enter:

$ pelican-quickstart

Follow the onscreen instructions and set up your blog. You will be prompted to use Dropbox, you don’t want to at this stage (we’re using our own setup for this) so just select no.

Once you’ve built the pelican setup, it’s time to test it. Enter:

$ make html and provided no errors were detected, your site will be available in the ./output folder.

#####Step 5: Install Dropbox#####

Dropbox’s instructions on installing are pretty self explanatory. Enter:

$ cd ~ && wget -O - "https://www.dropbox.com/download?plat=lnx.x86" | tar xzf -

Before we continue, we’re going to create a new Dropbox account. Why are we doing this? Simply for security. In the unlikely event your server is hacked, they won’t have access to your primary Dropbox account.

  1. Create a new account.
  2. In your primary Dropbox account, create a new folder and then share it via Dropbox to your new account.
  3. Log in to your new Dropbox account and confirm the shared folder.

Now we’ve shared the folder, we can then save files into it and the new account will automatically receive them, but since it’s not your own Dropbox account, all that personal data isn’t stored on a server.

(Sure, you can exclude folders but again, if someone hacks into your server they can simply remove the exclusions and have full access to your Dropbox).

Once you’ve created a new account, head back to your SSH session and enter:

$ ~/.dropbox-dist/dropboxd

This will start the Dropbox daemon. Watch the terminal window, it’ll continually generate a unique URL that you need to copy/paste into your browser. Do that and remember to be signed into the new Dropbox account.

#####Step 5: Dropbox Script#####

Dropbox have a Python script available to manage Dropbox from the CLI. Enter

$ cd ~ && wget https://www.dropbox.com/download?dl=packages/dropbox.py

The script will download to your home folder. To run it, enter

$ ./dropbox.py

You’ll then see all sorts of instructions about managing it. We don’t need it here but it’s worth having to make sure we know Dropbox is running.

#####Step 6: Symbolic Link#####

By default, Pelican looks for content within the ./content folder in the folder you set it up in. So, if you used the Pelican quickstart in /home/pelican/site then your content folder would be /home/pelican/site/content. All your posts and pages should reside here. During the initial setup, you can specify the folder location. I found that I had to edit the Makefile to change this path since running make html seemed to ignore the path set in the pelicanconf.py file it created. Ignore all this, instead, we’ll just symlink the folder.

Remove the content folder and then create a symbolic link called content to your newly created (and synced) folder in Dropbox. For example, if your Dropbox folder is located in /home/user/Dropbox with the sub-folder posts that you created, we’d do this:

$ ln -s /home/user/Dropbox/posts /home/pelican/site/content

Pelican will then still look in the content folder but it’ll really look inside the Dropbox folder we created. Make sure it’s working by navigating back to the main Pelican folder and enter:

make html

Again, if everything works, it should just report “done”.

Since Pelican generates your site to /output, you can specify where you’d like to output it. A more appropriate place could be your Apache’s /var/www directory where you’ve set up a VirtualHost.

#####Step 7: Auto-updates#####

Pelican requires you to run make html whenever your site is updated. There’s plugins that can do this for you, plugins to watch your Dropbox folder. I prefer to keep things as vanilla as possible.

All my Pelican posts are first written as drafts by adding status: draft when writing. I’ll still save them to Dropbox, the server will still see them but Pelican won’t publish them until that’s removed. Because of this, I’ve set up a cron job to automatically update.

Make sure you’ve sudo su’d and then enter:

$ crontab -e

You may or may not have other cron jobs here. Go to a new line and enter:

0 * /2 * * * cd /path/to/pelican/folder && make html

Quit and save changes. First of all, change the path appropriately. Secondly, the above line will run that command every 2 hours. If you need to publish a post urgently, you can still SSH in and do it manually. This just saves me having to keep logging in. I don’t publish any posts that are time sensitive so 4 hours seems fine to me. If you prefer, you can set this to whatever you wish. A great cronjob generator tool can be found here.

And that’s running Pelican on CentOS with Dropbox integration. I’m certainly not the first person to do this but I found I was looking at various places for help along the way so thought it best to aggregate everything I learnt for others to take and improve upon.

You can find complete documentation on writing posts and themes for Pelican in their Documentation.