Wednesday, January 28, 2009

iPhone PDF/CHM/DOC/XLS viewer with Safari and Lighttpd


Hi guys,
tonight we finally have a great geeky post!

Once upon a time on my iPhone I had a wonderful package found via Installer that provided a cool directory listing for lighttpd on the iPhone in order to be able to browse some files and use Safari native capabilities to open them.

Unfortunately this package is no more in Cydia and Installer repositories (or I hadn't found it).
So tonight surfed the internet looking for it and I found this page with some information about it and (most important) the php script package.
After some experiments mixing the info with this other tutorial I found the way to get it working on my 1G iPhone with Firmware version 2.1

Here you are the installation in ten steps.


1. Get all the files you need


Download this .zip file containing the php script for cool directory listing on lighttpd


2. Install the packages you need


Install via Cydia these packages:
  • lighttpd
  • PHP


3. Connect to your iPhone via ssh (as root)


$ ssh root@[iphone IP address]

obtaining

[iphone host name]:~ root#

in my case

$ ssh root@192.168.1.xxx
Bilbo:~ root#



4. Create log folder for lighttpd and chown it properly


Bilbo:~ root# mkdir /var/log/lighttpd
Bilbo:~ root# chown nobody:nobody /var/log/lighttpd



5. Create a startup .plist file for lighttpd


Bilbo:~ root# nano /Library/LaunchDaemons/com.http.lighttpd.plist



You can simply use a file manager that supports ssh like Dolphin and Konqueror in KDE4 or simply Konqueror in KDE3. To use the graphical file manager with ssh you should use the fish:// protocol writing this in the address bar:

fish://root@[iphone-IP-address]

for me

fish://root@192.168.1.xxx



6. Create lighttpd config file


Bilbo:~ root# nano /usr/etc/lighttpd.conf


Copy this content in it



As you could notice this file differs from the one contained in the script package.
The first difference is that I chose to use as my data folder /private/var/mobile/Sites instead of /private/var/root/Sites

The other more important difference is the location of the php-cgi binary file, that is placed in /usr/bin in my installation insted of the old /opt/iPhone/bin

After having set the lighttpd.conf file you can check its syntax with this command from the terminal:

Bilbo:~ root# lighttpd -t -f /usr/etc/lighttpd.conf



7. Test your lighttpd and PHP config


Now you can create a file named test.php in /private/var/mobile/Sites to test lighttpd and PHP configuration:

Bilbo:~ root# nano /private/var/mobile/Sites/test.php

Copy this code in it to test PHP support:


Start the webserver manually with this command:

Bilbo:~ root# lighttpd -D -f /usr/etc/lighttpd.conf

Now you can open Safari and point to http://localhost/test.php to view the file.
If you can see the notice in green everything was good.
After the test you can simply stop lighttpd with a CTRL-C stroke in the terminal.


8. Copy the directory listing script folder and rename it


I made this step with Dolphin as I described before, checking later the permissions and the folder owner and group with the terminal. If you can't do the copy with a filemanager you can use scp or sftp from the command line.

After the copy or the dirlist folder into /private/var/mobile/Sites rename it like this

Bilbo:~ root# mv dirlist .dirlist



9. Copy your files on the iPhone


Now it's time to put your favourite file in /private/var/mobile/Sites to take advantage of the powerful capabilites of Safari to open natively those file thank to the proper bindings to the iPhone OS applications and libraries.


10. Enjoy


Now you have your web server ready to go. You have your files in the right place... the last thing to to is to start the system to begin to play!

Bilbo:~ root# launchctl load -w /Library/LaunchDaemons/com.http.lighttpd.plist

This should start the server properly as a daemon.
If you experience problem at this step don't worry you can use also there commands to stop everything before another attempt:

Bilbo:~ root# launchctl unload -w /Library/LaunchDaemons/com.http.lighttpd.plist

and

Bilbo:~ root# killall lighttpd


However the best thing to do now is to reboot to let the server start at boot itself (that is definitely the "right" way to start it).

Once you will have your server up and running you can easily point Safari to http://localhost to view your folder and to be able to open your files



I really hope my effort in making this guide will be useful not only for me as a reference but to some of you too!

Let's see...

As for now, that's all folks!

Keep on hackin'

Andy

4 commenti:

  1. leberkäs12:28 PM

    Thanks for this useful guide!

    ReplyDelete
  2. Awesome. Thanks alot for putting this together.

    ReplyDelete
  3. Thanks a lot!
    It worked just fine on my iPhone 2G, firmware 3.0.
    I used DiskAid to work with the file copying.
    Best Regards!

    ReplyDelete
  4. Anonymous7:00 PM

    can I install the mysql server?

    ReplyDelete