Thursday, April 21, 2016

Raspberry Pi as Classroom Server and Digital Pet

With the new Raspberry Pi 3 just out and presumably much faster than the original, I wanted to try again how well it would hold up as a really small scale server for hosting web applications.

Over the last few years, Raspberry Pi had mostly made its mark in the maker community as a small low-power embedded computer.

Even the official Raspbian image is now available in a "lite" version, optimized for headless servers. It now has out of the box IPv6 as well as the avahi daemon enabled, making the changes described in this earlier tutorial no longer necessary. Installing a Raspberry Pi server is now easier than ever: copy image to a flash card, plug into any network (including direct tethering) and connect to it under its default name : ssh pi@raspberrypi.local. The hostname can easily be changed when running sudo raspi-config along with other settings (extend filesystem, change GPU memory allocation etc.).

Back to the original education use-case, a Raspberry Pi is quiet and low-power enough that it could be used as an always on micro-server in the classroom as a sharing platform and to give students a taste of what it means to run "mission critical" IT infrastructure themselves.

The oldest, most basic, most lightweight but also most flexible platform for managing and collaboratively authoring web based documents is a wiki. Wikis impose almost no restrictions on the structure, except consisting of a series of inter-linked pages. Pages or sections can be edited easily in place using a simplified markup syntax for formatting and structure. Wikipedia, the most famous of wikis, is still one of the most popular sites on the Internet today and demonstrates the power and flexibility of the wiki model.

Among the man wiki engines, one that is frequently recommended as being particularly lightweight and suitable for Raspberry Pi is DokuWiki. It uses plain-text files for its storage backend, not needing an additional database, it is simple to install and configure and the UI looks reasonably up to date.

In order to host the DokuWiki engine written in PHP, we need a lightweight web server with fast PHP execution. For another tutorial, we have used lighttpd as a small footprint web server, this time we are trying out nginx another highly resource efficient web server that is somewhat more widely adopted.

First to install the missing packages for nginx with fast PHP support:

sudo apt-get install nginx php5-fpm php5-cli php5-mcrypt php5-gd

The download the latest version of the DokuWiki distribution and install it in a "wiki" subdirectory under the default document root tree of the server:

$ wget http://download.dokuwiki.org/src/dokuwiki/dokuwiki-stable.tgz -O dokuwiki.tgz
$ tar xzf dokuwiki.tgz
$ sudo mv dokuwiki-2015-08-10a /var/www/wiki
$ sudo chown -R www-data:www-data /var/www/wiki

Then we edit the default host config of the nginx web server

sudo vi /etc/nginx/sites-available/default 

and insert the following configuration:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.php docu.php;

    server_name _;


    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }


    #Remember to comment the below out when you're installing, and uncomment it when done.
    #location ~ ^/wiki/(data/|conf/|bin/|inc/|install.php) { deny all; }


    location ~ \.php$ {
        try_files $uri $uri/ /doku.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param REDIRECT_STATUS 200;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
     }
}

and after each configuration change, restart the web server:

sudo /etc/init.d/nginx restart

To initialize the wiki, point a browser at http://raspberrypi.local/wiki/install.php and answer the settings questions on the web form.

For the most simplicity we could choose full public mode for reading and editing, but seeing who has made which changes in the revision history might also be useful for a classroom setting and encourage some level responsibility and accountability on behalf of the students. In this case, we choose public for read and authenticated users for editing as well as the checkbox to allow users to register themselves.

After finishing setup, we use the newly created admin user to log in, and choose the Admin function from the top right and choose "Configuration Manager" from the list to uncheck the box "Autogenerate passwords" under authentication and save. Since we do not have email service set up, this requires the user to enter a password at registration time rather than trying to send an auto-generated one by email. Then we can comment out the corresponding line in the web-server config above to block access to the wiki-engine internals.

On the Raspberry Pi 3 the wiki is surprisingly snappy and responsive and indeed a long way from the slow response times of the earlier version of the hardware. It seems that Raspberry Pi has now reached a level of performance that is quite usable in a small scale setting.

Depending on the expected volume of content created on the wiki (especially uploaded attachment and media files), the flash-card should be sufficiently large or an external USB disk should be mounted - however an external USB drive might increase noise and power consumption.

Students could use the wiki to create their own homepages, collaborate on projects or share media projects they have created. Being able to experiment with a permissive and open platform in a protected setting (i.e. not on the Internet) can help to teach the basis of respectful interaction in a virtual environment.