wiki:OpenWrt/webserver

Adding a Webserver To OpenWrt

Gateworks provides an OpenWrt board support package. By default, the LUCI admin has a default webserver uhttpd running on port 80

However, if one wants to add their own webserver for hosting custom content (with even CGI scripts...), it is best to install and configure one's own. OpenWrt makes it easy to add and install packages.

For this project, we chose the webserver lighthttpd. On this page we will show how to install the webserver lighthttpd and CGI-mod if desired, configure it, create an HTML file and create a CGI script.

References:

Basic Configuration

Please get familiar with building the OpenWrt BSP located here

Please also be familiar with using the make menuconfig command discussed in OpenWrt Configuration

  1. In the make menuconfig turn on libprce (lighthttpd depends on it)
  2. Turn on lighthttpd (Network -> Webservers -> LightHttpd)
  3. Turn on lighthttpd-mod-cgi (If CGI is desired) (Network -> Webservers -> LightHttpd -> lighthttpd-mod-cgi)
  4. Exit and save
  5. From the trunk directory, compile with the command: make -j8 V=99 && make package/index
  6. Find the IPK files located in the /trunk/bin/cns3xxx/packages/ directory
  7. Install via IPK method as discussed here: ipkupload
  8. Configure the webserver on the Gateworks board by editing /etc/lighttpd/lighttpd.conf and making the following changes:
    • server.document-root = "/www1/" where www1 is the root directory of the web server. Then create the www1 directory
    • server.port=8000 where 8000 is the port you want your webserver on (This is very important as by default the LUCI Web Admin is running on port 80. If you want to use port 80, you must re-configure LUCI to use something different)
  9. Create a simple index.html file in the www1 directory (example down towards bottom of this page)
  10. Start the lighthttpd server with the command
    /usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf 
    
  11. Access the server at an address like so: http://192.168.1.1:8000/index.html

Common Gateway Interface (CG)

If a Common Gateway Interface (CGI) is desired, it can be configured.

Modify the lighthttpd config file as noted above and make sure these lines are in the file:

server.modules = (
        "mod_cgi"
)
#### CGI module                                                                
cgi.assign = ( ".pl"  => "/usr/bin/perl", ".cgi" => "/usr/bin/perl", ".sh" => "" )

Once configured, write a script to access the desired function and an appropriate index.html

CGI Script

Here is a script that reads the voltage on a Laguna board: (Find more commands on the GSC page gsc)

root@OpenWrt:/www1# cat voltage.sh 
#!/bin/sh

CAT=/bin/cat

header() {
        echo Content-type: text/html
        echo ""
}

getvalue() {
        local DIR=$(find /sys/bus/i2c/devices/0-0029/ -name in0_input)
        DIR=${DIR%/*}
        val="$(cat $DIR/in0_input)"
        let "vin = val/1000"
        echo -e "$vin"
}
footer() {
        echo ""
}


header
case "$REQUEST_METHOD" in
        GET) getvalue;;
esac
footer

Index html file

Here is a sample index.html file that is used in the above image. Note the library jquery is used for the javascript ajax calls to the cgi scripts:

root@OpenWrt:/www1# cat index.html 
<html>
<head>
<script src="jquery.js"></script>



</head>

<body style="font-family:arial;bgcolor="#BDBDBD">

<style>
table, th , td,tr
{
        border: 1px solid black;
        border-collapse:collapse;
        text-align:center;
}



</style>
<script>
function getTemp()
{
        $.ajax({
                url:"temp.sh",
                type: "GET",
                success: function(data)
                {
                        $('#temperature').html(data+"&deg C");
                }
        });     


}
function getVoltage()
{
        $.ajax({
                url:"voltage.sh",
                type: "GET",
                success: function(data)
                {
                        $('#voltage').html(data+" Volts");
                }
        });     


}
$(document).ready(function() {
getVoltage();
getTemp();
});                                                                                                        
</script>
<center>
<div style="background-color:white;border: 5px solid red; width:600px;min-height:600px;">

<br>
<img alt="Gateworks" style="width:75%" src="images/gateworks.jpg"/>
<br>
<h1 style="font-family:arial">
Gateworks M2M Demo
</h1>
<br>
<div style="border: 0px solid blue;text-align:left; width:98%;">
<h3>GSC Values:</h3><br>
<table style="width:98%">
<tr>
<th>
Property
</th>
<th>
Value
</th>
</tr>
<tr>
<td>
Vin
</td>
<td>
<span id="voltage"/>
</td>
</tr>

<tr>
<td>
Processor Temperature
</td>
<td>
<span id="temperature"/>
</td>
</tr>


</table>


</div>
</div>
</center>
</body>
</html>

Troubleshooting

To see if the server is listening on a port, use the netstat command:

root@OpenWrt:/www1# netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
tcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN      
tcp        0      0 0.0.0.0:www             0.0.0.0:*               LISTEN      
tcp        0      0 0.0.0.0:domain          0.0.0.0:*               LISTEN      
tcp        0      0 :::domain               :::*                    LISTEN      
tcp        0      0 :::ssh                  :::*                    LISTEN      
tcp        0      0 :::telnet               :::*                    LISTEN      
tcp        0      0 ::ffff:192.168.1.73:telnet ::ffff:192.168.1.22:38861 ESTABLISHED 
udp        0      0 0.0.0.0:domain          0.0.0.0:*                           
udp        0      0 192.168.1.73:ntp        0.0.0.0:*                           
udp        0      0 localhost:ntp           0.0.0.0:*                           
udp        0      0 0.0.0.0:ntp             0.0.0.0:*                           
udp        0      0 :::domain               :::*                                
udp        0      0 ::1:ntp                 :::*                                
udp        0      0 fe80::2d0:12ff:fe9b:ede0:ntp :::*                                
udp        0      0 :::ntp                  :::*                                
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node Path
unix  2      [ ]         DGRAM                      1314 /var/run/hostapd-phy0/wlan0
unix  8      [ ]         DGRAM                       254 /dev/log
unix  2      [ ]         DGRAM                      1966 
unix  2      [ ]         DGRAM                      1912 
unix  2      [ ]         DGRAM                      1907 
unix  2      [ ]         DGRAM                      1729 
unix  2      [ ]         DGRAM                      1397 
unix  2      [ ]         DGRAM                       305 

Verify the process is running with the ps command:

root@OpenWrt:/www1# ps -aef | grep light
 2541 root      2680 S    /usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf
 2557 root      1140 S    grep light
root@OpenWrt:/www1# 
Last modified 3 months ago Last modified on 05/25/17 10:55:31

Attachments (1)

Download all attachments as: .zip