Pages

Monday, 27 January 2014

在一个造价25美元的raspberry pi上安装你的blog

everybody knows it's way smarter to just pay someone to host your website. But what not everybody knows is that it's way more punk rock to Do it Yourself. So what follows are some tips / pointers / instructions for setting  up your own home webserver (which will burn a scant 2 watts) using all free, open source software, a Raspberry Pi, and your home internet connection.

The emphasis here is on lightweight, which fits well with the Raspberry Pi. Sure, you can setup a blog with wordpress or Django, and they will run (I've tried it, at least with Django). But they probably will run rather slowly. Why? The rPi doesn't have a lot of memory or processing power, and a database / front end model requires a decent amount of that. If your site / blog ever gets much traffic, it'll likely buckle under the load. The answer? Just serve up plain old static HTML pages. It's fast, secure, simple, and easy on the rPi's limited resources. But rather than painstakingly handcodeing each new blog entry, you can use a static html generator like Pelican to make it easy.

Here are the hardware ingredients:
1) A Raspberry Pi (with power supply and SD card)
2) A computer with an SD card reader (or a USB stick SD card reader)
3) A simple home network, shared by your computer and the rPI. If you have a router and an internet connection, you're golden. The rPi will need to be connected via ethernet, so you might need a cable.

Here are the main software ingredients:
1) The standard Raspberry Pi Linux "Raspbian" operating system, based on Debian.
2) Lighttpd, a lightweight web server, that's pretty easy to setup.
3) Pelican - a static html blog generator (you can also use something like Octopad, if you prefer.)
4) PageKite - a "localhost tunneling solution," which I found to be much better than dynamic DNS.

Human requirements:
1) Some linux know how, or
2) A friend with some linux know how, and / or
3) Know how to use forums to ask for help, and
4) Patience

This instructable won't be comprehensive, but it will point you to the resources / places to get help if you need it.


Step 1: Setup your raspberryPi

First you need to setup the SD card on your rPi. On this little computer, the SD card is your hard drive. It needs an operating system so you can boot and run your rPI, and tell it what software to run to serve your site.

Check out the getting started guide here:
http://www.raspberrypi.org/quick-start-guide

You'll be installing the latest "Raspbian" SD card image from here:
http://www.raspberrypi.org/downloads

Follow the instructions to download the image and install it on your SD card.

Now, if you have a monitor, keyboard, and mouse that you can use to run your rPi, you're set. If you don't, it's ok - you can still set things up from the command line in what's called "headless" mode. To do that, you'll need to "SSH" into your rPi through the network connection shared by it and your computer. Here's a great tutorial that explain how to do that:
http://www.penguintutor.com/linux/raspberrypi-headless

Do run the Pi setup script when prompted.

You'll want to change the default password! If you leave the username as 'pi' and the password as 'raspberry,' you will be pwned. Change it now to something strong - web servers are exposed to the big bad internets, so you'll want to be security conscious.

Step 2: Setup a static IP, then install a webserver

Now you'll want your rPi to have a static IP address on your local network. Most networks use DHCP by default. That's a system where the router gives out an available IP address when asked by each computer. It's easier to setup your rPi to ask for the same IP every time. Here's another good tutorial from penguin tutor on how to set that up:

http://www.penguintutor.com/blog/viewblog.php?blog=6306

Once you have a static IP, install the webserver!

It's very easy to install software on Debian (and Raspbian is just Debian optimized for the pi), because pretty much all the software you need is available in the Debian software repositories.  So just open a terminal and type:

sudo apt-get install lighttpd

Lighttpd (pronounced "lighty") is a lightweight open source web server. Its job is to listen for requests from the internet on the http port - port 80. When it receives a request, it sends back the requested webpage data.

Once lighttpd is installed, you should be able to see a startup web page by typing your rPi's IP address into your browser's address bar. For example, if you chose 192.168.1.20, you just type that into your address bar, and lighttpd's default webpage will come up. (If it doesn't at first, try rebooting your rPi: sudo reboot )

Step 3: Pelican, a static html blog generator

Pelican is an awesome static html makin' blog engine. What does that mean?
It means that once you set it up, you write blog posts in your favorite text editor, place them in a special folder, and then run Pelican. It then converts your text files into a simple HTML website on your computer. You then copy those files to your rPi server, and your viewers will be able to navigate through your posts. You can even choose different themes.

Pelican setup is described here:
http://pelican.readthedocs.org/en/3.0/getting_started.html

You'll need to first get python  - the programming language in which Pelican is written and on which it runs - installed on your computer. The good news is that there are good instructions on how to do that on the webs. It might already be on there!
http://wiki.python.org/moin/BeginnersGuide/Download

The simple theme that comes with Pelican is pretty good, but there are a few others to choose from (see links on the site). You can also roll your own if you have some html / css chops.

Once you've generated your blog, you'll want to copy it to your rPI's /var/www directory - that's the directory where Lighttpd looks for html pages to serve when it gets http requests. Pelican has lots of handy utilities to make that easy, including synching through drop box, or copying the files via ssh.  

Step 4: Setup PageKite

When I first setup my rPi server, I tried using Dynamic Dns (through NO-IP.com) to make it work. It didn't. The reason was that my ISP, RCN cable, blocks all incoming requests on the http port - port 80. I thought that since No-IP has a service that will redirect those requests to another port of my choice (I used 8080), that'd I'd be ok.

But this had the very bad effect of making the non-standard port # I chose appear in my URLs. So when visiting my site, instead of seeing:

http://amosamos.net

in the address bar, the viewer would see:

http://amosamos.net:8080

That's enough to freak out most parents, grandmothers, and folks who are liable to get freaked out by the webs and viruses and hackery. I could avoid this by paying my ISP $20 more a month for a static IP, but that seems like a lot to hose a simple blog. I like them, for the most part, but not that much.

Luckily, a friend told me about PageKite. PageKite allows you to install their app - written in Python - on your webserver. It keeps in contact with PageKite's front end servers. So when a request to view your website comes in, it gets forwarded from the PageKite servers, through a "tunnel" to your rPi's localhost port 80. So there's no need to use Dynamic DNS to keep track of what IP address your ISP has assigned you today, or hop around the blocks they may or may not have installed.

http://pagekite.net/

PageKite is pay what you want, and the suggested rates are very low (~$3 / month). 

Step 5: W00t! Homegrown webs...

Now send your url to all your pals, and fill the internets with your homegrown, homeserved wisdom. :)

Next steps?

* Setup your rPi to use autoupdate. That means that it will install security patches / updates automagically, to ensure that any security holes get plugged up quick. Here's a great tutorial:
http://www.howtoforge.com/how-to-configure-automatic-updates-on-debian-squeeze

* Make your rPi run on solar power, so you can serve solar powered content round the world. (You only need around 2 watts!) Then make an instructable.

from http://www.instructables.com/id/Host-your-own-blog-from-a-25-Raspberry-Pi-compute/?ALLSTEPS