Difference between revisions of "Lighttpd"

From WebOS Internals
Jump to navigation Jump to search
 
(9 intermediate revisions by 5 users not shown)
Line 2: Line 2:
  
 
== Preliminaries==
 
== Preliminaries==
# Gain [[Portal:Accessing_Linux | root access]].
+
# Gain [[Portal:Accessing_Linux | access to Linux]] on your device.
 
# Setup the [[Next_steps | Optware Feed]].
 
# Setup the [[Next_steps | Optware Feed]].
 
# Open the root file system to read/write with rootfs_open.
 
# Open the root file system to read/write with rootfs_open.
 +
Update: Replace this older method by installing Lighty (lighttpd) from the Optware section of Preware.
  
 
== Install Lighttpd with PHP support ==
 
== Install Lighttpd with PHP support ==
Line 16: Line 17:
  
 
<pre><nowiki>                                "mod_fastcgi", </nowiki></pre>
 
<pre><nowiki>                                "mod_fastcgi", </nowiki></pre>
 +
 +
For Python on CGI, see section below.
  
 
You may also want to adjust the port that the web server runs on.  Sprint blocks port 80 and this package installs Lighttpd to run on port 8081 :
 
You may also want to adjust the port that the web server runs on.  Sprint blocks port 80 and this package installs Lighttpd to run on port 8081 :
Line 27: Line 30:
  
 
<pre><nowiki>/opt/etc/init.d/S80lighttpd start</nowiki></pre>
 
<pre><nowiki>/opt/etc/init.d/S80lighttpd start</nowiki></pre>
 +
 +
Update: The current methods of starting and stopping Lighty (lighttpd) are:
 +
<pre><nowiki>start mobi.optware.lighttpd
 +
stop mobi.optware.lighttpd</nowiki></pre>
 +
 +
It may be convenient to use a simple shell script for starting, stopping and restarting:
 +
<pre><nowiki>#!/bin/sh
 +
# lighty.sh
 +
if [ "$1" = "start" ]
 +
then    start mobi.optware.lighttpd; exit
 +
elif [ "$1" = "stop" ]
 +
then    stop mobi.optware.lighttpd; exit
 +
elif [ "$1" = "restart" ]
 +
then    stop mobi.optware.lighttpd
 +
        start mobi.optware.lighttpd
 +
else    echo "Instruction is missing or incorrect."
 +
        echo "Usage: lighty start|stop"; exit
 +
fi</nowiki></pre>
  
 
== Add a startup script ==
 
== Add a startup script ==
Line 58: Line 79:
  
 
<pre><nowiki>/opt/share/www/</nowiki></pre>
 
<pre><nowiki>/opt/share/www/</nowiki></pre>
 +
 +
Update: The default home directory defined in the conf file is now
 +
<pre><nowiki>/media/internal/www/</nowiki></pre>
 +
However, it useful to create subdirectories cgi-bin (for CGI scripts) and htdocs (for HTML documents). (See the Python section below for the relevant conf file tweaks.)
  
 
Sample gps.php file that will load your current location via Google maps:
 
Sample gps.php file that will load your current location via Google maps:
Line 63: Line 88:
 
<pre><nowiki>
 
<pre><nowiki>
 
<?php
 
<?php
$gps = `luna-send -n 2 palm://com.palm.location/startTracking '{"appId": "ILovePalm", "subscribe": true}' 2>&1 | tail -1 | cut -d, -f4,5 | sed -r 's/[^-\.0-9,]//g'`;
+
$gps = `luna-send -n 1 palm://com.palm.location/getCurrentPosition {} 2>&1 | cut -d, -f4,5 | sed -r 's/[^-\.0-9,]//g'`;
 
?>
 
?>
 
<html>
 
<html>
 
<iframe width="600" height="500" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/?ie=UTF8&amp;q=<?php echo $gps ?>&amp;z=16&amp;ll=<?php echo $gps ?>&amp;output=embed"></iframe><br /><small><a href="http://maps.google.com/?ie=UTF8&amp;q=<?php echo $gps ?>&amp;z=16&amp;ll=<?php echo $gps ?>&amp;source=embed" style="color:#0000FF;text-align:left">View Larger Map</a></small>
 
<iframe width="600" height="500" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/?ie=UTF8&amp;q=<?php echo $gps ?>&amp;z=16&amp;ll=<?php echo $gps ?>&amp;output=embed"></iframe><br /><small><a href="http://maps.google.com/?ie=UTF8&amp;q=<?php echo $gps ?>&amp;z=16&amp;ll=<?php echo $gps ?>&amp;source=embed" style="color:#0000FF;text-align:left">View Larger Map</a></small>
 
</html>
 
</html>
 +
</nowiki></pre>
 +
 +
FrankenPre webos 2.1 gps.php needs the following (getCurrentPosition different output for webOS 2.0+?):
 +
<pre><nowiki>
 +
