Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to get Python working with Apache, however I'm failing to have success with either CGI or mod_python.

Does anyone know a good tutorial or something?


share|improve this question
up vote 16 down vote accepted

mod_python is basically non-maintained - you should look into mod_wsgi. Install the package libapache2-mod-wsgi, then issue sudo a2enmod wsgi to enable it.

Just as a quick example to get it running, stuff something like this in your /etc/apache2/sites-enabled/default:

WSGIScriptAlias /test /path/to/python/

And in the file /path/to/python/

def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain')])
    return "Hello World"

After re-starting Apache2, any request to /test will turn into a call of application() in your python file.

For further reading, look into WSGI (WebServer Gateway Interface), the way Python integrates with web-servers.

Bonus / Update:

Python (unsurprisingly) has a small WSGI server in the standard library for testing. If you add this in the bottom of your file, you can run it as any old executable for testing purposes, and then let Apache take over for production:

if __name__ == '__main__':
    from wsgiref.simple_server import make_server

    httpd = make_server('', 8080, application)
    print "Serving on http://localhost:8080"

share|improve this answer
I got the following as an error, out of the apache log. As far as I can tell, it thinks it's not getting a string. – Vallery Mar 6 '11 at 1:08
[Sat Mar 05 17:06:14 2011] [error] [client] mod_wsgi (pid=2844): Exception occurred processing WSGI script '/var/www/'. [Sat Mar 05 17:06:14 2011] [error] [client] Traceback (most recent call last): [Sat Mar 05 17:06:14 2011] [error] [client] File "/var/www/", line 2, in application [Sat Mar 05 17:06:14 2011] [error] [client] start_response(200, [('Content-Type', 'text/plain')]) [Sat Mar 05 17:06:14 2011] [error] [client] TypeError: expected byte string object for status, value of type int found – Vallery Mar 6 '11 at 1:08
Ah. My bad. I'm used to frameworks doing all the tricky stuff for me ;). I've updated the 200 to '200 OK' per – Morten Siebuhr Mar 7 '11 at 15:20
Great, that works. Last question: how do I make it display in html? My browser shows the return in plaintext, ignoring the markup. – Vallery Mar 7 '11 at 22:50
Wait, got it: change text/plain to text/html. – Vallery Mar 7 '11 at 22:52

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.