Saturday, January 1, 2011

Simple cgi on the Chumby/Infocast with lighthttpd

There is a great blog posting, featured on Hack a Day, that describes how to run lighthttpd on the Chumby.  The blog also provides a pre-built version of the web server that is configured to run in /mnt/storage instead of /mnt/usb.  This is nice because it lets you run the httpd server from the writable partition of the internal microsd card instead of having to use a thumb drive.  A lot of the Chumby derived  devices have enough space in /mnt/storage to do all kinds of interesting things.

I recently purchased an BestBuy Insignia Infocast 8" device. It is essentially an 800Mhz big screen Chumby running linux 2.6 with some BestBuy customizations.  The Insignia team did not include a web server and the simplest way to fix that is to use the lighthttp server from the blog mentioned above.

Installing and Configuring

The basic steps for installation are:
  1. Turn on SSH in the Info cast. Touch the Insignia logo in the upper right corner.  This should make the About Device panel appear. Then touch the Pi symbol in the upper right corner. Enable sshd by touching the SSHD button. The daemon starts immediately. The machine's IP address appears in the panel.  You will need that to ssh into the box.
  2. SSH into the device from your PC.  Windows users can use putty which is freely available on the internet.  Linux users probably already have it installed.   The root user id has no ssh password so linux users will login with ssh root@<chumby_ip_address>
  3. You can enable sshd across reboots by creating /psp/start_sshd with the command touch /psp/start_sshd but remember that you the default sshd has no password.
  4. cd into /mnt/storage.  
  5. Download the pre-built version of light httpd.  Find the URL in the blog mentioned above and enter wget <the_.tar.gz file path> in the ssh window. I normally create a software or downloads directory to keep stuff organized.
  6. Unzip the package with gunzip <the_.tar.gz file name> and then untar the .tar file while in the /mnt/storage directory.
  7. You can delete the .tar file because it is no longer needed.
The next step is to configure and start the web server.  The blog author kindly created a startup script that works on the Chumby/Infocast.  The chumby folks kindly created a set of startup hooks that let you run additional scripts at startup without having to modify the Read/Only partition on the microsd card.  They search specific directories for scripts with specific names.  The most commonly used on is a debugchumby script in root directory of any inserted USB flash drive.  In this case we use one of the other two user hooks, /mnt/storage/psp/rfs1/userhook1.   
  1. The directory /mnt/storage/psp/rfs1 does not exist in the default Infocast installation so create that directory using mkdir.
  2. Copy /mnt/storage/lighty/startup.sh to /mnt/storage/psp/rfs1/userhook1.  Note that the userhook1 filename does not have any file extension.  
Web servers like lighthttp create error and access logs that grow over time filling up the micro sd card.  The other blog posting added log rotation and trim scripts to their installation but I didn't want to have to worry about it.  I turned off the access logs and put the error logs on the /tmp partition.
  1. Edit /mnt/storage/lighty/lighthttpd.conf
  2. Change the server.errorlog and accesslog.filename to fit your needs.  I changed mine to
# server.errorlog = "/mnt/storage/logs/lighthttpd.error.log"
# accesslog.filename = "/mnt/storage/logs/lighttpd.access.log"
server.errorlog = "/tmp/lighthttpd.error.log"
accesslog.filename = "/dev/null"
Start the web server and verify it's running
  1. Type /mnt/storage/psp/rfs1/userhook1 to start the server.  
  2. You should be able to verify  the process is running with the ps -ef command.
  3. You can then hit the web server with your browser http://<chumby_ip_address>  and see the welcome screen.
Some Simple cgi Programs

One of the nifty things about running a web server is that you can cause it to run programs on the device just by requesting the URL of a program tied to the web server.  Everything written to standard-out is returned to the web browser.

The lighty/lighttpd.conf file contains mappings to support Bourne Shell and Perl scripts. It assumes that any file ending in .sh is a shell script and any file ending in .pl is a Perl script.
cgi.assign = ( ".sh" => "/bin/sh", ".pl" => "/mnt/storage/usr/bin/perl", ".cgi" => "/psp/usr/bin/perl" )                        
alias.url = ( "/cgi-bin/" => "/mnt/storage/lighty/cgi-bin/", "/images/" => "/mnt/storage/images/" )
 The Infocast 8" comes with a couple shell scripts that were intended for the standard Chumby web server that are easily adapted to lighthttpd (with a simple rename). They have .sh on the end so we'll fix that when we copy them into the lighty cgi-bin directory.
  1. cp /www/cgi-bin/memstats /mnt/storage/lighty/cgi-bin/memstats.sh
  2. cp /www/cgi-bin/wifi /mnt/storage/lighty/cgi-bin/wifi.sh
The url for these files will be
  1. http://<ip_address>/cgi-bin/memstats.sh
  2. http://<ib_address>/cgi-bin/wifi.sh
Here is a simple shell script top.sh that displays cpu usage of currently running programs via the top command and returns the results to the web browser.

#!/bin/sh
echo "Content-type: text/html"
echo ""
echo "<html><head>"
echo "<title>Chumby Top Processes</title>"
echo "<meta http-equiv=\"Refresh\" content=\"5\";>"
echo "</head></html>"
echo "<body>"
echo "<h4>Top Processes (refreshes every 5 seconds) </h4>"
echo "<pre>"
top -n1
echo "</pre>"
echo "</body></html>"
I installed it in /mnt/storage/lighty/cgi-bin/top.sh and run it with http://<ip_address>/cgi-bin/top.sh

Final Thoughts

The referenced blog article describes how to build lighthttpd from scratch.  You might want to do a custom build if you need additional modules.

3 comments:

  1. I was have troubles with this, I'll try to do this later when I get home. Thanks.
    long island document scanning

    ReplyDelete
  2. Could you run .sh script without extension?
    The original chumby(classic) runs apache/httpd,
    http://wiki.chumby.com/index.php/Using_the_busybox_HTTP_server

    it has no .conf and can run shell script w/o extension, e.g.
    http://forum.chumby.com/viewtopic.php?id=1060

    but how to set the lighttpd.conf for this?

    ReplyDelete
  3. thanks for sharing this post with us. it is interested post . i have great interest in new things which are belongs to IT field. i installed this device and i am sure to read this post that this device become very useful for everone those are using this.....


    Architectural CGI

    ReplyDelete