`luna-send -n 1 palm://com.palm.location/getCurrentPosition {} 2>&1 | cut -d, -f5,6 | sed -r 's/[^-\.0-9,]//g'`
 
</nowiki></pre>
 
</nowiki></pre>
  
Line 79: Line 109:
 
== Log files ==
 
== Log files ==
 
# Need to keep an eye on the /opt/var/log/lighttpd/ log files and make sure they are rotated consistently
 
# Need to keep an eye on the /opt/var/log/lighttpd/ log files and make sure they are rotated consistently
 +
 +
 +
= Tweaking for CGI with Python =
 +
 +
== Installation and tweaks ==
 +
 +
Install python25 and cgi module from [[Optware | Optware ]] following the example on the Optware page, and creat softlink:
 +
<pre><nowiki>
 +
mount -o rw,remount /
 +
ipkg-opt install python25
 +
ipkg-opt install python25 cgi
 +
mount -o ro,remount /
 +
ln -sf /var/opt/bin/python2.5 /usr/bin/python</nowiki></pre>
 +
 +
Add directories to /media/internal/www to conform to more-or-less standard practice:
 +
<pre><nowiki>mkdir /media/internal/www/htdocs
 +
mkdir /media/internal/www/cgi-bin</nowiki></pre>
 +
 +
Tweak /opt/etc/lighttpd/lighttpd.conf as follows:
 +
 +
1. Add to the server.modules list:
 +
<pre><nowiki>                “mod_cgi”,</nowiki></pre>
 +
 +
2. Change document root to match new directory:
 +
<pre><nowiki>server.document-root        = “/media/internal/www/htdocs”</nowiki></pre>
 +
 +
3. Check that the Python extension is included in the non-static list:
 +
<pre><nowiki>static-file.exclude-extensions = ( “.fcgi”, “.php”, “.pl”, “.py”, “.rb”, “.sh” )</nowiki></pre>
 +
 +
4. Add cgi-bin alias:
 +
<pre><nowiki>        “/cgi-bin/”    => “/media/internal/www/cgi-bin/”,</nowiki></pre>
 +
 +
5. Add python path assignment (note python2.5 link defined above):
 +
<pre><nowiki>cgi.assign    = ( “.py” => “/usr/bin/python” )</nowiki></pre>
 +
 +
== Usage: HTML files ==
 +
 +
In /media/internal/www/htdocs. Example:
 +
<pre><nowiki><html>
 +
<head><title>pythontest.html</title></head>
 +
<body><h1 align=center>Test of Python on webOS</h1>
 +
<form method=post action=/cgi-bin/pythontest-cgi.py>
 +
<p>
 +
JustType :) <input type=text name=instring size=100 maxlength=600 value=”“>
 +
</p>
 +
<input type=reset value=” Reset “><input type=submit value=” SEND “>
 +
</form></body></html></nowiki></pre>
 +
 +
== Usage: CGI files ==
 +
 +
In /media/internal/www/cgi-bin. Example:
 +
<pre><nowiki>#!/usr/bin/python
 +
# pythontest-cgi.py
 +
 +
import cgi
 +
import cgitb; cgitb.enable()
 +
 +
fs = cgi.FieldStorage()
 +
if fs.has_key('instring'):
 +
    instring = fs['instring'].value
 +
else:
 +
    instring = ''
 +
 +
def htmlinit():
 +
    return 'Content-type: text/html\n\n'
 +
 +
def htmlhead():
 +
    head = '<html>\n<head>\n<title>Test of Python on webOS</title>\n</head>\n<body>'
 +
    return head
 +
 +
def htmltail():
 +
    tail = '</body></html>'
 +
    return tail
 +
 +
print htmlinit()
 +
print htmlhead()
 +
print instring
 +
print htmltail()</nowiki></pre>

Latest revision as of 15:31, 19 June 2011

Configuring the Lighttpd web server

Preliminaries

  1. Gain access to Linux on your device.
  2. Setup the Optware Feed.
  3. Open the root file system to read/write with rootfs_open.

Update: Replace this older method by installing Lighty (lighttpd) from the Optware section of Preware.

Install Lighttpd with PHP support

ipkg-opt install lighttpd php-fcgi

Tweak settings

vi /opt/etc/lighttpd/lighttpd.conf

If you are going to run static HTML pages, there is no need to do this. If you want the ability to run PHP pages then this line must be uncommented:

                                "mod_fastcgi", 

For Python on CGI, see section below.

You may also want to adjust the port that the web server runs on. Sprint blocks port 80 and this package installs Lighttpd to run on port 8081 :

## bind to port (default: 80)
server.port                = 8081

Start the web server:

/opt/etc/init.d/S80lighttpd start

Update: The current methods of starting and stopping Lighty (lighttpd) are:

start mobi.optware.lighttpd
stop mobi.optware.lighttpd

It may be convenient to use a simple shell script for starting, stopping and restarting:

