title: Executing a Python script in Apache2
body: <p>I am trying to execute a Python program using Apache.  However, Apache will only serve the file and not actually execute it.  The permissions on the file are r/w/x and it is in <code>/var/www</code>.  I will post the contents of <code>httpd.conf</code> and the program code after.  I also tried to running the python script as a <code>.cgi</code> file but that did not work as well.  I have both the <code>mod_python</code> and <code>mod_wsgi</code> modules loaded into apache as well.</p>
  
  <p>Python sample:</p>
  
  <pre><code>#!/usr/bin/python
  
  # enable debugging
  import cgitb
  cgitb.enable()
  
  print "Content-Type: text/plain\r\n\r\n"
  print
  
  print "Hello World!"
  </code></pre>
  
  <p>httpd.conf:</p>
  
  <pre><code>AddHandler cgi-script .cgi .pl
  AddHandler python-program .py
  </code></pre>
  
  <p>I know its a small httpd.conf file but when I installed apache, there was nothing in the file.  I should also mention that this is just so that I learn the basics of running python in apache.  It is not meant for production.
  Thanks for the help!</p>
  
  <h2>Edit</h2>
  
  <p>The OS I'm using is Ubuntu 10.04 and the version of apache is 2.  I have python version 2.6 which is automatically used when <code>#!/usr/bin/env python</code> is invoked.<br>
  I am getting two main errors, the first is that the file is not being found even though the permissions of the file and folder are 777. The error from the log is </p>
  
  <pre><code>[Sun Feb 05 13:29:44 2012] [error] [client 192.168.1.3] File does not exist: /var/www/poit-0.1
  </code></pre>
  
  <p>This error is for a different python script that I did not write.  What is weird is that the file shows up in the index of the folder when accessed from a browser window.  However, when I navigate to the file, I get the above error.</p>
  
  <p>The other error that I am getting is premature end of headers.  The error is below:</p>
  
  <pre><code>[Sun Feb 05 12:10:19 2012] [error] (8)Exec format error: exec of '/var/www/pyth.py' failed
  [Sun Feb 05 12:10:19 2012] [error] [client 192.168.1.3] Premature end of script headers: pyth.py
  </code></pre>