Difference between revisions of "Lighttpd"

From ArmadeusWiki
Jump to: navigation, search
(Installation)
 
(35 intermediate revisions by the same user not shown)
Line 24: Line 24:
 
</pre>
 
</pre>
 
* Save the configuration
 
* Save the configuration
* Then build the binaries and the default filesystem. This will be useful later to perform the configuration:
+
* Then build the binaries and the default filesystem:
 
<pre class="host">
 
<pre class="host">
 
  $ make
 
  $ make
 
</pre>
 
</pre>
  
==Configuration==
+
==Server configuration==
  
 
=== Generalities ===
 
=== Generalities ===
  
To apply a permanent configuration, be sure not to change anything on the board filesystem: that would be erased the next time you reflash the rootfs. <br>
+
To apply a permanent configuration, be sure not to change anything on the board filesystem: that would be erased the next time you reflash the rootfs.
 +
 
 
Instead, do all changes on the filesystem "image" located on your Host PC in ''buildroot/output/target/''.
 
Instead, do all changes on the filesystem "image" located on your Host PC in ''buildroot/output/target/''.
  
The Lighttpd configuration file is located in ''/etc/lighttpd.conf'' on the board; this means that to permanently change the configuration, you need to edit the file ''buildroot/output/target/etc/lighttpd.conf'' '''on the Host'''.
+
The Lighttpd configuration file is located in ''/etc/lighttpd/lighttpd.conf'' on the board; this means that to permanently change the configuration, you need to edit the file ''buildroot/output/target//etc/lighttpd/lighttpd.conf'' '''on the Host'''.
  
 
There is an envt variable to easily access the target directory:
 
There is an envt variable to easily access the target directory:
Line 43: Line 44:
 
</pre>
 
</pre>
 
Then the directory can be accessed with ''$ARMADEUS_ROOTFS_DIR''.
 
Then the directory can be accessed with ''$ARMADEUS_ROOTFS_DIR''.
 +
 +
* So, create a new lighttpd configuration file:
 +
<pre class="host">
 +
$ mv $ARMADEUS_ROOTFS_DIR/etc/lighttpd/lighttpd.conf $ARMADEUS_ROOTFS_DIR/etc/lighttpd/lighttpd.conf.old
 +
$ vi $ARMADEUS_ROOTFS_DIR/etc/lighttpd/lighttpd.conf
 +
</pre>
 +
* and put it the following lines:
 +
<pre class="host">
 +
var.log_root    = "/var/log"
 +
var.server_root = "/var/www"
 +
var.state_dir  = "/var/run"
 +
var.home_dir    = "/var/lib/lighttpd"
 +
var.conf_dir    = "/etc/lighttpd"
 +
 +
server.document-root = "/var/www/"
 +
server.errorlog = log_root + "/lighttpd-error.log"
 +
server.port = 80 
 +
 +
mimetype.assign = (
 +
  ".html" => "text/html",
 +
  ".txt" => "text/plain",
 +
  ".jpg" => "image/jpeg",
 +
  ".png" => "image/png"
 +
)
 +
</pre>
  
 
=== Create a Dummy page ===
 
=== Create a Dummy page ===
  
*By default, DocumentRoot is set to /var/www, so let's add a dummy file here to test our configuration:
+
* By default, ''server.document-root'' is set to ''/var/www'', so let's add a dummy file here to test our configuration:
 
<pre class="host">
 
<pre class="host">
  $ mkdir $ARMADEUS_ROOTFS_DIR/var/www
+
  $ mkdir -p $ARMADEUS_ROOTFS_DIR/var/www
 
  $ vim $ARMADEUS_ROOTFS_DIR/var/www/index.html
 
  $ vim $ARMADEUS_ROOTFS_DIR/var/www/index.html
 
</pre>
 
</pre>
Line 56: Line 82:
 
<html>
 
<html>
 
     <head>
 
     <head>
         <title> Welcome on the Armadeus Board </title>
+
         <title> Welcome on an Armadeus Board </title>
 
     </head>
 
     </head>
 
     <body>
 
     <body>
         <nowiki><h1> Welcome on the Armadeus Board </h1></nowiki>
+
         <h1> Welcome on your APF! :-) </h1>
 
     </body>
 
     </body>
 
</html>
 
</html>
 
</source>
 
</source>
  
=== Enable hostname resolution ===
+
== Test the result ==
  
