Version 3 (modified by 21 months ago) ( diff ) | ,
---|
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
- In the make menuconfig turn on libprce (lighthttpd depends on it)
- Turn on lighthttpd (Network -> Webservers -> LightHttpd)
- Turn on lighthttpd-mod-cgi (If CGI is desired) (Network -> Webservers -> LightHttpd -> lighthttpd-mod-cgi)
- Exit and save
- From the OpenWrt directory, compile with the command
make -j8 V=S && make package/index
- Find the IPK files located in the /bin/<target>/packages/ directory
- Install via IPK method as discussed here: ipkupload
- 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)
- Create a simple index.html file in the www1 directory (example down towards bottom of this page)
- Start the lighthttpd server with the command
/usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf
- 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 Gateworks 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() { val="$(cat /sys/class/hwmon/hwmon0/device/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+"° 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#