DangerousDave
Posts: 3
Joined: Sat Feb 01, 2014 6:11 pm

Using Python On Apache2 Webserver

Sat Feb 01, 2014 6:28 pm

Hi,

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 (192.168.1.80). 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 http://raspberrywebserver.com/ 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: http://raspberrywebserver.com/cgiscript ... ython.html

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

Code: Select all

#!/user/bin/python

import cgi
cgi.test()
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 test.py). 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/test.py' 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?

WebPi
Posts: 262
Joined: Wed Apr 10, 2013 6:47 pm
Location: Birmingham, UK
Contact: Website

Re: Using Python On Apache2 Webserver

Sat Feb 01, 2014 7:07 pm

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 test.py 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
	</Directory>
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/test.py
raspberrywebserver.com - Raspberry Pi tutorials
LinuxWebServers.net - Linux Web Server tutorials and examples
pyplate.com - Python web publishing tool

DangerousDave
Posts: 3
Joined: Sat Feb 01, 2014 6:11 pm

Re: Using Python On Apache2 Webserver

Sat Feb 01, 2014 7:47 pm

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! :) :)

WebPi
Posts: 262
Joined: Wed Apr 10, 2013 6:47 pm
Location: Birmingham, UK
Contact: Website

Re: Using Python On Apache2 Webserver

Sat Feb 01, 2014 11:57 pm

Glad it's working for you now :)
raspberrywebserver.com - Raspberry Pi tutorials
LinuxWebServers.net - Linux Web Server tutorials and examples
pyplate.com - Python web publishing tool

Return to “Troubleshooting”