*Boa has to be able to resolve the hostname to work, so let's do:
+
* Create the final rootfs:
<pre class="host">
+
$ make
  $ echo "127.0.0.1 `cat $ARMADEUS_ROOTFS_DIR/etc/hostname`" >> $ARMADEUS_ROOTFS_DIR/etc/hosts
+
 
 +
* Then [[Target_Software_Installation | update the rootfs]] on your board. Reboot it, and lighttpd should start automagically:
 +
<pre class=apf>
 +
  Starting lighttpd: OK
 
</pre>
 
</pre>
  
=== Create the access log directory ===
+
* To access your web page, use a browser:
 +
http://''<your board's IP Address>''/index.html
  
*By default, Boa logs will be stored in ''/var/log/boa/''. If you didn't not change this setting, you will need to perform the following :
+
* You should see the page !
 +
* If not, check the file ''/var/log/lighttpd-error.log'' on your board
 +
 
 +
== Working with CGI ==
 +
* Create an new ''cgi.conf'' configuration file (on your Host) with the following lines:
 
<pre class="host">
 
<pre class="host">
$ ls -l $ARMADEUS_ROOTFS_DIR/var/log
+
$ vi $ARMADEUS_ROOTFS_DIR/etc/lighttpd/conf.d/cgi.conf
 
</pre>
 
</pre>
  
*If it's a link to ''/tmp'', then:
 
 
<pre class="host">
 
<pre class="host">
  $ rm -f $ARMADEUS_ROOTFS_DIR/var/log && mkdir $ARMADEUS_ROOTFS_DIR/var/log/
+
server.modules += ( "mod_cgi" )
 +
 
 +
cgi.assign                = ( ".pl" => "/usr/bin/perl",
 +
                              ".cgi" => "/bin/sh",
 +
                              ".sh"  => "/bin/sh",
 +
                              ".py"  => "/usr/bin/python" )
 +
 
 +
index-file.names          += ( "index.pl",  "default.pl",
 +
                              "index.py",  "default.py",
 +
                              "index.php",  "default.php" )
 
</pre>
 
</pre>
  
*Then, create the boa directory :
+
* Then add this configuration sourcing in ''lighttpd.conf'':
 
<pre class="host">
 
<pre class="host">
$ mkdir $ARMADEUS_ROOTFS_DIR/var/log/boa
+
$ vi $ARMADEUS_ROOTFS_DIR/etc/lighttpd/lighttpd.conf
 
</pre>
 
</pre>
  
-----
+
<pre class="host">
Why shoud I delete the symbolink link? <br>
+
This is because during the filesystem creation, mkfs.jffs2 will keep the link from ''/var/log'' to ''/tmp'' on the board.
+
Thus, once on the board, you will have:
+
<pre class=apf>
+
# ls -l /var/log
+
lrwxrwxrwx    1 root    root            4 Apr 15  2007 /var/log -> /tmp
+
</pre>
+
  
With such a behavior, even if you fill the directory ''$ARMADEUS_ROOTFS_DIR/var/log/'' on your host
+
...
(which actually fill ''/tmp'' on your host), nothing will be saved once the filesystem will be flashed on your board.
+
##
 +
## CGI
 +
##
 +
include "conf.d/cgi.conf"
  
== Test the result ==
+
mimetype.assign = (
 +
...
 +
</pre>
  
*Create the final rootfs:
+
* Now, let's write a dummy CGI script that will get IP address of your board:
$ make
+
<pre class="host">
 
+
  $ vim $ARMADEUS_ROOTFS_DIR/var/www/ip.sh
*Then [[Target_Software_Installation | update the rootfs]] on your board. Reboot it, and start Boa:
+
<pre class=apf>
+
  # boa
+
 
</pre>
 
</pre>
  
*To access your web page, use a browser:
+
<source lang="bash">
http://''<your board's IP Address>''/
+
#!/bin/sh
 +
echo -e "Content-type: text/html\r\n\r\n";
 +
echo -e `/sbin/ifconfig eth0 | grep 'inet addr'`;
 +
</source>
  
You should see the page ! <br />
+
* The script have to be executable:
If not, check the file ''/var/log/lighttpd/error_log'' on your board
+
$ chmod a+x $ARMADEUS_ROOTFS_DIR/var/www/ip.sh
  
[[Image:Boa_welcome.png]]
+
* Then change your ''index.html'' page to add a link to the CGI:
  
== Working with cgi ==
+
$ vi $ARMADEUS_ROOTFS_DIR/var/www/index.html
 +
<source lang="html4strict">
 +
<html>
 +
    <head>
 +
        <title> Welcome on an Armadeus Board </title>
 +
    </head>
  
*First, check the ''lighttpd.conf'' file. Once more, you should use the one located on your host before changing anything.
+
    <body>
$ vi $ARMADEUS_ROOTFS_DIR/etc/lighttpd.conf
+
        <h1> Welcome on your APF! :-) </h1>
 +
        <a href=ip.sh> Get the board IP config </a><br><br>
 +
    </body>
 +
