Hello World in mod_wsgi

Prakhar picture Prakhar · Aug 29, 2012 · Viewed 23.8k times · Source

After failing repeatedly in my quest to get my flask application to run on Apache using mod_wsgi I decided to try running the hello world example. Here is what I have -

Directory Structure (I changed the apache default /var/www to ~/public_html)

- public_html    
   - wsgi-scripts
      - test_wsgi.wsgi
   - test_wsgi
      - test_wsgi.wsgi

test_wsgi.wsgi file

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]

VirtualHost Configuration file (called testwsgi) - This resides in /etc/apache2/sites-enabled/

<VirtualHost *:80>
    DocumentRoot ~/public_html/test_wsgi

    <Directory ~/public_html/test_wsgi>
        Order allow,deny
        Allow from all
    </Directory>

    WSGIScriptAlias /wsgi ~/public_html/wsgi-scripts/test_wsgi.wsgi

    <Directory ~/public_html/wsgi-scripts>
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

When I try going to localhost/wsgi on the browser I get a 404 Not Found error. What am I doing wrong? This is the first time I'm trying to deploy an app on a production server. Until now I took the easy way of using Google App Engine. I cant proceed to deploy my flask app until this is up and running. Thanks a lot!

Answer

Mike Pennington picture Mike Pennington · Aug 29, 2012

You need to use an absolute path, i.e. don't use ~. This works fine for me...

[mpenning@tsunami public_html]$ sudo cat /etc/apache2/sites-available/wsgi_test
<VirtualHost *:80>
    ServerName wsgihost
    DocumentRoot /home/mpenning/public_html
    WSGIScriptAlias / /home/mpenning/public_html/test.wsgi
</VirtualHost>
[mpenning@tsunami public_html]$

First I set up a hostname in /etc/hosts, so I could ensure that I can mux on the hostname in the query...

[mpenning@tsunami public_html]$ grep wsgihost /etc/hosts
127.0.1.1       tsunami.foo.net  tsunami wsgihost
[mpenning@tsunami public_html]$

Restart apache, and issue a wget...

[mpenning@tsunami public_html]$ wget http://wsgihost/
--2012-08-29 05:50:26--  http://wsgihost/
Resolving wsgihost... 127.0.1.1
Connecting to wsgihost|127.0.1.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 12 [text/plain]
Saving to: âindex.html.3â

100%[======================================>] 12          --.-K/s   in 0s

2012-08-29 05:50:26 (1.48 MB/s) - âindex.html.3â

[mpenning@tsunami public_html]$ cat index.html
Hello World![mpenning@tsunami public_html]$ #  <------