Thursday 7 November 2013

Raspberry web interface for scripts

After getting some of my scripts working, I wanted to be able to turn them on via a web interface. I've made a very basic interface to turn on the scripts. Its not going to win any design awards, but it does what I want for the momment. I might add some features to it later.

My OS does come with apache installed but I reckon this might hog resources, especially as I may be running kismet at the same time. Enter lighttpd. I think it runs as one process and has way less overhead.

apt-get install lighttpd

You have to edit some things in lighttpd.conf before you have it up and running.
path: /etc/lighttpd/lighttpd.conf
Now in order to get Python and bash working we need to enable cgi
add in "mod_cgi",

From trying out my web gui it seems the default www-data user doesn't have permissions set to initiate scripts, even if you modify the sudoers file to give permissions, you'll still run into problems. I've added the user root to make sure they do run. Of course this is not exactly secure running scripts as root from a web application! But it gets it working. Well not quite.. lighttpd have added in a security net to stop you adding root user..
You have to actually modify the source files to get around this, recompile and then replace the lighttpd file with the original.
Manually install lighttpd and download the tar file.
Heres a good step through: guide
Heres the link to the download: download page
From there jump into the src folder and look for server.c file. Now we have to edit an if statement that stops you being root user.
I just deleted the two if statements which stopped uid and gid being 0 (root).
Now you can save the file and run the installer. Afterwards you need to copy the created lighttpd file which you will use in a fresh install of lighttpd. Save it to your desktop.
Now apt-get install lighttpd.
We just need to replace our modified lighttpd file with the freshly installed one.
Head to usr/sbin and look for the lighttpd file. Replace it with your modified lighttpd file.

Here is my modified lighttpd.conf file where I added root as user.

Also at the end of the file add this so it knows where to look for python and bash so we can run scripts.


Now create a folder where you will hold all your scripts to run:
Head to /var/www and create a new folder, mkdir cgi-bin

Now pile all your scripts into cgi-bin and your ready to make a web page that will call them.

I made my test page in /var/www and called it test.html


Heres the page:

Now I can browse to my pi address i.e http://192.168.43.15/test.html

If you want lighttpd to start on boot so you can jump into the web interface,
head to etc/rc.local
add in : service lighttpd start
Now it will start on boot. Yippee