</html>
 +
</source>
  
*Be sure to have an uncommented line like this:
+
* Create the new rootfs:
  ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
+
<pre class="host">
 +
  $ make
 +
</pre>
  
*Now, let's write a dummy cgi:
+
* Finally, [[Target_Software_Installation | transfer the rootfs]] to the board & flash it.
  
$ mkdir $ARMADEUS_ROOTFS_DIR/usr/lib/cgi-bin
+
You can now access to the new web page and click on the CGI link !
$ vim $ARMADEUS_ROOTFS_DIR/usr/lib/cgi-bin/get_ipconfig.sh
+
  
*And add the following :
+
[[Image:Lighttpd_tuto_ip.png]]
<source lang="bash">
+
#!/bin/sh
+
echo -e "Content-type: text/html\r\n\r\n";
+
echo -e `/sbin/ifconfig  eth0 | grep 'inet addr'`;
+
</source>
+
The script have to be executable:
+
$ chmod a+x $ARMADEUS_ROOTFS_DIR/usr/lib/cgi-bin/get_ipconfig.sh
+
  
*Then change your index.html page to add a link to the cgi:
+
==Sending data to the board with CGI==
 
+
* By using forms and some HTML5 widget, we can easily achieve some basic functionalities. Here we will change LCD backlight from the Web interface.
$ vi $ARMADEUS_ROOTFS_DIR/var/www/index.html
+
* Edit ''index.html'':
 +
<pre class="host">
 +
$ vim $ARMADEUS_ROOTFS_DIR/var/www/index.html
 +
</pre>
 +
*  and add it the following form:
 
<source lang="html4strict">
 
<source lang="html4strict">
 
<html>
 
<html>
 
     <head>
 
     <head>
         <title> Welcome on the Armadeus Board </title>
+
         <title> Welcome on an Armadeus Board </title>
 
     </head>
 
     </head>
 +
 
     <body>
 
     <body>
         <h1> Welcome on the Armadeus Board </h1>
+
         <h1> Welcome on your APF ! :-)</h1>
         <br> <br>
+
         <a href=ip.sh> Get the board IP config </a><br><br>
         <a href=http://<Your board IP Address>/cgi-bin/get_ipconfig.sh> Get the board ip config </a>
+
 
         <br>
+
         <form action="form.sh" method="get">
 +
            Choose backlight value: <br>
 +
            <input id="slider1" type="range" name="pwm" min="0" max="7" step="1" onchange="textbox1.value = slider1.value" /><br>
 +
            <input id="textbox1" type="text" /><br>
 +
            <input type="submit" value="Change it!">
 +
         </form>
 
     </body>
 
     </body>
 
</html>
 
</html>
 
</source>
 
</source>
  
*Create the new rootfs:
+
* This will create a slider to choose a value (here in [0-7], for APF6. On other APF you can change values interval to better match backlight driver capabilities):
$ make
+
  
*Finally, [[Target_Software_Installation | transfer the rootfs]] to the board & flash it.
+
* After having choosed backlight intensity and clicked "Change it!" button, CGI script form.sh will be called with some environement variable set. The variable useful to get form values is ''QUERY_STRING''. So create a new script called form.sh and put it the following lines:
 +
<pre class="host">
 +
$ vim $ARMADEUS_ROOTFS_DIR/var/www/form.sh
 +
</pre>
  
You can now access to the new web page and click on the cgi link !
+
<source lang="bash">
 +
#!/bin/bash
 +
 
 +
echo "Content-type: text/html"
 +
echo ""
 +
 
 +
echo '<html>'
 +
echo '<head>'
 +
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
 +
echo '<title>Environment Variables</title>'
 +
echo '</head>'
 +
echo '<body>'
 +
echo 'Environment Variables:'
 +
