Android

Running native CGI scripts (perl) with micro-httpd on Android

Introduction

This article describes the software needed so you can have your native web server running CGI scripts on Android. When I mean native, I mean not an .apk file (Java), but a cross compiled version of a C web server which is able to invoke Perl scripts (a native Perl interpreter is also required).

You need to be root as well as be able to remount the /system partition in read write mode. Busybox is a requirement.

I will not be responsible if anything bad happens if you follow this tutorial. Proceed at your own risk!

Due to the limitations of the low level Android API (and difficulty to cross-compile full blown applications – it is not Linux) not all things will work (lack of perl modules, no database, etc).

However, I was determined to run a blog on my phone (just because I felt it was possible). It is useless from many points of views (since mobile carriers block incoming connections to the phone). But fun!

One of the simplest Perl blogs out there is PPLOG. I’ll use it as a demo of how to setup a CGI webserver / application on Android.

Installation

Installation consists in several steps:

  1. micro-httpd
  2. Perl for Android
  3. CGI.pm module for Perl
  4. setting up the blog

micro-httpd

I have managed to cross compile micro-httpd by ACME Labs.

Grab it from here: mini_httpd_1.19.arm.tar.gz

Licenses of mini_httpd and crypt source code are bundled within the archive. The Android binary itself (as well as the patch to build) is free, license is found within the Cross Compiling section below.

After you copy it to your /sdcard/, execute the following commands to set it up. /system/usr/www will be the folder where the configuration file / log file as well as the doc root will be.

cd /system/xbin
tar xf /sdcard/mini_httpd_1.19.arm.tar.gz
# let's make the folders - and then the configuration file
cd /system/usr
mkdir www
mkdir www/htdocs
mkdir www/htdocs/pplog
vi mini_httpd.conf

For more configuration options, please check here.

The configuration file contents:

nochroot
user=root
port=80
dir=/system/usr/www/htdocs
data_dir=/system/usr/www/htdocs
cgipat=**.pl
logfile=/system/usr/www/mini_httpd.log

Now it’s time to start it up:

mini_httpd -C /system/usr/www/mini_httpd.conf

It will complain that it runs as root – but we can’t run it otherwise anyway.

Perl for Android

Perl for Android is an effort to support Perl scripting on Android, using SL4A. While it is targeted for UI applications (and the SL4A uses it to execute Perl code behind the scenes) and use Android’s services (location, etc) to develop UI applications in Android. However, it does offer a native Perl interpreter which can be invoked standalone from native code.

Grab it from here: http://code.google.com/p/android-scripting/downloads/detail?name=perl_for_android_r1.apk&can=2&q=

This is just the installer. After you install it, click the “Install” button to install the Perl interpreter (it will download a bunch (3) .zip files and unpack them for you).

The version at the moment of writing (r1) will install the Perl interpreter at

/data/data/com.googlecode.perlforandroid/files/perl/perl

This will be the shebang all .pl scripts will need to have so that they can be invoked by the server.

#!/data/data/com.googlecode.perlforandroid/files/perl/perl
use CGI;
$cgi = CGI->new;
...

CGI.pm module

This is an important module that is missing from Perl for Android interpreters. Fortunately, it is Perl only (no need to recompile it) and thus, it can be manually installed on Perl’s library folder.

Grab it from here: http://search.cpan.org/CPAN/authors/id/M/MA/MARKSTOS/CGI.pm-3.55.tar.gz

You need to copy it to your SD Card, and type the commands below:

busybox mount -o remount,rw /system
cd /data/local/tmp
tar xf /sdcard/CGI.pm-3.55.tar.gz
cd CGI.pm-3.55/lib
cp -r * /data/data/com.googlecode.perlforandroid/files/perl/5.10.0/
cd /data/local/tmp
rm -r CGI.pm-3.55

PPLOG

Installing PPLOG is simple. First you have to download it: http://code.google.com/p/pplog/downloads/list (Version 1.1b STABLE)

Steps:

a) Extract the contents of the .rar archive, and upload the content of upload this folder to the /system/usr/www/htdocs/pplog folder.

You now should have a structure like this one:

[root@arc]/system/usr/www# tree
.
|-- htdocs
|   `-- pplog
|       |-- bg.gif
|       |-- getPath.pl
|       |-- header.gif
|       |-- pplog.pl
|       |-- reddit.gif
|       |-- smilies
|       |   |-- Thumbs.db
|       |   |-- cool.gif
|       |   |-- cry.png
|       |   |-- doh.gif
|       |   |-- happy.gif
|       |   |-- n_n.png
|       |   `-- worried.gif
|       |-- style.css
|       `-- update to 1.0
|           `-- update.pl
|-- mini_httpd.conf
`-- mini_httpd.log

Two more things left to do: configure pplog and update the .pl scripts to add the shebang.

To configure, edit the /system/usr/www/htdocs/pplog/pplog.pl file like this (module CGI::Carp is missing, but since it is optional, we can safely comment out the line that requires it):

...
# use CGI::Carp qw/fatalsToBrowser/;      # This is optional
...
our $config_adminPass = 'yourpass';
our $config_smiliesFolder = '/system/usr/www/htdocs/pplog';
our $config_smiliesFolderName = 'smilies';
our $config_postsDatabaseFolder = '/system/usr/www/htdocs/pplog/posts';
our $config_commentsDatabaseFolder = '/system/usr/www/htdocs/pplog/comments';
...

To update the shebang edit the two files getPath.pl and pplog.pl and modify the first line:

#!/data/data/com.googlecode.perlforandroid/files/perl/perl

then make both files executables: since they will be executed as CGI and thus need to be executables. This is also the reason we keep the home folder on the /system folder and not on the SD Card.

chmod 755 *.pl

WE’RE DONE!

Now browse to http://[phone_wifi_ip]/pplog/pplog.pl and enjoy your blog!

Perl CGI on Android

Compiling mini_httpd 1.19 for Android

You need:
a) micro-httpd source code from here: http://www.acme.com/software/mini_httpd/mini_httpd-1.19.tar.gz
b) you need the libcrypt headers / sources (*.h and *.c) from here. Just copy them in the mini_httpd folder.
c) the patch: mini_httpd-1.19.arm.patch

The patch is licensed under BSD License:

Copyright © 2011 by Iulian Virtejanu . All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.

You will have to modify the patched Makefile to point to your toolchain / setup and then rebuild mini_httpd with make.

Observations

PHP it is not currently possible. This is because of two reasons:
a) PHP for Android guys do not want to offer a CGI version since SL4A is OK with CLI version.
b) Some PHP software does detect if CGI / CLI and behaves acordingly (writing more headers which are added by default by CGI, but not by CLI), but their latest compilation outputs some “FIXME: ” warnings which are read by the web server – but they are not valid HTML content.

There is another php-cgi alternative that I know about, but apparently it is not stable, so I decided not to try it. It might worth check it out though.

4 Comments

  1. What are the options for accessing the Android hardware from PERL?

    Can things like the GPS, Accelerometer, Compass be accessed this way?

  2. ThierryB

    Do you think there is a way to put PPLOG data on /sdcard/?

  3. Abhishek Jain

    Thank you so much 🙂
    will give it a try

  4. antonio

    Hi,

    great; You may try perl-android-apk (github), it works, too.

    I’m using pplog on a Thinclient (puppylinux), and I’m planning to move the server to a npbc 724 (android 2.2).

    thx

Leave a Reply