13Jan2009

Installing Perl and the Lighttpd webserver on an iPhone

This guide assumes that you have jailbroken your iPhone with firmware 2.2 and have Cydia and MobileTerminal installed.

Installing Lighttpd and Perl

Open Cydia, go to the search tab and search for ‘lighttpd’. Click on the resulting listing and install Lighttpd.

To install Perl you need to add the CoreDev repository to Cydia. Open MobileTerminal and type the following:

(¬ denotes a wrapped line.)

iPod:~ mobile$ wget http://coredev.nl/cydia/coredev.pub
iPod:~ mobile$ apt-key add coredev.pub
iPod:~ mobile$ echo 'deb http://coredev.nl/cydia iphone main' > /etc¬
/apt/sources.list.d/coredev.nl.list

Now when you run Cydia you should see the CoreDev repository in the sources list, look for an entry called something like Perl 5.x.x-x and install it.

Configuring Lighttpd and Perl

Lighttpd will install without a config file or a place to put your website HTML files so you now need to create these. Open MobileTerminal, you should find yourself at /var/mobile. If not, go there by typing the following and creating a folder called Sites:

iPod:~ mobile$ cd /var/mobile
iPod:~ mobile$ mkdir Sites

Next you need to create the Lighttpd config file. Navigate to the /usr/etc folder and create the file lighttpd.conf:

iPod:~ mobile$ cd /usr/etc
iPod:~ mobile$ nano lighttpd.conf

This will open the nano text editor. Type the following into your file:

server.document-root = "/var/mobile/Sites/"
server.port = 80
mimetype.assign = (
    ".html" => "text/html",
    ".txt" => "text/plain",
    ".jpg" => "image/jpeg",
    ".gif" => "image/gif"
)
server.modules += ( "mod_cgi" )
cgi.assign = (
    ".pl" => "/usr/bin/perl"
)
index-file.names = (
    "index.html",
    "index.pl"
)

This is the minimum you need in your config file to run Lighttpd. This config sets the location of the folder that houses your HTML files that you created earlier, sets the port, assigns a few basic file types and tells Lighttpd where to find Perl.

Now save the file by swiping diagonally down and to the right onscreen until the cursor goes red, then press the letter O. To quit nano swipe diagonally down and to the right again and press X.

You can check the syntax of the config file by typing the following:

iPod:~ mobile$ lighttpd -t -f lighttpd.conf

Testing the webserver

In order to test the webserver you need to create a test HTML file and save it to your /var/mobile/Sites folder as index.html.

Get the IP address of your iPhone in the network settings panel and make a note of it. Go back into MobileTerminal on your iPhone and type the following to temporarily activate Lighttpd:

iPod:~ mobile$ lighttpd -D -f lighttpd.conf

Now try and load the iPhone’s IP address in a browser on your computer, you should see your test index.html file.

Starting up Lighttpd automatically

Open MobileTerminal, navigate to /usr/bin and create a shell script containing the previous start up command:

iPod:~ mobile$ cd /usr/bin
iPod:~ mobile$ nano lighttpd-startup

Then inside nano:

#! /bin/sh
lighttpd -D -f /usr/bin/lighttpd.conf

Save the file then and quit nano. This file needs to have its permissions altered to enable it to run:

iPod:~ mobile$ chmod 755 lighttpd-startup

Now for the code to make the lighttpd-startup script run on start up:

iPod:~ mobile$ cd /System/Library/LaunchDaemons
iPod:~ mobile$ nano com.lighttpd.startup

Again, inside nano type the following:

(¬ denotes a wrapped line.)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" ¬
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.lighttpd.startup</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/lighttpd-startup</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>

Save the file and close nano. Now the next time you restart your iPhone Lighttpd should automatically start up.

Filed under: Tech, Web Design / Dev — Tags: , , , — Di @ 10:00 pm