Hosting a Blog on Raspberry Pi

As of this writing, my blog is hosted entirely on a cute little Raspberry Pi downstairs in my family room. There are a million ways to publish a blog; I’ll tell you how I did this one.

Please note that this is not the easiest or the most reliable (or even cheapest) way to publish a blog. For that, just sign up at https://wordpress.com or https://www.blogger.com. Putting a blog on your Raspberry Pi is for you if you like DIY projects, you like making things work, and you don’t mind your blog going offline when your cat pulls out the power cord.

I started with a Raspberry Pi running the stock OS, which is a Debian Linux variant called Raspbian. My Pi is headless, which means I don’t have it hooked up to a monitor, or even a keyboard. It’s got a power cable and a network cable, and that’s it. Right out of the box, you can ssh to your little computer from another computer as long as you know its IP address. I use my home router admin console to find out the addresses of all the devices on my network. Once you have the address, you’re ready to connect (this also assumes you have another computer on the network with a terminal program and an ssh program).

# Your IP address is likely to be different from this.
ssh pi@192.168.1.101

You absolutely must change the default password.

passwd

Now you’re ready to get some blog software. I like to create a statically-generated blog. This means that your pages are all created in advance, and are not assembled by a programming framework every time a request is made. This makes the site faster, handle more traffic, and keeps you from having to mess around with programming or setting up a database to get your blog.

The first thing I installed was a web server. I chose lighttpd because it’s easy to get started.

sudo apt-get -y install lighttpd

And just like that, you’ll be serving web pages from your Pi. You can visit it in a browser at http://<your-ip-address>/ Lighttpd starts out with just a starter page, which we’ll be replacing soon. All the pages for your blog will be stored in /var/www.

Next, we need some software that can generate blog pages for you. I tried Hyde but didn’t end up loving it. I’m currently rocking Pelican, which is really cool so far. We’ll install Pelican with a package manager called pip. We need to get pip first:

wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py
sudo python get-pip.py

Now, Pelican:

sudo pip install pelican markdown

Now we’ll use Pelican to make a blog. First we create a directory (I’ll call it super-blog), then use Pelican to make the sources for our blog.

mkdir super-blog
cd super-blog
pelican-quickstart

It’ll ask you a few questions like what your blog is called, and what your web domain is (if you have one; I have a bit more to say about getting your own domain below). Now create your first post. You’ll put your pages in the content directory inside your blog directory.

nano content/hello-world.md

Nano is a simple text editor that’s comes with the Pi, but you might choose another editor. That’s a topic for another post! I actually write my posts on my laptop and copy them over to the pi for publishing, but that’s an extra complication you don’t have to deal with.

Write your pages using Markdown syntax, and make sure to save before you exit. Now, we generate the blog:

sudo pelican -o /var/www content

With the -o argument, we’re telling Pelican where the finished HTML, CSS, and supporting files should be written. /var/www is where lighttpd serves our content from.

Now if you refresh your browser, you’ll see the blog awesomeness! The Pelican docs are very good, so be sure to check those out.

To get your blog out to the world, you need to use a fairly advanced feature of your home router called port forwarding. The way it works is that your home network has many internal addresses (each device on your network has its own address) but only one address exposed to the global Internet. By default, all the devices on your network can see out to the Internet, but the Internet cannot see in (a bit like a two-way mirror). This is a good thing, because it protects your network from attacks and other mischief. But you want your blog to be visible to the world, so you need the Raspberry Pi exposed to the world. Specifically, you need to have your router forward all requests for port 80 (the port reserved for web traffic) to the IP address of your Pi. The details of how to do this will depend on the make and model of router you have, so use your router’s documentation to get the steps to follow. Once it’s working, you’ll be able to view your blog using your external (to the world) IP address. If you don’t know this address, go to http://www.whatismyip.com and it’ll tell you.

You’re probably also going to want a domain name. While you can use an IP address directly in a web browser, people almost never do that, because IP addresses are not very readable or memorable. Registering your own domain name costs money, but you can get a subdomain for free. A subdomain is when you piggyback off of someone else’s domain name. For example, I don’t own the sly.io domain, but its owner allows people to sign up for subdomains of it, so I chose the subdomain continuou.sly.io. You can sign up for one for free at http://freedns.afraid.org. You choose a domain, create a subdomain off of it, and then tell them what your (external) IP address is, and from then on you can use the domain in your browser (or give someone a link) instead of using the IP address.

Have fun blogging!

links

social