Changes between Initial Version and Version 1 of OpenWrt/webserver


Ignore:
Timestamp:
10/22/2017 05:28:45 AM (6 years ago)
Author:
trac
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • OpenWrt/webserver

    v1 v1  
     1[[PageOutline]]
     2
     3= Adding a Webserver To OpenWrt =
     4Gateworks provides an OpenWrt board support package. By default, the LUCI admin has a default webserver uhttpd running on port 80
     5
     6However, 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.
     7
     8For this project, we chose the webserver [http://www.lighttpd.net/ 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.
     9
     10References:
     11 * http://wiki.openwrt.org/doc/howto/http.lighttpd
     12
     13
     14== Basic Configuration ==
     15Please get familiar with building the OpenWrt BSP located [wiki:OpenWrt/building here]
     16
     17Please also be familiar with using the make menuconfig command discussed in [wiki:Configuration OpenWrt Configuration]
     18
     19 1. In the make menuconfig turn on libprce (lighthttpd depends on it)
     20 2. Turn on lighthttpd  (Network -> Webservers -> !LightHttpd)
     21 3. Turn on lighthttpd-mod-cgi (If CGI is desired)  (Network -> Webservers -> !LightHttpd -> lighthttpd-mod-cgi)
     22 4. Exit and save
     23 5. From the trunk directory, compile with the command: make -j8 V=99 && make package/index
     24 6. Find the IPK files located in the /trunk/bin/cns3xxx/packages/ directory
     25 7. Install via IPK method as discussed here: [wiki:ipkupload]
     26 8. Configure the webserver on the Gateworks board by editing  /etc/lighttpd/lighttpd.conf  and making the following changes:
     27  * '''server.document-root = "/www1/"''' where www1 is the root directory of the web server. Then create the www1 directory
     28  * '''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)'''
     29 9. Create a simple index.html file in the www1 directory (example down towards bottom of this page)
     30 10. Start the lighthttpd server with the command
     31{{{
     32/usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf
     33}}}
     34 11. Access the server at an address like so: http://192.168.1.1:8000/index.html
     35[[Image(m2mdemolighthttpd.png, 300px)]]
     36
     37
     38== Common Gateway Interface (CG) ==
     39If a Common Gateway Interface (CGI) is desired, it can be configured.
     40
     41Modify the lighthttpd config file as noted above and make sure these lines are in the file:
     42{{{
     43server.modules = (
     44        "mod_cgi"
     45)
     46}}}
     47
     48{{{
     49#### CGI module                                                               
     50cgi.assign = ( ".pl"  => "/usr/bin/perl", ".cgi" => "/usr/bin/perl", ".sh" => "" )
     51
     52}}}
     53
     54Once configured, write a script to access the desired function and an appropriate index.html
     55
     56
     57=== CGI Script ===
     58Here is a script that reads the voltage on a Laguna board: (Find more commands on the GSC page [wiki:gsc])
     59{{{
     60root@OpenWrt:/www1# cat voltage.sh
     61#!/bin/sh
     62
     63CAT=/bin/cat
     64
     65header() {
     66        echo Content-type: text/html
     67        echo ""
     68}
     69
     70getvalue() {
     71        val="$(cat /sys/class/hwmon/hwmon0/device/in0_input)"
     72        let "vin = val/1000"
     73        echo -e "$vin"
     74}
     75footer() {
     76        echo ""
     77}
     78
     79
     80header
     81case "$REQUEST_METHOD" in
     82        GET) getvalue;;
     83esac
     84footer
     85}}}
     86
     87=== Index html file ===
     88Here 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:
     89{{{
     90root@OpenWrt:/www1# cat index.html
     91<html>
     92<head>
     93<script src="jquery.js"></script>
     94
     95
     96
     97</head>
     98
     99<body style="font-family:arial;bgcolor="#BDBDBD">
     100
     101<style>
     102table, th , td,tr
     103{
     104        border: 1px solid black;
     105        border-collapse:collapse;
     106        text-align:center;
     107}
     108
     109
     110
     111</style>
     112<script>
     113function getTemp()
     114{
     115        $.ajax({
     116                url:"temp.sh",
     117                type: "GET",
     118                success: function(data)
     119                {
     120                        $('#temperature').html(data+"&deg C");
     121                }
     122        });     
     123
     124
     125}
     126function getVoltage()
     127{
     128        $.ajax({
     129                url:"voltage.sh",
     130                type: "GET",
     131                success: function(data)
     132                {
     133                        $('#voltage').html(data+" Volts");
     134                }
     135        });     
     136
     137
     138}
     139$(document).ready(function() {
     140getVoltage();
     141getTemp();
     142});                                                                                                       
     143</script>
     144<center>
     145<div style="background-color:white;border: 5px solid red; width:600px;min-height:600px;">
     146
     147<br>
     148<img alt="Gateworks" style="width:75%" src="images/gateworks.jpg"/>
     149<br>
     150<h1 style="font-family:arial">
     151Gateworks M2M Demo
     152</h1>
     153<br>
     154<div style="border: 0px solid blue;text-align:left; width:98%;">
     155<h3>GSC Values:</h3><br>
     156<table style="width:98%">
     157<tr>
     158<th>
     159Property
     160</th>
     161<th>
     162Value
     163</th>
     164</tr>
     165<tr>
     166<td>
     167Vin
     168</td>
     169<td>
     170<span id="voltage"/>
     171</td>
     172</tr>
     173
     174<tr>
     175<td>
     176Processor Temperature
     177</td>
     178<td>
     179<span id="temperature"/>
     180</td>
     181</tr>
     182
     183
     184</table>
     185
     186
     187</div>
     188</div>
     189</center>
     190</body>
     191</html>
     192}}}
     193
     194
     195== Troubleshooting ==
     196To see if the server is listening on a port, use the netstat command:
     197{{{
     198root@OpenWrt:/www1# netstat -a
     199Active Internet connections (servers and established)
     200Proto Recv-Q Send-Q Local Address           Foreign Address         State       
     201tcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN     
     202tcp        0      0 0.0.0.0:www             0.0.0.0:*               LISTEN     
     203tcp        0      0 0.0.0.0:domain          0.0.0.0:*               LISTEN     
     204tcp        0      0 :::domain               :::*                    LISTEN     
     205tcp        0      0 :::ssh                  :::*                    LISTEN     
     206tcp        0      0 :::telnet               :::*                    LISTEN     
     207tcp        0      0 ::ffff:192.168.1.73:telnet ::ffff:192.168.1.22:38861 ESTABLISHED
     208udp        0      0 0.0.0.0:domain          0.0.0.0:*                           
     209udp        0      0 192.168.1.73:ntp        0.0.0.0:*                           
     210udp        0      0 localhost:ntp           0.0.0.0:*                           
     211udp        0      0 0.0.0.0:ntp             0.0.0.0:*                           
     212udp        0      0 :::domain               :::*                               
     213udp        0      0 ::1:ntp                 :::*                               
     214udp        0      0 fe80::2d0:12ff:fe9b:ede0:ntp :::*                               
     215udp        0      0 :::ntp                  :::*                               
     216Active UNIX domain sockets (servers and established)
     217Proto RefCnt Flags       Type       State         I-Node Path
     218unix  2      [ ]         DGRAM                      1314 /var/run/hostapd-phy0/wlan0
     219unix  8      [ ]         DGRAM                       254 /dev/log
     220unix  2      [ ]         DGRAM                      1966
     221unix  2      [ ]         DGRAM                      1912
     222unix  2      [ ]         DGRAM                      1907
     223unix  2      [ ]         DGRAM                      1729
     224unix  2      [ ]         DGRAM                      1397
     225unix  2      [ ]         DGRAM                       305
     226}}}
     227
     228Verify the process is running with the ps command:
     229{{{
     230root@OpenWrt:/www1# ps -aef | grep light
     231 2541 root      2680 S    /usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf
     232 2557 root      1140 S    grep light
     233root@OpenWrt:/www1#
     234}}}