Wednesday, February 20, 2013

Website accessible via dynamic DNS

To allow access to my website hosted on the raspberry pi, I need a domain name. Also because I am behind a router I will have no fixed public IP. To solve this dilemma we can use a so called dynamic DNS service, which will update our DNS entry with the current public IP of the router. http://en.wikipedia.org/wiki/Dynamic_DNS
Luckily there are several free available dynic DNS providers: http://dnslookup.me/dynamic-dns/
I decided to use no-ip.com because DynDNS is not free any more.

First you need to go to http://www.noip.com/ and register your domain name. Afterwards you need to install the no-ip client software on your raspberry pi. Usually all that it does,is to check your public ip in a specific interval and send this information to no-ip, so they can update the DNS entry.

But because I am using a FritzBox as router, I can configure directly that I want to use a Dynamic DNS provider and it takes care of everything for me (so no need to install anything on my precious pi).
All you have to do, is go to the webinterface of the FritzBox and enter your dynDNS provider, username & password.
It should look something like this:


Save everything, maybe restart your router and wait some minutes. Now you should be able to reach your website under your selected domain name from everywhere in the worl.

Note: After subscribing to no-ip.com it took some hours until my domain name was populated & working - so be a little patient!

Phyton-CGI with lighttpd

Since static html pages are boring and for little girls only, we now want to create our first website with phyton-cgi calls.

Add/change the following in your lighttpd configuration file (/etc/lighttpd/lighttpd.conf):
$HTTP["url"] =~ "^/cgi-bin/" {
        cgi.assign = ( ".py" => "/usr/bin/python" )
}
Create a directoy "/var/www/cgi-bin/" for your phyton scripts, and also make sure that phyton is installed and the binary is "/usr/bin/phyton" - otherwise change according to your setup.
Now we change or index.html file and add a simple form to it:
<form method="GET" action="/cgi-bin/test.cgi">
  <input type="submit" value="Start" name="Start"/>
</form> 
Then we create our phyton file "/var/www/cgi-bin/test.py" with following content:
#!/usr/bin/python
import cgi
import cgitb 
cgitb.enable()  # for troubleshooting
cgi.test()      # cgi test output
 
Now we can go to our website and click on the "Start"-Button, which will call our python-cgi script and outputs a page with all kinds of environment variables etc. It worked!

If you have problems try restarting the lighttpd service and check if you have the rights to read/write/execute stuff in your /var/www/ directory.

More information about phyton & cgi can be found here: http://docs.python.org/2/library/cgi.html

Raspberry Pi as webserver

Now I want to setup a small webserver on my raspberry pi. First we have to choose an appropriate lightweight webserver, because a standard Apache installation seems to big & overkill.
Two small webserver, which seem to be very good and also widely used are:
I heard a lot of good things about nginx, mainly its speed and rewriting abilities, but I decided to use lighttpd just because I found good instructions for it first. Installing:
 sudo apt-get install lighttpd
Configuration:
sudo nano /etc/lighttpd/lighttpd.conf
Check that the following modules are enabled:
server.modules = (
            "mod_access",
            "mod_alias",
            "mod_accesslog",
            "mod_auth",
            "mod_ssi",
            "mod_cgi",
            "mod_compress",
            "mod_fastcgi",
            "mod_rewrite",
            "mod_magnet",
)
The web root dir should be "/var/www/" and the user which runs lighttpd is "www-data" - no need to change anything here.
Lighttpd should be already running. You can start/stop/restart the service with:
sudo /etc/init.d/lighttpd start
sudo /etc/init.d/lighttpd stop
sudo /etc/init.d/lighttpd restart
Now create a file "/var/www/index.html" with some html code, open a Browser and go to "http://<yourIP>/index.html". VoilĂ .