The Raspberry Pi might not be a heavyweight in the specifications department but that is no reason why this inexpensive educational computer shouldn't help you learn more about some of the latest technology used to create web sites. The availability of some of the latest open source software in Arch Linux ARM introduces the exiting possibility of using the device as a mini portable web server (you could even battery power it). This could be very useful, not just for learning about these new technologies but also if you wanted to try your sites out with client machines that may not let you install server software locally, e.g. phones, tablets and set top boxes.
There has been a lot of attention focused on people learning to code this year and one language that has been a popular choice to learn has been Javascript. Traditionally this language has been used client side in a browser to enhance the experience of using web sites but now it is possible to use this language to create the web sites themselves through a technology called Node.js. There has been a fair bit of controversy around this Node (although that is probably the case with every technology). Putting those debates aside for a moment, after using Node for a project at work I think it is useful in helping to understand how web development works. The differences in its approach can aid reflection when learning any web technology I think.
Getting Node.js up and running on a Raspberry Pi is quite straightforward under Arch Linux ARM. Installation is a simple as:
sudo pacman -S nodejs
Once you get it up and running you can write your scripts and run them as normal with node. The example script below prints out some properties of the platform. See the node.js documentation for details on how this works.
var os = require('os');
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write("Node.js version: " + process.version + "\n");
res.write("Hostname: " + os.hostname() + "\n");
res.write("OS Type: " + os.type() + "\n");
res.write("OS Platform: " + os.platform() + "\n");
res.write("OS Architecture: " + os.arch() + "\n");
res.write("OS Release: " + os.release() + "\n");
res.write("OS Freemem: " + os.totalmem() + "\n");
res.end('Hello World\n');
}).listen(1337, '0.0.0.0');
console.log('Server running on port 1337');
You can run it with this command:
node example.js
You should then be able to connect a client to it and see debug output on the terminal. Using an IP address of 0.0.0.0 means that Node will listen on all ports of the Raspberry Pi, meaning that other devices on your network can connect to your web site. You should only do this on private networks!
If you go to your Raspberry Pi's web address with this script on port 1337 in a browser you should see something like this:
Node.js version: v0.6.19
Hostname: alarmpi
OS Type: Linux
OS Platform: linux
OS Architecture: arm
OS Release: 3.1.9-20-ARCH+
OS Freemem: 126562304
Hello World
Not very exciting, but it gives you the idea.
Node.js has a package manager of its own called npm which lets you install various libraries to extend the functionality available to the developer. This package manager has a search function, but I found that I got an out-of-memory error when I first tried to build the index. To get round this I made a swap partition (a place that can function as temporary memory on the SD card) and enabled it only when the search index was building. After that npm seemed to work just fine.
It is not just web server technology that is changing but also approaches to storing data. At one time we would save data in flat tables and interrogate it with SQL. Today new non-relational, document orientated databases are becoming more popular. One of these is Apache CouchDB (shown below), the latest version of which is available from the Arch Linux ARM repositories. I installed this and ran all of the tests in the supplied test suite. All passed! So the Raspberry Pi could be a handy platform for picking up new database skills. I write a tutorial on how to start with CouchDB some time ago, although I have not tested it to see if it works on the Pi. If you try it let me know in the comments.
CouchDB also has a plugin called CouchApp. This enables you to host entire web applications in the database itself and serve them to clients without the need for a separate web server. I installed it with the help of the python2-pip package with these commands:
sudo pip2 install couchdb
sudo pip2 install simplejson
sudo pip2 install couchapp
Again web applications are written using Javascript and HTML5 with the advantage of CouchDB to process the data. To give this a proper test I installed an example CouchApp application called Sofa which is a simple blogging application. Everything seemed to work on my little Raspberry Pi CouchDB server and I was able to log in (once I made an admin account) and create a page.
Whenever I mention CouchDB for some reason some people instantly bring up MongoDB! This is another document-orientated non-relational database that has proved popular. Here the story is not so simple, apparently Mongo is orientated towards X86 type computers (most desktops and laptops fall into this category). However the Raspberry Pi of course has an ARM processor so the Mongo source code will not work as is on this machine. An unofficial patched version of Mongo 1.8 source code for non-X86 machines exists and it seems that this wil run on the Raspberry Pi. A blog post about this on Debian based Raspberry Pis can be found at http://elsmorian.com/post/24395639198/building-mongodb-on-raspberry-pi. I haven't tried this yet, but it might make a good project for a rainy day.
The solutions above might feel like radical ways to produce web sites, but it is possible to use more familiar favourites too. PHP powers a huge number of websites and the latest version, 5.4, is available in the Arch Linux ARM repositories. Usefully for a machine with limited resource, the new version of PHP also features for the fist time a built in web server that can be used during development. This is really good as it means if someone just wants to learn PHP they no longer have to worry about setting up Apache and getting all of the configuration right. Instead they can just go into the directory containing their code and run the PHP web server from there. This seems to work fine on the Raspberry Pi (see the image below) and could be another educational use case. Add on features for PHP are also available in the repositories too. You can read more about PHP 5.4 at: http://php.net/releases/5_4_0.php.
It is great to see that the Raspberry Pi can be used to learn about some of the latest web technology. It is certainly a feature that will add to its educational credentials. At the moment though it looks like you may be better off using a web browser on another machine to see the output of these programs running on the Pi thanks to a lack of memory and the current lack of a hardware accelerated X Server. It is possible now but rather slow. In my next blog post I will be looking at the use of a Raspberry Pi as a set top box. Its mission will not be to educate this time but entertain.