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.


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:

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 = (
#### 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 


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

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

        GET) getvalue;;

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 
<script src="jquery.js"></script>


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

table, th , td,tr
        border: 1px solid black;

function getTemp()
                type: "GET",
                success: function(data)
                        $('#temperature').html(data+"&deg C");

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

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

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

Processor Temperature
<span id="temperature"/>




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  *               LISTEN      
tcp        0      0   *               LISTEN      
tcp        0      0*               LISTEN      
tcp        0      0 :::domain               :::*                    LISTEN      
tcp        0      0 :::ssh                  :::*                    LISTEN      
tcp        0      0 :::telnet               :::*                    LISTEN      
tcp        0      0 ::ffff: ::ffff: ESTABLISHED 
udp        0      0*                           
udp        0      0*                           
udp        0      0 localhost:ntp *                           
udp        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
Last modified 5 weeks ago Last modified on 05/25/17 10:55:31

Attachments (1)

Download all attachments as: .zip