#!/bin/sh
# lighty.sh
if [ "$1" = "start" ]
then    start mobi.optware.lighttpd; exit
elif [ "$1" = "stop" ]
then    stop mobi.optware.lighttpd; exit
elif [ "$1" = "restart" ]
then    stop mobi.optware.lighttpd
        start mobi.optware.lighttpd
else    echo "Instruction is missing or incorrect."
        echo "Usage: lighty start|stop"; exit
fi

Add a startup script

I placed a file at /etc/event.d/optware-lighttpd so that the web server starts up after each reboot and the iptables firewall rules are executed to allow access to port 8080:

# don't start until the WebOS finishes it's normal boot
# that way no delay is added to the GUI startup
start on stopped finish
stop on runlevel [!2]

# Restart the lighttpd daemon if it exits/dies
respawn

exec /opt/sbin/lighttpd -f /opt/etc/lighttpd/lighttpd.conf

pre-start script
        /usr/sbin/iptables -D INPUT -p tcp --dport 8081 -j ACCEPT || /bin/true
        /usr/sbin/iptables -I INPUT -p tcp --dport 8081 -j ACCEPT
end script

Reboot the phone so that the firewall rules are executed:

/sbin/reboot

Add files to the web directory:

The default home directory lives at:

/opt/share/www/

Update: The default home directory defined in the conf file is now

/media/internal/www/

However, it useful to create subdirectories cgi-bin (for CGI scripts) and htdocs (for HTML documents). (See the Python section below for the relevant conf file tweaks.)

Sample gps.php file that will load your current location via Google maps:

<?php
$gps = `luna-send -n 1 palm://com.palm.location/getCurrentPosition {} 2>&1 | cut -d, -f4,5 | sed -r 's/[^-\.0-9,]//g'`;
?>
<html>
<iframe width="600" height="500" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/?ie=UTF8&q=<?php echo $gps ?>&z=16&ll=<?php echo $gps ?>&output=embed"></iframe><br /><small><a href="http://maps.google.com/?ie=UTF8&q=<?php echo $gps ?>&z=16&ll=<?php echo $gps ?>&source=embed" style="color:#0000FF;text-align:left">View Larger Map</a></small>
</html>

FrankenPre webos 2.1 gps.php needs the following (getCurrentPosition different output for webOS 2.0+?):

`luna-send -n 1 palm://com.palm.location/getCurrentPosition {} 2>&1 | cut -d, -f5,6 | sed -r 's/[^-\.0-9,]//g'`

Other Considerations

Dynamic DNS

  1. Install the ez-ipupdate package
  2. Configure ez-ipupdate to update a DDNS service any time the EVDO IP changes
  3. CNAME record of primary domain - I have a CNAME record pointed to my DDNS record so that I always have the most up-to-date IP of my phone

Log files

  1. Need to keep an eye on the /opt/var/log/lighttpd/ log files and make sure they are rotated consistently


Tweaking for CGI with Python

Installation and tweaks

Install python25 and cgi module from Optware following the example on the Optware page, and creat softlink:

mount -o rw,remount /
ipkg-opt install python25
ipkg-opt install python25 cgi
mount -o ro,remount /
ln -sf /var/opt/bin/python2.5 /usr/bin/python

Add directories to /media/internal/www to conform to more-or-less standard practice:

mkdir /media/internal/www/htdocs
mkdir /media/internal/www/cgi-bin

Tweak /opt/etc/lighttpd/lighttpd.conf as follows:

1. Add to the server.modules list:

                “mod_cgi”,

2. Change document root to match new directory:

server.document-root        = “/media/internal/www/htdocs”

3. Check that the Python extension is included in the non-static list:

static-file.exclude-extensions = ( “.fcgi”, “.php”, “.pl”, “.py”, “.rb”, “.sh” )

4. Add cgi-bin alias:

        “/cgi-bin/”    => “/media/internal/www/cgi-bin/”,

5. Add python path assignment (note python2.5 link defined above):

cgi.assign    = ( “.py” => “/usr/bin/python” )

Usage: HTML files

In /media/internal/www/htdocs. Example:

<html>
<head><title>pythontest.html</title></head>
<body><h1 align=center>Test of Python on webOS</h1>
<form method=post action=/cgi-bin/pythontest-cgi.py>
<p>
JustType :) <input type=text name=instring size=100 maxlength=600 value=”“>
</p>
<input type=reset value=” Reset “><input type=submit value=” SEND “>
</form></body></html>

Usage: CGI files

In /media/internal/www/cgi-bin. Example:

#!/usr/bin/python
# pythontest-cgi.py

import cgi
import cgitb; cgitb.enable()

fs = cgi.FieldStorage()
if fs.has_key('instring'):
    instring = fs['instring'].value
else:
    instring = ''

def htmlinit():
    return 'Content-type: text/html\n\n'

def htmlhead():
    head = '<html>\n<head>\n<title>Test of Python on webOS</title>\n</head>\n<body>'
    return head

def htmltail():
    tail = '</body></html>'
    return tail

print htmlinit()
print htmlhead()
print instring
print htmltail()