Turning my iPhone into a Moodle server
Having ended up with a spare iPhone from a recent upgrade I decided to try jail-breaking the old one and see what software was out there away from the restrictions of the app store. I discovered that lighttpd, php and sqlite were all available from the software repositories for download - these three combined are enough to run a Moodle server. So out the window went cleaning my flat and sensible tasks - I had to make my phone into a Moodle server!
Getting the software for moodle installed and configured was relatively painless, the ‘cydia’ software installer appears to use dpkg under the hood, so I installed openssh server and apt through the gui installer and then sshed onto the phone to do the work with a full size keyboard and the moodle server was up and running quite quickly. (More details for configuration below).
Sqlite is a really interesting technology which seems to be making its way into a lot of software and I was quite interested to see Moodle support for lightweight testing sorts of applications and it has made its way into Moodle thanks to the great work of Andrei Bautu in his GSOC project last year. It only exists in the highly unstable Moodle 2.0 development branch so I needed to install this on my iPhone. Development is moving incredibly fast in the Moodle 2.0 branch so I was not at all suprised to see that the sqlite driver was not working. It took a bit of time to find out what the major issue with the driver as it was a silent error. But I eventually found and fixed the major issue.
Sadly, despite successfully installing and passing most of the database unit tests on my development machine with sqlite, some database queries were continuing to cause the iPhone server issues. I spent some bit of time improving the sqlite driver to show more debugging information and get to the bottom of the issue.
After a lot of debugging and irritation, it seems that the sqlite library version (3.3.7) linked to from php has a bug/incompatibility which means it does not like queries like:
SELECT student.id FROM mdl_user student JOIN (SELECT ra.userid FROM mdl_role_assignments ra) roles ON student.id = roles.userid
It’ll report: SQLSTATE[HY000]: General error: 1 no such column: roles.userid
Where as it will work fine with something like:
SELECT student.id FROM mdl_user student JOIN (SELECT userid FROM mdl_role_assignments ra) roles ON student.id = roles.userid
(That was a simple example to try and find the problem - the SQL we have in moodle is a lot more complex that that).
To confuse matters, the sqlite command line tool I was using to test on the phone itself was a newer version (3.6.12), which works absolutely fine with both queries. The same was true on my development machine, which meant that I could install with sqlite succesfully everywhere but the iPhone itself. I assume the php version has been linked to the iPhone OS version - but I am too lazy to check/do something about it!
While I don’t yet have a working moodle server install running on the phone itself, the exercise in improving the sqlite driver has been really useful. I’ve updated the driver in CVS, on recent sqlite versions it is only currently failing 9 of 1298 tests. (CEIL and SUBSTR being the major issues - but they are only used for stats and the admin healthcheck) so its really looking like a really useful option for those situations where a full-grown database server is overkill. I’ve put a video of the (disapointing) install on youtube and you can find details of the various bits of configuration below.
Configuration Details
I love apt :)
apt-get install lighttpd php sqlite3 git
I’ve never configured lighttpd before, but a quick search for configuring with php and I made a very simple config with /etc/lighttpd/lighttpd.conf
and /etc/lighttpd/mod_fastcgi.conf
JB-Phone:~ root# cat /etc/lighttpd/lighttpd.conf
include "mod_fastcgi.conf"
server.document-root = "/htdocs/moodle"
server.port = 80
server.tag ="lighttpd"
server.errorlog = "/htdocs/log/error.log"
accesslog.filename = "/htdocs/log/access.log"
server.modules = (
"mod_access",
"mod_accesslog",
"mod_fastcgi",
"mod_rewrite",
"mod_auth",
"mod_fastcgi"
)
index-file.names = ( "index.html", "index.php" )
# cat /etc/lighttpd/mod_fastcgi.conf
fastcgi.server = ( ".php" =>
( "localhost" =>
(
"bin-path" => "/usr/bin/php-cgi",
"socket" => "/tmp/php.socket"
)
)
)
And started the webserver manually with:
lighttpd -f /etc/lighttpd/lighttpd.conf
I created the /htdocs/ directories - and git cloned moodle into /htdocs/moodle and created the moodle config file as mentioned in the sqlite moodle docs:
$CFG->prefix = 'mdl_'; // prefix to use for all table namesV
$CFG->dbtype = 'sqlite3';
$CFG->dblibrary = 'pdo';
$CFG->dbhost = 'localhost';// leave dbhost to localhost (or blank) to store the database file in Moodle data directory
Oh and I was also naughty and made the ‘zip’ php extension an optional item (as it wasn’t in the packed php version):
diff --git a/admin/environment.xml b/admin/environment.xml
index e15a33b..94ed8f2 100644
--- a/admin/environment.xml
+++ b/admin/environment.xml
@@ -262,7 +262,7 @@
<ON_ERROR message="ctyperequired" />
</FEEDBACK>
</PHP_EXTENSION>
- <PHP_EXTENSION name="zip" level="required">
+ <PHP_EXTENSION name="zip" level="optional">
<FEEDBACK>
<ON_ERROR message="ziprequired" />
</FEEDBACK>
Enjoy!