Wednesday September 5, 2007
Porting lighttpd to the iPhone.
After a full night of hacking, nearly bricking my iPhone once, and way too much caffeine, I have finally done it. I have a viable port of lighttpd for the iPhone. Even CGI using Perl, Python, and Ruby works, if installed!
Apache was already available via Installer.app, but just thinking about what a resource hog Apache can be, I decided I'd rather run lighttpd. Lighttpd, after running a few requests, uses 876 bytes of real memory and has a virtual footprint of 267KiB. That's damn good.
Getting it compiled was a bit of a pain, though, and took a long, long time. I followed the handy instructions on the iPhone Dev Wiki, downloaded and extracted the latest lighttpd source, and ran the following commands:
export CC=/usr/local/arm-apple-darwin/bin/gcc export CXX=/usr/local/arm-apple-darwin/bin/g++ export LDFLAGS=-Wl,-syslibroot,$HEAVENLY export DESTDIR=/Users/amcgregor/Public/iPhone/lighttpd-1.4.17/stage/ ./configure --host=arm-apple-darwin --prefix=/usr/local make make install
I copied the contents of the stage folder to the root of my iPhone, wrote up a simple configuration file, SSH'ed in, and gave it a whirl. Worked like a hot damn. With the finishing touches I wrote a launchd init-script and filled out the configuration file to offer CGI via Perl, Python, and Ruby.
Lighttpd is available using Installer.app thanks to the gracious hosting of Ste packaging (accessed through Community Sources). Please install from there.
The webroot is in /private/var/root/Sites/. The reason for this vs. /Library/WebServer/Documents as per Mac OS X Server and the iPhone Apache installation is because /Library is normally a read-only filesystem (when jailed) and has limited storage space. The home folder is stored in a separate partition which uses the vast majority of the 4 or 8 GB of the device.
In iPhone Firmware 1.1.3 using the root account is frowned upon. You can move the webroot using SSH, SFTP, AFPD, or SMBD from `/private/var/root/Sites` to `/private/var/mobile/Sites` if you wish. Just remember to change the ownership of the files (as root) after moving using something similar to the following:
chown -R ~mobile/Sites
Modify /usr/local/etc/lighttpd.conf and change the first line:
server.document-root = "/private/var/mobile/Sites/"
Enjoy!
— Alice.