mypod.me

iPhone hackery and networkery with chewy BSD goodness!

rainbow

A web server on an iPhone?

Yes, and there are some very good reasons why you’d want a webserver running on your iPhone. Those reasons come later. First comes the webserver.

To install lighttpd, a tiny and insanely full-featured HTTP server, either use the Cydia GUI, or use the command apt-get install lighttpd. Now a few more steps of manual configuration will complete the installation:

  • Create the directory /var/log/lighttpd to store the log files;
  • Create the directory /var/www as the document root for your web site;
  • Create a configuration file in /etc/lighttpd.conf for server configuration.

Mine looks like this:

server.document-root = "/var/www/"
server.port = 80 # Default
server.modules = ( "mod_cgi","mod_indexfile","mod_dirlisting", "mod_accesslog" )
server.tag = "iPhone lighttpd"
server.name = "Aiode.webearth.org"

server.pid-file = "/var/run/lighttpd.pid"
dir-listing.activate = "enable"
dir-listing.external-css = "/.dirlist/iphonedirlist.css"
accesslog.filename = "/var/log/lighttpd/access.log"
server.errorlog = "/var/log/lighttpd/error.log"

index-file.names = ( "index.php", "index.py", "index.pl", "index.cgi", "index.html", "default.html", "/.dirlist/dir-generator.py" )
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi", ".cgi", ".py" )

cgi.assign = ( ".pl" => "/usr/bin/perl", ".py" => "/usr/bin/python", ".rb" => "/usr/local/bin/ruby", ".cgi" => "", ".sh" => "", ".php" => "/opt/iphone/bin/php-cgi" )

mimetype.assign = (
".pdf" => "application/pdf",
".sig" => "application/pgp-signature",
".spl" => "application/futuresplash",
".class" => "application/octet-stream",
".ps" => "application/postscript",
".torrent" => "application/x-bittorrent",
".dvi" => "application/x-dvi",
".gz" => "application/x-gzip",
".pac" => "application/x-ns-proxy-autoconfig",
".swf" => "application/x-shockwave-flash",
".tar.gz" => "application/x-tgz",
".tgz" => "application/x-tgz",
".tar" => "application/x-tar",
".zip" => "application/zip",
".mp3" => "audio/mpeg",
".m3u" => "audio/x-mpegurl",
".wma" => "audio/x-ms-wma",
".wax" => "audio/x-ms-wax",
".ogg" => "application/ogg",
".wav" => "audio/x-wav",
".gif" => "image/gif",
".jpg" => "image/jpeg",
".jpeg" => "image/jpeg",
".png" => "image/png",
".xbm" => "image/x-xbitmap",
".xpm" => "image/x-xpixmap",
".xwd" => "image/x-xwindowdump",
".css" => "text/css",
".html" => "text/html",
".htm" => "text/html",
".js" => "text/javascript",
".asc" => "text/plain",
".c" => "text/plain",
".cpp" => "text/plain",
".log" => "text/plain",
".conf" => "text/plain",
".text" => "text/plain",
".txt" => "text/plain",
".dtd" => "text/xml",
".xml" => "text/xml",
".mpeg" => "video/mpeg",
".mpg" => "video/mpeg",
".mov" => "video/quicktime",
".qt" => "video/quicktime",
".avi" => "video/x-msvideo",
".asf" => "video/x-ms-asf",
".asx" => "video/x-ms-asf",
".wmv" => "video/x-ms-wmv",
".bz2" => "application/x-bzip",
".tbz" => "application/x-bzip-compressed-tar",
".tar.bz2" => "application/x-bzip-compressed-tar",
# default mime type
"" => "application/octet-stream",
)

In order to make sure that the web server launches every time the iPhone reboots, we need to add a launchd control file. This file, com.lighttpd.plist is placed into the /Library/LaunchDaemons directory, where the launchd process will start it and keep it running if it should crash or be killed.

And once I can figure out how to get tags into WordPress, well, then I’ll show you the contents of that file. Sigh. The launchd control file is an XML property list, which means that WordPress (and other web tools) attempt to parse it, rather than display it.

When iPhone is rebooted, lighttpd will be running as one the background processes.

2 Responses to “A web server on an iPhone?”

  1. August 3rd, 2008 at 7:20 pm

    Stephen Kelly says:

    Did you get it working as a background process ? I thought Apple didn’t let background processes run for third party apps ? or will they only run on jailbroken phones ?

  2. August 3rd, 2008 at 10:40 pm

    admin says:

    Yes, it’s running as a background process, that’s what launchd handles so excellently. And yes, Apple doesn’t want background processes (except their own, and those, sir, are legion). And yes, this will only run on jailbroken phones. But if you see my next post, you’ll see why that’s a hilariously !FAIL! idea…

Leave a Reply