echo '<pre>'
 +
/usr/bin/env
 +
echo '</pre>'
 +
 
 +
pwm_value=`echo "$QUERY_STRING" | cut -d '&' -f 1 | cut -d '=' -f 2`
 +
echo "VALUE=$pwm_value ($QUERY_STRING)"
 +
 
 +
echo $pwm_value > /sys/class/backlight/backlight_lcd/brightness
 +
 
 +
echo '</body>'
 +
echo '</html>'
 +
 
 +
exit 0
 +
</source>
 +
 
 +
* ''/sys/class/backlight/backlight_lcd/brightness'' have to be adapted depending on your board's [[backlight]] driver name.
 +
* This script has of course to be set as executable:
 +
 
 +
<pre class="host">
 +
$ chmod a+x $ARMADEUS_ROOTFS_DIR/var/www/form.sh
 +
</pre>
 +
 
 +
* This CGI will display the environment content of the HTML form sent from the Web browser and execute the Backlight update depending on the form's value.
 +
* Re-generate your rootfs:
 +
<pre class="host">
 +
$ make
 +
</pre>
 +
* and reflash it on your board. Then reload page on your browser.
 +
* Tada !!!
 +
[[Image:Lighttpd_tuto_form.png]]
  
 
==Links==
 
==Links==
 
