Homepage Wiki Forum Buy

Webserver with CGI

From GNUBLIN

In order to run a Webserver easily, the following steps have to be done on GNUBLIN:

  • Create a network, according to the GNUBLIN-Wiki-Manual
  • Cofigure lighttpd as follows:

Change in file /etc/lighttpd/modules.conf the entry :

#include "conf.d/cgi.conf"

to

include "conf.d/cgi.conf"

Change in /etc/lighttpd/conf.d/cgi.conf the entry:

#alias.url += ( "/cgi-bin" => server_root + "/cgi-bin" )

to

alias.url += ( "/cgi-bin" => var.server_root + "/cgi-bin" )

Provided you want to use Shell Scripts with CGI, you have to add the following entry in cgi.assign:

".sh"  => "/bin/sh"

There you have to watch out to have a correct syntax, especially on the commas and the quotation marks.

Now you should create a subdirectoy in directory /srv/www/htdocs, named cgi-bin.

Following example, which must be saved in the cgi-bin directory, is controlling the GPIO11 pin.

Therefore the GPIO11 pin has to be already exported:

 #!/bin/sh
 
 if [ "$REQUEST_METHOD" == "POST" ] 
 then
  if [ `cat /sys/class/gpio/gpio11/value` == 1 ] 
  then
	echo 0 > /sys/class/gpio/gpio11/value
  else
	echo 1 > /sys/class/gpio/gpio11/value
  fi
 fi


 echo "Content-Type: text/html; charset=utf-8"
 echo "" 
 echo "<html>"
 echo " <head>"
 echo "   <title>Control Port 11 via CGI and webserver</title>"
 echo " </head>"
 echo " <body>"
 echo "  <h1>Control Port 11 via CGI and webserver</h1>"
 if [ `cat /sys/class/gpio/gpio11/value` == 1 ] 
 then
	echo "  Portstatus: On"
 else
	echo "  Portstatus: Off"
 fi
 echo "  <br><br>"
 echo "  <form action\"/cgi-bin/example.sh\" method = \"POST\">"
 echo "  <input type=\"submit\" value=\"switch\">"
 echo " </form>"
 echo " </body>"
 echo "</html>"
 

Now you should be able to start the webserver:

root@gnublin:/# /etc/init.d/lighttpd start
Syntax OK

Provided the configuration of the webserver is ok, you will get the validation therefore. With a faulty configuration you will get a respective message. If the start of the webserver was successfull, you can access the webserver via your browser:

http://192.168.0.2/cgi-bin/example.sh

Now you should reach a web environment, with which you can control the GPIO11 pin of GNUBLIN. If you have connected a LED oder a realy, you now can switch it on or off over your browser

In other languages