Page 1 of 1

Using Python On Apache2 Webserver

Posted: Sat Feb 01, 2014 6:28 pm
by DangerousDave

I've recently started playing with the Pi and I've set it up as a webserver. I have created several html files which I can view by typing in the IP address of the Pi ( I thought it would be good to be able to use Python to output html so the content can be more dynamic but I'm not having success in setting it up. I've been following the tutorials on with success until now.

I've got python installed and working (scripts running using a cron job) and the webserver is working OK with html files, but despite following this tutorial, and following potential solutions for numerous people with the same problem, I just cant seem to get it working. Tutorial is here: ... ython.html

So, I've got a python file (called which simply has the following code:

Code: Select all


import cgi
This should splurge out a load of stuff about the status of cgi on my Pi. This script runs OK when I run it manually (i.e. by typing python I've also tried other examples such as the one on the url above and get the same result.

In the file "default" in my Apache2 "sites-enabled" folder, I have the additional "ExecCGI" added to my options line for /var/www/ and I've added the line "AddHandler cgi-script .py".

Whenever I load the page through Google Chrome, I get an "Internal Server Error". Looking at the logs on my Pi, I get "Premaure end of script headers" and "No such file or directory: exec of '/var/www/' failed" in my Apache2 error log.

I have been searching various potential solutions all day without any luck. If I remove the "ExecCGI" "AddHandler" lines mentioned above, the text of the file loads, so the file is there!

Can anyone help me with where I am going wrong?

Re: Using Python On Apache2 Webserver

Posted: Sat Feb 01, 2014 7:07 pm
by WebPi
There are two things that I noticed:

1. Your script needs to print an HTTP header with a line like this:

Code: Select all

print "Content-type: text/html\n\n"
2. It looks like, you put in /var/www. It's meant to go in /usr/lib/cgi-bin. The AddHandler directive needs to go in the section of the config file for /usr/lib/cgi-bin:

Code: Select all

	ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
	<Directory "/usr/lib/cgi-bin">
		AllowOverride None
		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                AddHandler cgi-script .py
		Order allow,deny
		Allow from all
If you haven't done it already, you also need to make the script executable with this command:

Code: Select all

sudo chmod +x /usr/lib/cgi-bin/

Re: Using Python On Apache2 Webserver

Posted: Sat Feb 01, 2014 7:47 pm
by DangerousDave
Awsome. Working now. I missed the part about them needing to run from cgi-bin. Rookie mistake. Sorry and thank you for your help.

Amazing website by the way. I've worked through all of your articles on the temperature logger and related items! :) :)

Re: Using Python On Apache2 Webserver

Posted: Sat Feb 01, 2014 11:57 pm
by WebPi
Glad it's working for you now :)