* [https://www.lighttpd.net/ The Lighttpd Web page]
 
* [https://www.lighttpd.net/ The Lighttpd Web page]
 +
* [http://www.yolinux.com/TUTORIALS/BashShellCgi.html CGI with Bash tutorial]
  
 
[[Category:Network]]
 
[[Category:Network]]
 
[[Category:Web]]
 
[[Category:Web]]

Latest revision as of 17:10, 20 April 2016

How-to install and configure the Lighttpd Web server on your Armadeus board.

Lighttpd.png


Installation

  • Launch Buildroot's configuration:
 $ make menuconfig
  • Select:
...
Target packages  --->
        ...
    [*] Networking applications  --->
            ...
            [*] lighttpd
            [ ]   openssl support
            [ ]   zlib support
            [ ]   bzip2 support
            [ ]   pcre support
            [ ]   webdav support
  • Save the configuration
  • Then build the binaries and the default filesystem:
 $ make

Server configuration

Generalities

To apply a permanent configuration, be sure not to change anything on the board filesystem: that would be erased the next time you reflash the rootfs.

Instead, do all changes on the filesystem "image" located on your Host PC in buildroot/output/target/.

The Lighttpd configuration file is located in /etc/lighttpd/lighttpd.conf on the board; this means that to permanently change the configuration, you need to edit the file buildroot/output/target//etc/lighttpd/lighttpd.conf on the Host.

There is an envt variable to easily access the target directory:

 $ make shell_env && source armadeus_env.sh

Then the directory can be accessed with $ARMADEUS_ROOTFS_DIR.

  • So, create a new lighttpd configuration file:
$ mv $ARMADEUS_ROOTFS_DIR/etc/lighttpd/lighttpd.conf $ARMADEUS_ROOTFS_DIR/etc/lighttpd/lighttpd.conf.old
$ vi $ARMADEUS_ROOTFS_DIR/etc/lighttpd/lighttpd.conf
  • and put it the following lines:
var.log_root    = "/var/log"
var.server_root = "/var/www"
var.state_dir   = "/var/run"
var.home_dir    = "/var/lib/lighttpd"
var.conf_dir    = "/etc/lighttpd"

server.document-root = "/var/www/"
server.errorlog = log_root + "/lighttpd-error.log"
server.port = 80  

mimetype.assign = (
  ".html" => "text/html",
  ".txt" => "text/plain",
  ".jpg" => "image/jpeg",
  ".png" => "image/png"
)

Create a Dummy page

  • By default, server.document-root is set to /var/www, so let's add a dummy file here to test our configuration:
 $ mkdir -p $ARMADEUS_ROOTFS_DIR/var/www
 $ vim $ARMADEUS_ROOTFS_DIR/var/www/index.html
  • And fill the file with:
<html>
    <head>
        <title> Welcome on an Armadeus Board </title>
    </head>
    <body>
        <h1> Welcome on your APF! :-) </h1>
    </body>
</html>

Test the result

  • Create the final rootfs:
$ make
  • Then update the rootfs on your board. Reboot it, and lighttpd should start automagically:
 Starting lighttpd: OK
  • To access your web page, use a browser:
http://<your board's IP Address>/index.html
  • You should see the page !
  • If not, check the file /var/log/lighttpd-error.log on your board

Working with CGI

  • Create an new cgi.conf configuration file (on your Host) with the following lines:
$ vi $ARMADEUS_ROOTFS_DIR/etc/lighttpd/conf.d/cgi.conf
server.modules += ( "mod_cgi" )

cgi.assign                 = ( ".pl"  => "/usr/bin/perl",
                               ".cgi" => "/bin/sh",
                               ".sh"  => "/bin/sh",
                               ".py"  => "/usr/bin/python" )

index-file.names           += ( "index.pl",   "default.pl",
                               "index.py",   "default.py",
                               "index.php",  "default.php" )
  • Then add this configuration sourcing in lighttpd.conf:
$ vi $ARMADEUS_ROOTFS_DIR/etc/lighttpd/lighttpd.conf

...
##
## CGI
##
include "conf.d/cgi.conf"

mimetype.assign = (
...
  • Now, let's write a dummy CGI script that will get IP address of your board:
 $ vim $ARMADEUS_ROOTFS_DIR/var/www/ip.sh
#!/bin/sh
echo -e "Content-type: text/html\r\n\r\n";
echo -e `/sbin/ifconfig eth0 | grep 'inet addr'`;
  • The script have to be executable:
$ chmod a+x $ARMADEUS_ROOTFS_DIR/var/www/ip.sh
  • Then change your index.html page to add a link to the CGI:
$ vi $ARMADEUS_ROOTFS_DIR/var/www/index.html
<html>
    <head>
        <title> Welcome on an Armadeus Board </title>
    </head>

    <body>
        <h1> Welcome on your APF! :-) </h1>
        <a href=ip.sh> Get the board IP config </a><br><br>
    </body>
</html>
  • Create the new rootfs:
 $ make

You can now access to the new web page and click on the CGI link !

Lighttpd tuto ip.png

Sending data to the board with CGI

  • By using forms and some HTML5 widget, we can easily achieve some basic functionalities. Here we will change LCD backlight from the Web interface.
  • Edit index.html:
$ vim $ARMADEUS_ROOTFS_DIR/var/www/index.html
  • and add it the following form:
<html>
    <head>
        <title> Welcome on an Armadeus Board </title>
    </head>

    <body>
        <h1> Welcome on your APF ! :-)</h1>
        <a href=ip.sh> Get the board IP config </a><br><br>

        <form action="form.sh" method="get">
            Choose backlight value: <br>
            <input id="slider1" type="range" name="pwm" min="0" max="7" step="1" onchange="textbox1.value = slider1.value" /><br>
            <input id="textbox1" type="text" /><br>
            <input type="submit" value="Change it!">
        </form>
    </body>
</html>
  • This will create a slider to choose a value (here in [0-7], for APF6. On other APF you can change values interval to better match backlight driver capabilities):
  • After having choosed backlight intensity and clicked "Change it!" button, CGI script form.sh will be called with some environement variable set. The variable useful to get form values is QUERY_STRING. So create a new script called form.sh and put it the following lines:
$ vim $ARMADEUS_ROOTFS_DIR/var/www/form.sh
#!/bin/bash

echo "Content-type: text/html"
echo ""

echo '<html>'
echo '<head>'
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
echo '<title>Environment Variables</title>'
echo '</head>'
echo '<body>'
echo 'Environment Variables:'
echo '<pre>'
/usr/bin/env
echo '</pre>'

pwm_value=`echo "$QUERY_STRING" | cut -d '&' -f 1 | cut -d '=' -f 2`
echo "VALUE=$pwm_value ($QUERY_STRING)"

echo $pwm_value > /sys/class/backlight/backlight_lcd/brightness

echo '</body>'
echo '</html>'

exit 0
  • /sys/class/backlight/backlight_lcd/brightness have to be adapted depending on your board's backlight driver name.
  • This script has of course to be set as executable:
$ chmod a+x $ARMADEUS_ROOTFS_DIR/var/www/form.sh
  • This CGI will display the environment content of the HTML form sent from the Web browser and execute the Backlight update depending on the form's value.
  • Re-generate your rootfs:
$ make
  • and reflash it on your board. Then reload page on your browser.
  • Tada !!!

Lighttpd tuto form.png

Links