Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up Python with WSGI on Apache for a directory

I am trying to setup Python with WSGI for a particular directory on Apache but I am getting the following error:

mod_wsgi (pid=3857): Target WSGI script '/var/www/test/test.py' does not contain WSGI application 'application'.

My test.py contains:

print 'Hello, World!'

And my wsgi.conf contains:

LoadModule wsgi_module modules/mod_wsgi.so

WSGIPythonHome /usr/local/bin/python2.7

Alias /test/ /var/www/test/test.py

<Directory /var/www/test>
    SetHandler wsgi-script
    Options ExecCGI
    Order deny,allow
        Allow from all
</Directory>

On top of all that, interestingly enough, the web browser returns a "404 Not Found" error but thankfully the error_log is a little more enlightening.

What am I doing wrong?

like image 660
hanleyhansen Avatar asked Jan 19 '13 02:01

hanleyhansen


People also ask

Does Apache support WSGI?

mod_wsgi is an Apache module which can host any Python WSGI application, including Django. Django will work with any version of Apache which supports mod_wsgi. The official mod_wsgi documentation is your source for all the details about how to use mod_wsgi.

What is Apache WSGI?

mod_wsgi is an Apache HTTP Server module that embeds a Python application within the server and allow them to communicate through the Python WSGI interface as defined in the Python PEP 333. WSGI is one of the Python ways to produce high quality and high performance web applications.


1 Answers

You're using WSGI as though it was CGI (strangely without headers).

What you need to do, for your immediate problem is adapt the following from http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

So that you have application present.

And from the referenced doc.

Note that mod_wsgi requires that the WSGI application entry point be called 'application'. If you want to call it something else then you would need to configure mod_wsgi explicitly to use the other name. Thus, don't go arbitrarily changing the name of the function. If you do, even if you set up everything else correctly the application will not be found.

like image 55
Jon Clements Avatar answered Oct 13 '22 16:10

Jon Clements